本文介绍了Google地图使用地点ID添加标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图使用PlaceID将标记放置到Google地图上。我有地图工作和显示,也可以添加标记(使用LatLong)。 下面的代码是我用来试图使用它的placeID进行标记显示的,但是它不显示。 function addPlaces(){ var marker = new google.maps.Marker({ place:new google.maps.Place('ChIJN1t_tDeuEmsRUsoyG83frY4') , map:map }); 这个函数在地图加载后调用。 google.maps.event.addDomListener(window,load,addPlaces); 解决方案如果您想在地图上放置标记place_id:'ChIJN1t_tDeuEmsRUsoyG83frY4'的地方,您需要向 getDetails 请求 PlaceService var service = new google.maps.places.PlacesService(map); service.getDetails({ placeId:'ChIJN1t_tDeuEmsRUsoyG83frY4'},function(result,status){ var marker = new google.maps.Marker({ map:map, place:{ placeId:'ChIJN1t_tDeuEmsRUsoyG83frY4', location:result.geometry.location } }); }) ; 概念证明小提琴 程式码片段: var map; var infoWindow; var service; function initialize(){var mapOptions = {zoom:19,center:new google.maps.LatLng(51.257195,3.716563)}; map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions); infoWindow = new google.maps.InfoWindow(); var service = new google.maps.places.PlacesService(map); service.getDetails({placeId:'ChIJN1t_tDeuEmsRUsoyG83frY4'},function(result,status){if(status!= google.maps.places.PlacesServiceStatus.OK){alert(status); return;} var marker = new google.maps .marker({map:map,position:result.geometry.location}); var address = result.adr_address; var newAddr = address.split(< / span>,); infoWindow.setContent(result.name + < br>+ newAddr [0] +< br>+ newAddr [1] +< br>+ newAddr [2]); infoWindow.open(map,marker);});} google.maps.event.addDomListener(window,'load',initialize); html,body,#map-canvas {height:100%;宽度:100%; margin:0px; < script src =https:// I am trying to place a marker onto Google Maps using its PlaceID. I have the map working and displaying and can also add markers (using LatLong) onto it.The code below is what I am using to try and make the marker display using its placeID however it is not displaying.function addPlaces(){ var marker = new google.maps.Marker({ place: new google.maps.Place('ChIJN1t_tDeuEmsRUsoyG83frY4'), map: map });}This function is called after the map has loaded.google.maps.event.addDomListener(window, "load", addPlaces); 解决方案 If you want to place a marker on the map at the place with the place_id: 'ChIJN1t_tDeuEmsRUsoyG83frY4', you need to make a getDetails request to the PlaceServicevar service = new google.maps.places.PlacesService(map);service.getDetails({ placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4'}, function (result, status) { var marker = new google.maps.Marker({ map: map, place: { placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4', location: result.geometry.location } });});proof of concept fiddlecode snippet:var map;var infoWindow;var service;function initialize() { var mapOptions = { zoom: 19, center: new google.maps.LatLng(51.257195, 3.716563) }; map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); infoWindow = new google.maps.InfoWindow(); var service = new google.maps.places.PlacesService(map); service.getDetails({ placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4' }, function(result, status) { if (status != google.maps.places.PlacesServiceStatus.OK) { alert(status); return; } var marker = new google.maps.Marker({ map: map, position: result.geometry.location }); var address = result.adr_address; var newAddr = address.split("</span>,"); infoWindow.setContent(result.name + "<br>" + newAddr[0] + "<br>" + newAddr[1] + "<br>" + newAddr[2]); infoWindow.open(map, marker); });}google.maps.event.addDomListener(window, 'load', initialize);html,body,#map-canvas { height: 100%; width: 100%; margin: 0px; padding: 0px}<script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places"></script><div id="map-canvas"></div> 这篇关于Google地图使用地点ID添加标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-06 05:32