-1

我正在为自定义标记网站制作Google地图。这些标记已经制作完成,但是当点击标记时,我需要为每个单独的标记打开信息框。我在互联网上搜索,并尝试了很多东西,但它不会工作。Google地图:如何为自定义标记创建一个infowindow?

下面是代码:

<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> 
 
    <meta charset="utf-8"> 
 
    <style> 
 
     html, body, #map { 
 
     width:700px; 
 
\t \t height:600px; 
 
\t \t border-radius:5px; 
 
\t \t margin-left:auto; 
 
\t \t margin-right:auto; 
 
     } 
 
     #legend { 
 
     font-family: Arial, sans-serif; 
 
     background: #fff; 
 
\t \t opacity:0.85; 
 
     padding: 10px; 
 
     margin: 10px; 
 
     border: 1px solid #000; 
 
\t \t border-radius:5px; 
 
     } 
 
     #legend h3 { 
 
     margin-top: 0; 
 
     } 
 
     #legend img { 
 
     vertical-align: middle; 
 
     } 
 
    </style> 
 
    <script src="https://maps.googleapis.com/maps/api/js"></script> 
 
    <script> 
 
     var map; 
 
     function initialize() { 
 
     map = new google.maps.Map(document.getElementById('map'), { 
 
      zoom: 12, 
 
      center: new google.maps.LatLng(51.99683352, 5.88360088), 
 
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
 
     }); 
 

 
     var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/'; 
 
     var icons = { 
 
\t \t camping: { 
 
      name: 'Camping', 
 
      icon: iconBase + 'campground_maps.png' 
 
      }, 
 
\t \t grocery: { 
 
      name: 'Supermarkt', 
 
      icon: iconBase + 'grocery_maps.png' 
 
     
 
      }, 
 
      dining: { 
 
      name: 'Restaurant', 
 
      icon: iconBase + 'dining_maps.png' 
 
      }, 
 
      snack_bar: { 
 
      name: 'Snackbar', 
 
      icon: iconBase + 'snack_bar_maps.png' 
 
      }, 
 
\t \t swimming: { 
 
      name: 'Zwembad', 
 
      icon: iconBase + 'swimming_maps.png' 
 
      }, 
 
\t \t museum: { 
 
      name: 'Museum', 
 
      icon: iconBase + 'museum_maps.png' 
 
\t \t }, 
 
\t \t parks: { 
 
      name: 'Landgoed', 
 
      icon: iconBase + 'parks_maps.png' 
 
\t \t }, 
 
\t \t hospitals: { 
 
      name: 'Ziekenhuis', 
 
      icon: iconBase + 'hospitals_maps.png' 
 
      } 
 
     }; 
 

 
     function addMarker(feature) { 
 
      var marker = new google.maps.Marker({ 
 
      position: feature.position, 
 
      icon: icons[feature.type].icon, 
 
      map: map 
 
      }); 
 
     } 
 

 
     var features = [ 
 
      { 
 
      position: new google.maps.LatLng(52.0072118, 5.87047509), 
 
      type: 'camping' 
 
      }, { 
 
      position: new google.maps.LatLng(52.00030944, 5.8805877), 
 
      type: 'dining' 
 
      }, { 
 
      position: new google.maps.LatLng(51.99683352, 5.88360088), 
 
      type: 'dining' 
 
      }, { 
 
      position: new google.maps.LatLng(51.962503, 5.90451), 
 
      type: 'swimming' 
 
\t \t }, { 
 
      position: new google.maps.LatLng(51.9956291, 5.9225834), 
 
      type: 'swimming' 
 
\t \t }, { 
 
      position: new google.maps.LatLng(51.98811773, 5.89362323), 
 
      type: 'grocery' 
 
\t \t }, { 
 
      position: new google.maps.LatLng(51.99012513, 5.89376865), 
 
      type: 'grocery' 
 
\t \t }, { 
 
      position: new google.maps.LatLng(52.009949, 5.906641), 
 
      type: 'museum' 
 
\t \t }, { 
 
      position: new google.maps.LatLng(51.988764, 5.901436), 
 
      type: 'museum' 
 
\t \t }, { 
 
      position: new google.maps.LatLng(52.02687639, 5.87179417), 
 
      type: 'museum' 
 
\t \t }, { 
 
      position: new google.maps.LatLng(51.987706, 5.832719), 
 
      type: 'museum' 
 
\t \t }, { 
 
      position: new google.maps.LatLng(52.03369001, 5.86739123), 
 
      type: 'parks' 
 
\t \t }, { 
 
      position: new google.maps.LatLng(51.9911136, 5.8907901), 
 
      type: 'snack_bar' 
 
\t \t }, { 
 
      position: new google.maps.LatLng(52.001069, 5.910066), 
 
      type: 'hospitals' 
 
      } 
 
     ]; 
 

 
     for (var i = 0, feature; feature = features[i]; i++) { 
 
      addMarker(feature); 
 
     } 
 

 
     var legend = document.getElementById('legend'); 
 
     for (var key in icons) { 
 
      var type = icons[key]; 
 
      var name = type.name; 
 
      var icon = type.icon; 
 
      var div = document.createElement('div'); 
 
      div.innerHTML = '<img src="' + icon + '"> ' + name; 
 
      legend.appendChild(div); 
 
     } 
 

 
     map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(legend); 
 
     } 
 

 
     google.maps.event.addDomListener(window, 'load', initialize); 
 
    </script> 
 
    </head> 
 
    <body> 
 
    <div id="map"></div> 
 
    <div id="legend"><h3>Legenda</h3></div> 
 
    </body> 
 
