首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >带样式的地图和地址库Google Map API v3

带样式的地图和地址库Google Map API v3
EN

Stack Overflow用户
提问于 2012-07-14 02:18:17
回答 1查看 521关注 0票数 0

如何从Google Places库中获取结果以显示在我的带样式的Google Maps上?有了这段代码,我只得到了带样式的地图,没有地点。

代码语言:javascript
运行
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-14 03:27:29

您必须将变量map设置为全局变量,并将map赋值给createMarker()中标记的地图选项

当前您使用的是styledMap,但这不是一个地图对象,而是一个StyledMapType

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11476096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档