Ben*_*ter 1 maps google-maps google-maps-api-3
试图让我的SetCenter功能工作,但事实并非如此.
这是代码:
$("#searchclick").click(function(){
var address = document.getElementById('searchbar').value;
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude2 = results[0].geometry.location.lat();
var longitude2 = results[0].geometry.location.lng();
var relocate = ("(" + latitude2 + ", " + longitude2 + ")");
alert("setting the center to: " + relocate);
map.setCenter(relocate);
} //when status is OK
}); // geocode
});
Run Code Online (Sandbox Code Playgroud)
警报正确返回以下内容: setting the center to: (40.7143528, -74.0059731)
但它没有使地图的中心正确点......
有什么想法吗?
设置中心需要一个google.maps.LatLng对象.将您的代码更改为:
var relocate = new google.maps.LatLng(latitude2, longitude2);
alert("setting the center to: " + relocate);
map.setCenter(relocate);
Run Code Online (Sandbox Code Playgroud)
看到: