【问题标题】:I want to get the lat-long of a city name我想得到一个城市名称的经纬度
【发布时间】:2016-03-15 19:17:19
【问题描述】:

我想将谷歌地图移动到一个城市。用户可以从选择框中选择城市名称。我需要所选城市的经纬度。当用户选择城市时,我必须找到它的经纬度。 我正在使用此代码来移动地图

var center = new google.maps.LatLng(lat,long);
map.panTo(center);

【问题讨论】:

标签: javascript php jquery ajax


【解决方案1】:

我用下面的代码试了一下

var address = city_name;
geocoder.geocode({'address': address},function(results, status){
    if (status === google.maps.GeocoderStatus.OK)
    {
        map.setCenter(results[0].geometry.location);
    }else
    {
        alert('Geocode was not successful for the following reason: ' + status);
    }
});

【讨论】:

    【解决方案2】:

    您必须使用 Google 地理编码器。它的 API 是here。有了它的帮助,您可以像这样度过 lat-long:

    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({
        address: "Your address here"
    }, function(results, status) {
        // here you get your result
    });
    

    【讨论】:

    • 感谢您的回答,我会尝试一下,然后将结果告诉您。
    • 我已经完成了这段代码 var address = city_name; geocoder.geocode({'address': address}, function(results, status) { if (status === google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location); } else { alert('Geocode 不成功,原因如下:' + status); } });
    • 太棒了。如果它帮助您解决问题,您可以接受答案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 2012-06-24
    • 2013-03-03
    • 2013-10-10
    相关资源
    最近更新 更多