Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get city name from latitude and longitude in phone gap?

I am able to get get full address from current latitude and longitude. but how can I get only city name from full address. this is my code.

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);
    }
});
like image 395
Vinod Avatar asked Mar 28 '14 05:03

Vinod


1 Answers

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

geocoder.geocode(
    {'latLng': latlng}, 
    function(results, 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);
        }
    }
);

Split the full address with "," as delimiter and get the city name..

like image 116
Vinod Avatar answered Oct 18 '22 03:10

Vinod