javascript – 如何从手机间隙的纬度和经度获取城市名称?

我可以从当前纬度和经度得到完整的地址.但是如何才能从完整地址获取城市名称.这是我的代码.
var geocoder;
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(latitude,longitude);
//alert("Else loop" + latlng);
geocoder.geocode({
    'latLng': latlng
},function(results,status) {
    //alert("Else loop1");
    if (status == google.maps.GeocoderStatus.OK) {
        if (results[0]) {
            var add = results[0].formatted_address;
            alert("Full address is: " + add);
        } else {
            alert("address not found");
        }
    } else {
        //document.getElementById("location").innerHTML="Geocoder Failed due to: " + status;
        //alert("Geocoder Failed due to: " + status);
    }
});

解决方法

var geocoder;
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(latitude,longitude);

geocoder.geocode(
    {'latLng': latlng},status) {
        if (status == google.maps.GeocoderStatus.OK) {
                if (results[0]) {
                    var add= results[0].formatted_address ;
                    var  value=add.split(",");

                    count=value.length;
                    country=value[count-1];
                    state=value[count-2];
                    city=value[count-3];
                    alert("city name is: " + city);
                }
                else  {
                    alert("address not found");
                }
        }
         else {
            alert("Geocoder Failed due to: " + status);
        }
    }
);

用“,”作为分隔符分割完整地址获取城市名称.

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...