</html>

是否有人有任何建议如何使InfoBoxes到工作?

+0

相关的问题:谷歌地图JS API V3 - 简单多个标记示例](http://stackoverflow.com/questions/3059044/google-maps-js-api-v3-simple-multiple-marker-例如) – geocodezip

+0

在你的问题中没有代码来添加任何infowindows。你尝试过什么(你认为应该起作用),那是行不通的? – geocodezip

回答

0

您需要设置info属性每个标记像这样

marker.info = new google.maps.InfoWindow({ 
    content: 'Add some info here' 
}); 

google.maps.event.addListener(marker, 'click', function() { 
    marker.info.open(map, marker); 
} 

您可以并且应该可能重新SUSE一个信息窗口的所有标记,但是这是超级简单。

运行下面片段来测试它:)

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> 
 
    <meta charset="utf-8"> 
 
    <style> 
 
    html, 
 
    body, 
 
    #map { 
 
     width: 700px; 
 
     height: 600px; 
 
     border-radius: 5px; 
 
     margin-left: auto; 
 
     margin-right: auto; 
 
    } 
 
    #legend { 
 
     font-family: Arial, sans-serif; 
 
     background: #fff; 
 
     opacity: 0.85; 
 
     padding: 10px; 
 
     margin: 10px; 
 
     border: 1px solid #000; 
 
     border-radius: 5px; 
 
    } 
 
    #legend h3 { 
 
     margin-top: 0; 
 
    } 
 
    #legend img { 
 
     vertical-align: middle; 
 
    } 
 
    </style> 
 
    <script src="https://maps.googleapis.com/maps/api/js"></script> 
 
    <script> 
 
    var map; 
 

 
    function initialize() { 
 
     map = new google.maps.Map(document.getElementById('map'), { 
 
     zoom: 12, 
 
     center: new google.maps.LatLng(51.99683352, 5.88360088), 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
 
     }); 
 

 
     var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/'; 
 
     var icons = { 
 
     camping: { 
 
      name: 'Camping', 
 
      icon: iconBase + 'campground_maps.png' 
 
     }, 
 
     grocery: { 
 
      name: 'Supermarkt', 
 
      icon: iconBase + 'grocery_maps.png' 
 

 
     }, 
 
     dining: { 
 
      name: 'Restaurant', 
 
      icon: iconBase + 'dining_maps.png' 
 
     }, 
 
     snack_bar: { 
 
      name: 'Snackbar', 
 
      icon: iconBase + 'snack_bar_maps.png' 
 
     }, 
 
     swimming: { 
 
      name: 'Zwembad', 
 
      icon: iconBase + 'swimming_maps.png' 
 
     }, 
 
     museum: { 
 
      name: 'Museum', 
 
      icon: iconBase + 'museum_maps.png' 
 
     }, 
 
     parks: { 
 
      name: 'Landgoed', 
 
      icon: iconBase + 'parks_maps.png' 
 
     }, 
 
     hospitals: { 
 
      name: 'Ziekenhuis', 
 
      icon: iconBase + 'hospitals_maps.png' 
 
     } 
 
     }; 
 

 
     function addMarker(feature) { 
 
     var marker = new google.maps.Marker({ 
 
      position: feature.position, 
 
      icon: icons[feature.type].icon, 
 
      map: map 
 
     }); 
 
     marker.info = new google.maps.InfoWindow({ 
 
      content: 'Add some info here' 
 
     }); 
 

 
     google.maps.event.addListener(marker, 'click', function() { 
 
      marker.info.open(map, marker); 
 
     }); 
 
     } 
 

 
     var features = [{ 
 
     position: new google.maps.LatLng(52.0072118, 5.87047509), 
 
     type: 'camping' 
 
     }, { 
 
     position: new google.maps.LatLng(52.00030944, 5.8805877), 
 
     type: 'dining' 
 
     }, { 
 
     position: new google.maps.LatLng(51.99683352, 5.88360088), 
 
     type: 'dining' 
 
     }, { 
 
     position: new google.maps.LatLng(51.962503, 5.90451), 
 
     type: 'swimming' 
 
     }, { 
 
     position: new google.maps.LatLng(51.9956291, 5.9225834), 
 
     type: 'swimming' 
 
     }, { 
 
     position: new google.maps.LatLng(51.98811773, 5.89362323), 
 
     type: 'grocery' 
 
     }, { 
 
     position: new google.maps.LatLng(51.99012513, 5.89376865), 
 
     type: 'grocery' 
 
     }, { 
 
     position: new google.maps.LatLng(52.009949, 5.906641), 
 
     type: 'museum' 
 
     }, { 
 
     position: new google.maps.LatLng(51.988764, 5.901436), 
 
     type: 'museum' 
 
     }, { 
 
     position: new google.maps.LatLng(52.02687639, 5.87179417), 
 
     type: 'museum' 
 
     }, { 
 
     position: new google.maps.LatLng(51.987706, 5.832719), 
 
     type: 'museum' 
 
     }, { 
 
     position: new google.maps.LatLng(52.03369001, 5.86739123), 
 
     type: 'parks' 
 
     }, { 
 
     position: new google.maps.LatLng(51.9911136, 5.8907901), 
 
     type: 'snack_bar' 
 
     }, { 
 
     position: new google.maps.LatLng(52.001069, 5.910066), 
 
     type: 'hospitals' 
 
     }]; 
 

 
     for (var i = 0, feature; feature = features[i]; i++) { 
 
     addMarker(feature); 
 
     } 
 

 
     var legend = document.getElementById('legend'); 
 
     for (var key in icons) { 
 
     var type = icons[key]; 
 
     var name = type.name; 
 
     var icon = type.icon; 
 
     var div = document.createElement('div'); 
 
     div.innerHTML = '<img src="' + icon + '"> ' + name; 
 
     legend.appendChild(div); 
 
     } 
 

 
     map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(legend); 
 
    } 
 

 
    google.maps.event.addDomListener(window, 'load', initialize); 
 
    </script> 
 
