2017-07-03 28 views
0

我有问题在m谷歌地图动态地图内的标记上实施地址可视化。添加有关谷歌地图地址的信息

我的代码如下:

<!DOCTYPE html> 
<html> 
    <head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <title>Marker Clustering</title> 
    <style> 
     /* Always set the map height explicitly to define the size of the div 
     * element that contains the map. */ 
     #map { 
     height: 100%; 
     } 
     /* Optional: Makes the sample page fill the window. */ 
     html, body { 
     height: 100%; 
     margin: 0; 
     padding: 0; 
     } 
    </style> 
    </head> 
    <body> 
    <div id="map"></div> 
    <script> 

     function initMap() { 

     var map = new google.maps.Map(document.getElementById('map'), { 
      zoom: 5, 
      center: {lat: 44.0, lng: 12.0} 
     }); 

     // Create an array of alphabetical characters used to label the markers. 
     var labels = ''; 

     // Add some markers to the map. 
     // Note: The code uses the JavaScript Array.prototype.map() method to 
     // create an array of markers based on a given "locations" array. 
     // The map() method here has nothing to do with the Google Maps API. 
     var markers = locations.map(function(location, i) { 
      return new google.maps.Marker({ 
      position: location, 
      label: labels[i % labels.length] 
      }); 
     }); 
     // Add a marker clusterer to manage the markers. 
     var markerCluster = new MarkerClusterer(map, markers, 
      {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'}); 
     } 

     var locations = [ 
     {lat: 44.5153, lng: 11.3428}, 
{lat: 44.4926, lng: 11.3657}, 
{lat: 44.4948, lng: 11.3158} 
//input geocode continue 
     ] 
    </script> 
    <script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"> 
    </script> 
    <script async defer 
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBu-916DdpKAjTmJNIgngS6HL_kDIKU0aU&callback=initMap"> 
    </script> 
    </body> 
</html> 

我想对每个标记点击弹出信息添加,但我不能设法做到这一点,我已经搜查了很多,但I'ven't找到任何工作方案。

有人可以帮助我吗?

+0

我需要的信息主要是这样的: 地址公民号码,城市 – RevivedPicard

+0

尝试使用信息窗口和谷歌地方的API。 – KingCoder11

+0

我已经尝试建议在这里的其他职位上,但无法设法得到它的工作...提供我的代码的功能示例请 – RevivedPicard

回答

-2

我已经编辑你的代码,让它与信息窗口工作

之前,初始化标记添加此行代码

var infoWin = new google.maps.InfoWindow(); 

这将初始化信息窗口,现在修改标记初始化这个

var markers = locations.map(function(location, i) { 
      var marker = new google.maps.Marker({ 
       position: location, 
       label: labels[i % labels.length] 
      }); 

      //// here we are adding the event listner for the markers to open the info window on click 
      google.maps.event.addListener(marker, 'click', function(evt) { 
       infoWin.setContent(location.info); 
       infoWin.open(map, marker); 
      }) 
      return marker; 
     }); 

通知我们正在阅读从位置阵列的信息,从而改变你的位置阵列包括第三参数“信息”这样

var locations = [{ 
      lat: 44.5153, 
      lng: 11.3428, 
      info: 'place 1' 
     }, { 
      lat: 44.4926, 
      lng: 11.3657, 
      info: 'place2' 
     }, { 
      lat: 44.4948, 
      lng: 11.3158, 
      info: 'place 3' 
     } 
     //input geocode continue 
    ] 

希望这会有所帮助,它的工作将与我,请删除下投票,因为我已经编辑我的答案

+0

您的答案无法描述_why_。简单地给某人编码,并说“你走到这里”并不能帮助那个人清楚他们做错了什么,它不会教他们任何东西,也很可能不会帮助未来的读者。 – GrumpyCrouton

+0

谢谢,但它并没有帮助...它修改了我的初始代码太多,我需要在我发布的代码上实现信息窗口 – RevivedPicard

+0

我;我编辑了我的上面的代码,请检查它 –