2015-04-18 32 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