0

如何从 Google 地方信息库中获取结果以显示在我的风格化 Google 地图上?使用此代码,我只得到样式地图,没有地方。

<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>
4

1 回答 1

0

您必须将变量设为map全局变量并分配 map给标记的地图选项createMarker()

目前你在那里使用styledMap,但这不是地图对象,它是 StyledMapType

于 2012-07-13T19:27:29.437 回答