【问题标题】:Given latitude and longitude, get name of Location in Sencha Touch给定纬度和经度,在 Sencha Touch 中获取位置名称
【发布时间】:2013-07-06 14:41:02
【问题描述】:

我正在尝试从给定的纬度和经度中获取位置的名称。 有没有可能得到它像

getNameOfLocation(经度、纬度)?

例如 getNameOfLocation(48.1471904942317, 11.591434478759765) --> 结果慕尼黑

我不想像谷歌地图那样使用任何图形元素来显示。

有什么想法吗? 感谢您的帮助

var geo = Ext.create('Ext.util.Geolocation', {
    autoUpdate: false,
    listeners: {
        locationupdate: function(geo) {
            alert('New latitude: ' + geo.getLatitude());
        },
        locationerror: function(geo, bTimeout, bPermissionDenied, bLocationUnavailable, message) {
            if(bTimeout){
                alert('Timeout occurred.');
            } else {
                alert('Error occurred.');
            }
        }
    }
});

geo.updateLocation();

【问题讨论】:

    标签: geolocation sencha-touch latitude-longitude


    【解决方案1】:

    试试这个..

    function getNameOfLocation(lat, lng) {
        var geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(lat, lng);
        geocoder.geocode({
            'latLng': latlng
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
    alert(results[0].formatted_address) // your result              
     //country name
                    for (var i=0; i<results[0].address_components.length; i++) {
                        for (var b=0;b<results[0].address_components[i].types.length;b++) {
    
                            if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
                                city= results[0].address_components[i];
                                break;
                            }
                        }
                    }
    
    
                } else {
                    alert("No result");
                }
            } else {
                alert("failed: " + status);
            }
        });
    }
    

    【讨论】:

    • 我需要一个 API 密钥吗?
    • 是的,对于 google api,您有一个 api 密钥...首先转到 code.google.com/apis/console 点击服务并在 Google Maps API v3 上滑动,然后转到 api 访问并创建您的应用程序以生成您的api 键。
    • 因为您不需要 APi Key,所以有可能没有它:stackoverflow.com/a/12615275/2436861
    【解决方案2】:

    您可以向谷歌地图 API 发送 Ajax 请求。例如:

    http://maps.googleapis.com/maps/api/geocode/json?latlng=48.1471904942317,11.591434478759765&sensor=false

    然后解析 JSON 结果。

    【讨论】:

    • 原生应用也能做到这一点吗?不是基于浏览器的网络应用程序
    • 原生应用是什么意思?你用的是 ExtJS,对吧?
    • 我的应用不是基于网络的。它在我的 android 设备上本地运行,就像来自 Play 商店的任何本机应用程序一样。我使用 Sencha Touch 并将其打包到我的设备上直到现在我不需要 JSON/Ajax 等。有没有可能没有?
    • 如果您的应用程序是使用 sencha touch 开发的 - 它是基于 Web 的,并且如果您有互联网连接,则可以发出 ajax 请求。如果没有请求,您将无法检索有关该位置的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 2012-02-16
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    相关资源
    最近更新 更多