admin管理员组文章数量:1416651
How do you center a google map based on a placeID, without setting the lat and lng explicitly in the map object?
All examples I've seen have a value set e.g.
var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316);
Form: developers.google
Surely you can getDetails on a place based on an ID, then use the lat and lng to center the map?
function initializeGoogleMaps() {
jQuery('.site-content').prepend('<div id=\"map-container\"><div id=\"map-canvas\"></div></div>');
var request = {
placeId: 'ChIJzXBGLkQFdkgRoyT2MIxgWGw'
};
function callback(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
}
}
var map = new google.maps.Map(document.getElementById('map-canvas'), {
center: {
lat: 51.509526,
lng: -0.156314
},
zoom: 15
});
service = new google.maps.places.PlacesService(map);
service.getDetails(request, callback);
}
How do you center a google map based on a placeID, without setting the lat and lng explicitly in the map object?
All examples I've seen have a value set e.g.
var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316);
Form: developers.google.
Surely you can getDetails on a place based on an ID, then use the lat and lng to center the map?
function initializeGoogleMaps() {
jQuery('.site-content').prepend('<div id=\"map-container\"><div id=\"map-canvas\"></div></div>');
var request = {
placeId: 'ChIJzXBGLkQFdkgRoyT2MIxgWGw'
};
function callback(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
}
}
var map = new google.maps.Map(document.getElementById('map-canvas'), {
center: {
lat: 51.509526,
lng: -0.156314
},
zoom: 15
});
service = new google.maps.places.PlacesService(map);
service.getDetails(request, callback);
}
Share
Improve this question
edited Oct 6, 2015 at 16:17
jonny-harte
asked Oct 6, 2015 at 15:48
jonny-hartejonny-harte
3391 gold badge7 silver badges16 bronze badges
0
1 Answer
Reset to default 6You can set the center of the map using the value returned from the places service (or the position of the marker):
proof of concept fiddle
function callback(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
map.setCenter(marker.getPosition());
}
}
var map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 15
});
code snippet:
function initializeGoogleMaps() {
var request = {
placeId: 'ChIJzXBGLkQFdkgRoyT2MIxgWGw'
};
function callback(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
map.setCenter(marker.getPosition());
}
}
var map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 15
});
service = new google.maps.places.PlacesService(map);
service.getDetails(request, callback);
}
google.maps.event.addDomListener(window, 'load', initializeGoogleMaps);
html,
body,
#map-canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis./maps/api/js?libraries=places"></script>
<div id="map-canvas"></div>
本文标签: javascriptCenter google Map on place ID without specifying lat and longStack Overflow
版权声明:本文标题:javascript - Center google Map on place ID without specifying lat and long? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745252563a2649909.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论