最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - reverse geocoding - returning only towncity or country - Stack Overflow

programmeradmin1浏览0评论

I have a google map which has a marker on it so people can move it around. When they do I'm attempting to reverse geocode the location into a proper address but I only really want the town/city and the country, I don't want the postcode returned

Is it possible to just get the locality without having to use a regex to remove the postcode - which would prob be difficult!

Thanks in advance

I have a google map which has a marker on it so people can move it around. When they do I'm attempting to reverse geocode the location into a proper address but I only really want the town/city and the country, I don't want the postcode returned

Is it possible to just get the locality without having to use a regex to remove the postcode - which would prob be difficult!

Thanks in advance

Share Improve this question asked Feb 19, 2014 at 1:02 crazy sarahcrazy sarah 6314 gold badges13 silver badges29 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

The response didn't only return the address, it also contains the address_ponents, an array with the specific details of the location, e.g. country, city, street etc. (see https://developers.google./maps/documentation/geocoding/#JSON)

fetch the desired ponents out of this array.

Reverse GeoCoding returns an address_ponents array containing few objects. (You can print this object in console to get the feel.) Extracting required information from this array is very easy. Now have a look at the code -

function getLatLong(position) {
  geocoder = new google.maps.Geocoder();
  var latitude = position.coords.latitude;
  var longitude = position.coords.longitude;

  // Reverse Geocoding, Location name from co-ordinates.
  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 ponents=results[0].address_ponents;

        for (var ponent=0;ponent<(ponents.length);ponent++){
            if(ponents[ponent].types[0]=="administrative_area_level_1"){
                var admin_area=ponents[ponent].long_name;
            }
            if(ponents[ponent].types[0]=="country"){
                var country=ponents[ponent].long_name;
            }

            if(ponents[ponent].types[0]=="postal_code"){
                var postal_code=ponents[ponent].long_name;
            }
        }
      }
    }
  }
}

I think you can!

if (status == google.maps.GeocoderStatus.OK) {
                if (results[0]) {
                    for (var i = 0; i < results.length; i++) {
                        if (results[i].types[0] === "locality") {
                            var city = results[i].address_ponents[0].short_name;
                            var state = results[i].address_ponents[2].short_name;
                            alert('Serial=' + i+ ' city=' + city+ ' state=' + state)
                        };
                    };
                };
            };
发布评论

评论列表(0)

  1. 暂无评论