</head> 
 

 
<body> 
 
    <div id="map"></div> 
 
    <div id="legend"> 
 
    <h3>Legenda</h3> 
 
    </div> 
 
</body> 
 

 
</html>

0

看看这个简单的解决方案。我试过这个,它工作正常。相应地修改你的代码。

for (i = 0; i < latitude_longitude_array.length; i++) {  
     var data = latitude_longitude_array[i]; 
     var myLatlng = new google.maps.LatLng(data.latitude, data.longitude); 

     var marker = new google.maps.Marker({ 
       position: myLatlng, 
       map: map, 
     }); 

     var geocoder = new google.maps.Geocoder(); 
      (function (marker, data) { 
       geocoder.geocode(
           { 
            'latLng': myLatlng 
           }, 
           function (results, status) { 

            if (status == google.maps.GeocoderStatus.OK) { 
             if (results[1]) { 
              var infoWindow = new google.maps.InfoWindow();  

              infoWindow.setContent("Address" + ": " + results[1].formatted_address + " ");  

              google.maps.event.addListener(marker, "click", function (e) { 
               infoWindow.open(map, marker); 
              }); 
             } 
             else { 
              alert("No results found"); 
             } 
            } 
            else { 
             alert("Geocoder failed due to: " + status); 
            } 
           }); 
      })(marker, data); 
    } //end of for 
相关问题