样式化地图和地点库Google Map API v3

styled maps and places library Google Map API v3

本文关键字:Google Map API v3 地图 样式      更新时间:2023-09-26

如何从Google Places库中获得结果,以显示在我的风格谷歌地图上?有了这段代码,我只得到了样式化的地图,没有位置。

<script type="text/javascript"
  src="http://maps.googleapis.com/maps/api/js?key=mykey&sensor=true">
</script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=true"></script>  

<script type="text/javascript">

 function initialize() {
var styles = [ { featureType: "road.arterial", elementType: "geometry", stylers: [ {   color: "#808080" }, { weight: 0.5 } ] },{ featureType: "road.arterial", elementType: "labels.text.stroke", stylers: [ { visibility: "on" }, { color: "#fafafa" } ] },{ featureType: "water", elementType: "geometry", stylers: [ { color: "#b4d2fa" } ] },{ featureType: "water", elementType: "geometry.fill", stylers: [ { color: "#9c3239" } ] } ]
var styledMap = new google.maps.StyledMapType(styles,
{name: "WATS Map"});
var myLatLng = new google.maps.LatLng(42.3068, -83.681)
var mapOptions = {
zoom: 12,
center: myLatLng,
mapTypeControlOptions: {
  mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style']
}
};
var map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);

map.mapTypes.set('map_style', styledMap);
map.setMapTypeId('map_style');

var request = {
      location: myLatLng,
      radius: 1500,
      types: ['store']
    };
    var infowindow = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(map);
    service.search(request, callback);
  }
  function callback(results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
      for (var i = 0; i < results.length; i++) {
        createMarker(results[i]);
      }
    }
  }
  function createMarker(place) {
    var placeLoc = place.geometry.location;
    var marker = new google.maps.Marker({
      map: styledMap,
      position: place.geometry.location
    });
    google.maps.event.addListener(marker, 'click', function() {
      infowindow.setContent(place.name);
      infowindow.open(styledMap, this);
    });
  }
</script>

您必须将变量map设置为全局变量,并将map分配给createMarker() 中标记的映射选项

目前您使用的是styledMap,但这不是一个映射对象,它是一个StyledMapType