2015-04-18 50 views
4

我想使用其PlaceID将标记放置到Google地图上。我有地图工作和显示,也可以添加标记(使用LatLong)。Google地图使用地点ID添加标记

下面的代码是我用来尝试使用它的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); 
+0

你能提供一个[最小,完整,测试和可读的例子](http://stackoverflow.com/help/mcve),表现出这个问题? – geocodezip

+0

哎呦,不小心将这个编辑提交给了这个帖子。请忽略。 – Devnetics

+0

@geocodezip我更新了问题。 – Rafty

回答

7

如果要放置在地图上的标记在与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 
     } 
    }); 
}); 

proof of concept fiddle

代码片段:

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>

+0

我正在为你做一个jsfiddle的中途!但是,谢谢你已经得到了这个标记,所以我应该能够从这里得到其余的。 – Rafty