javascript - 谷歌地图 API V3 中的 tohere 和 fromhere 消息窗口未定义

标签 javascript google-maps google-maps-api-3

我的网站上正在加载 map (我不是 java 编码员,这是我在互联网上找到的代码)。我已经弄清楚如何让它满足我的需求。但是,当您单击“到这里”或“从这里”链接时,而不是从标记上填充地址,它会在消息窗口中显示为未定义。我确信这是我所缺少的简单的东西,但感谢任何帮助

var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();
// arrays to hold copies of the markers and html used by the side_bar 
// because the function closure trick doesnt work there 
var gmarkers = [];
var htmls = [];

// arrays to hold variants of the info window html with get direction forms open
var to_htmls = [];
var from_htmls = [];

// global "map" variable
var map = null;

var infowindow = new google.maps.InfoWindow({
    size: new google.maps.Size(150, 50)
});


function initialize() {

    var location = new google.maps.LatLng(33.3440017700195, -111.96068572998);

    var mapOptions = {
        center: location,
        zoom: 14,
        scrollwheel: true
    };

    map = new google.maps.Map(document.getElementById("map"),
        mapOptions);

    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("map"));
    google.maps.event.addListener(map, 'click', function() {
        infowindow.close();
    });

    var image = {
        url: 'http://maps.google.com/mapfiles/ms/micons/green.png'
    };
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(33.34396, -111.960606),
        map: map,
        animation: google.maps.Animation.DROP,
        icon: image,
        Title: 'Fusion Formulations<br>1430 W Auto Drive<br>Tempe, AZ 85284'
    });

    var i = gmarkers.length;
    latlng = location;

    // The info window version with the "to here" form open
    to_htmls[i] = html +
        '<b>To here<\/b> - <a href="javascript:fromhere(' + i + ')">From here<\/a>' +
        '<br>Start address:<form action="javascript:getDirections()">' +
        '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
        '<INPUT value="Get Directions" TYPE="button" onclick="getDirections()"><br>' +
        'Walk <input type="checkbox" name="walk" id="walk" /> &nbsp; Avoid Highways <input type="checkbox" name="highways" id="highways" />' +
        '<input type="hidden" id="daddr" value="' + latlng.lat() + ',' + latlng.lng() +
        '"/>';
    // The info window version with the "from here" form open
    from_htmls[i] = html + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here<\/a> - <b>From here<\/b>' +
        '<br>End address:<form action="javascript:getDirections()">' +
        '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
        '<INPUT value="Get Directions" TYPE="SUBMIT"><br>' +
        'Walk <input type="checkbox" name="walk" id="walk" /> &nbsp; Avoid Highways <input type="checkbox" name="highways" id="highways" />' +
        '<input type="hidden" id="saddr" value="' + latlng.lat() + ',' + latlng.lng() +
        '"/>';
    // The inactive version of the direction info
    var html = marker.getTitle() + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here<\/a> - <a href="javascript:fromhere(' + i + ')">From here<\/a>';
    var contentString = html;

    google.maps.event.addListener(marker, 'click', function() {
        map.setZoom(15);
        map.setCenter(marker.getPosition());
        infowindow.setContent(contentString);
        infowindow.open(map, marker);
    });
    // save the info we need to use later for the side_bar
    gmarkers.push(marker);
    htmls[i] = html;
}

google.maps.event.addDomListener(window, 'load', initialize);

// ===== request the directions =====
function getDirections() {
    // ==== Set up the walk and avoid highways options ====
    var request = {};
    if (document.getElementById("walk").checked) {
        request.travelMode = google.maps.DirectionsTravelMode.WALKING;
    } else {
        request.travelMode = google.maps.DirectionsTravelMode.DRIVING;
    }

    if (document.getElementById("highways").checked) {
        request.avoidHighways = true;
    }
    // ==== set the start and end locations ====
    var saddr = document.getElementById("saddr").value;
    var daddr = document.getElementById("daddr").value;

    request.origin = saddr;
    request.destination = daddr;
    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
        } else alert("Directions not found:" + status);
    });
}


// This function picks up the click and opens the corresponding info window
function myclick(i) {
    google.maps.event.trigger(gmarkers[i], "click");
}


// functions that open the directions forms
function tohere(i) {
    //gmarkers[i].openInfoWindowHtml(to_htmls[i]);
    infowindow.setContent(to_htmls[i]);
    infowindow.open(map, gmarkers[i]);
}

function fromhere(i) {
    //gmarkers[i].openInfoWindowHtml(from_htmls[i]);
    infowindow.setContent(from_htmls[i]);
    infowindow.open(map, gmarkers[i]);
}

最佳答案

您没有定义用于信息窗口中 HTML 的第一个字段的 html 变量。

// The info window version with the "to here" form open
to_htmls[i] = html +
  '<b>To here<\/b> - <a href="javascript:fromhere(' + i + ')">From here<\/a>' +
// ...
  '"/>';

这应该是您想要显示的 HTML,在案例或您的示例中,这对我有用:

html = 'Fusion Formulations<br>1430 W Auto Drive<br>Tempe, AZ 85284<br>';

此外,您还遇到了标记的 title 属性的问题。您分配的 Title 属性不同(javascript 区分大小写)。

(另外,仅供引用, MarkerOptions title 属性不支持 HTML 标记,因此您不应在标题字符串中包含 HTML 标记)

proof of concept fiddle

代码片段:

var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();
// arrays to hold copies of the markers and html used by the side_bar 
// because the function closure trick doesnt work there 
var gmarkers = [];
var htmls = [];

// arrays to hold variants of the info window html with get direction forms open
var to_htmls = [];
var from_htmls = [];

// global "map" variable
var map = null;

var infowindow = new google.maps.InfoWindow({
  size: new google.maps.Size(150, 50)
});


function initialize() {

  var location = new google.maps.LatLng(33.3440017700195, -111.96068572998);

  var mapOptions = {
    center: location,
    zoom: 14,
    scrollwheel: true
  };

  map = new google.maps.Map(document.getElementById("map"),
    mapOptions);

  directionsDisplay.setMap(map);
  directionsDisplay.setPanel(document.getElementById("map"));
  google.maps.event.addListener(map, 'click', function() {
    infowindow.close();
  });

  var image = {
    url: 'http://maps.google.com/mapfiles/ms/micons/green.png'
  };
  var marker = new google.maps.Marker({
    position: new google.maps.LatLng(33.34396, -111.960606),
    map: map,
    animation: google.maps.Animation.DROP,
    icon: image,
    Title: 'Fusion Formulations<br>1430 W Auto Drive<br>Tempe, AZ 85284'
  });
  html = 'Fusion Formulations<br>1430 W Auto Drive<br>Tempe, AZ 85284<br>';
  var i = gmarkers.length;
  latlng = location;

  // The info window version with the "to here" form open
  to_htmls[i] = html +
    '<b>To here<\/b> - <a href="javascript:fromhere(' + i + ')">From here<\/a>' +
    '<br>Start address:<form action="javascript:getDirections()">' +
    '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
    '<INPUT value="Get Directions" TYPE="button" onclick="getDirections()"><br>' +
    'Walk <input type="checkbox" name="walk" id="walk" /> &nbsp; Avoid Highways <input type="checkbox" name="highways" id="highways" />' +
    '<input type="hidden" id="daddr" value="' + latlng.lat() + ',' + latlng.lng() +
    '"/>';
  // The info window version with the "from here" form open
  from_htmls[i] = html + '<a href="javascript:tohere(' + i + ')">To here<\/a> - <b>From here<\/b>' +
    '<br>End address:<form action="javascript:getDirections()">' +
    '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
    '<INPUT value="Get Directions" TYPE="SUBMIT"><br>' +
    'Walk <input type="checkbox" name="walk" id="walk" /> &nbsp; Avoid Highways <input type="checkbox" name="highways" id="highways" />' +
    '<input type="hidden" id="saddr" value="' + latlng.lat() + ',' + latlng.lng() +
    '"/>';
  // The inactive version of the direction info
  var html = marker.getTitle() + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here<\/a> - <a href="javascript:fromhere(' + i + ')">From here<\/a>';
  var contentString = html;

  google.maps.event.addListener(marker, 'click', function() {
    map.setZoom(15);
    map.setCenter(marker.getPosition());
    infowindow.setContent(contentString);
    infowindow.open(map, marker);
  });
  // save the info we need to use later for the side_bar
  gmarkers.push(marker);
  htmls[i] = html;
}

google.maps.event.addDomListener(window, 'load', initialize);

// ===== request the directions =====
function getDirections() {
  // ==== Set up the walk and avoid highways options ====
  var request = {};
  if (document.getElementById("walk").checked) {
    request.travelMode = google.maps.DirectionsTravelMode.WALKING;
  } else {
    request.travelMode = google.maps.DirectionsTravelMode.DRIVING;
  }

  if (document.getElementById("highways").checked) {
    request.avoidHighways = true;
  }
  // ==== set the start and end locations ====
  var saddr = document.getElementById("saddr").value;
  var daddr = document.getElementById("daddr").value;

  request.origin = saddr;
  request.destination = daddr;
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    } else alert("Directions not found:" + status);
  });
}


// This function picks up the click and opens the corresponding info window
function myclick(i) {
  google.maps.event.trigger(gmarkers[i], "click");
}


// functions that open the directions forms
function tohere(i) {
  //gmarkers[i].openInfoWindowHtml(to_htmls[i]);
  infowindow.setContent(to_htmls[i]);
  infowindow.open(map, gmarkers[i]);
}

function fromhere(i) {
  //gmarkers[i].openInfoWindowHtml(from_htmls[i]);
  infowindow.setContent(from_htmls[i]);
  infowindow.open(map, gmarkers[i]);
}
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>

(请注意,由于安全限制,实际的路线请求在代码片段中不起作用:阻止表单提交到“javascript:getDirections()”,因为表单的框架已沙箱化且“允许表单”权限未设置。,它确实在 fiddle 中工作)

关于javascript - 谷歌地图 API V3 中的 tohere 和 fromhere 消息窗口未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38663612/

相关文章:

javascript - 在谷歌地图中使用 ngClass 与 map 缩放事件没有绑定(bind)

google-maps - 检查地址是否在区域 map 内

javascript - 异步脚本加载

javascript - 当用户离开页面时结束 Seam 中的对话

ios - IB Designables : Failed to render and update auto layout status (Google Maps)

android - 如何在不锁定 UI 线程的情况下快速向 Google Map API v2 中的 MapFragment 添加多个标记?

javascript - 如何解决Google map 上多个标记上的信息框标签重叠问题

javascript - 如何使用 Html5 地理定位链接到 google map 打印方向?

javascript - Kendo-UI 中的反向 Y 轴

javascript - jQuery 函数使整个页面加载两次