Google Maps SetCenter function refusing to work

心不动则不痛 提交于 2019-12-11 06:15:46

问题


Trying to get my SetCenter Function to work but its not.

Here is the code:

$("#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

    });

the alert correctly returns the following: setting the center to: (40.7143528, -74.0059731)

but its not making the center of the map the correct point...

Any ideas why?


回答1:


Set center takes a google.maps.LatLng object. Change your code to:

var relocate = new google.maps.LatLng(latitude2, longitude2);
alert("setting the center to: " + relocate);
map.setCenter(relocate);

See:

setCenter

LatLng



来源:https://stackoverflow.com/questions/10423979/google-maps-setcenter-function-refusing-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!