Google Map Add Marker Using Place Id
I am trying to place a marker into Google Maps using its PlaceID. I have the map working and displaying and can also add markers (using Lattitude and Longitude) into it. The code
Solution 1:
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 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
}
});
});
code snippet:
var map;
var infoWindow;
var service;
functioninitialize() {
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
}
<scriptsrc="https://maps.googleapis.com/maps/api/js?v=3&libraries=places&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script><divid="map-canvas"></div>
Post a Comment for "Google Map Add Marker Using Place Id"