Google Maps API,获取用户城市/最近的城市/一般区域 [英] Google maps API, get the users city/ nearest city/ general area

查看:211
本文介绍了Google Maps API,获取用户城市/最近的城市/一般区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试编写一个函数,以返回一个人所处或附近的最合理,大小适中的城市/城镇/地区.

I've been trying to code a function that returns the most reasonable, moderately sized city/ town/ region a person is in or near.

yik yak之类的应用程序使用了这样的算法将人们分组在一起.是否有任何现有的alogorithims可以做到这一点?目前,我正在使用这个:

Apps such as yik yak used an algorithim like this to group people together. Is there any existing alogorithims to do this? At the moment I'm using this:

 geocoder.geocode({'latLng': latlng}, function (results, status) {

        var city = 'Unknown';
        if (status == google.maps.GeocoderStatus.OK) {

            var details = results[0].address_components;

            for (var i = details.length-1; i >= 0; i--) {
                for (var j=0; j<details[i].types.length;j++) {

                    if (details[i].types[j] == 'locality') {
                        city = details[i].long_name;
                    } else if (details[i].types[j] == 'sublocality') {
                        city = details[i].long_name;
                    } else if (details[i].types[j] == 'neighborhood') {
                        city = details[i].long_name;
                    }
                }
            }

哪个不是最好的.我刚刚尝试输入以下坐标:{纬度:53.4808,经度:-2.7426}

Which isn't the best. I've just tried to enter these coords: {latitude: 53.4808, longitude: -2.7426}

不为人知.在查看地理编码器返回的结果时,我看到Saint Helens将是放置此用户的最合理规模的组.如果不存在,则返回Merseysid,这将是下一个最好的选择.

And got unknown. When looking as the results the geocoder returned, I see Saint Helens would be the most reasonably sized group to put this user in. If that didn't exist, I see Merseysid is also returned which would be the next best thing.

推荐答案

在我看来,圣海伦斯和梅西赛德都是来自反向地理编码器的结果.

It looks to me like both Saint Helens and Mersyside are in the result from the Reverse Geocoder.

反向地理编码器结果:

[
  {"long_name":"Unnamed Road","short_name":"Unnamed Road","types":["route"]},
  {"long_name":"Saint Helens","short_name":"Saint Helens","types":["postal_town"]},
  {"long_name":"Merseyside","short_name":"Merseyside","types":["administrative_area_level_2","political"]},
  {"long_name":"England","short_name":"England","types":["administrative_area_level_1","political"]},
  {"long_name":"United Kingdom","short_name":"GB","types":["country","political"]},
  {"long_name":"WA11","short_name":"WA11","types":["postal_code","postal_code_prefix"]}
]

如果将这些类型添加到if语句中,它将出现在结果中:

If you add those types to your if statement, the it will appear in the result:

  for (var i = details.length-1; i >= 0; i--) {
    for (var j=0; j<details[i].types.length;j++) {
      if (details[i].types[j] == 'locality') {
        city = details[i].long_name;
      } else if (details[i].types[j] == 'sublocality') {
        city = details[i].long_name;
      } else if (details[i].types[j] == 'neighborhood') {
        city = details[i].long_name;
      } else if (details[i].types[j] == 'postal_town') {
        city = details[i].long_name;
      } else if (details[i].types[j] == 'administrative_area_level_2') {
        city = details[i].long_name;
      }
    }
  }
  console.log("city="+city);
}

概念提琴证明

代码段:

var geocoder;
var map;

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(53.4808, -2.7426),
      zoom: 12,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var marker = new google.maps.Marker({
    map: map,
    position: map.getCenter(),
  });
  geocoder = new google.maps.Geocoder();
  geocoder.geocode({
    'latLng': new google.maps.LatLng(53.4808, -2.7426)
  }, function(results, status) {

    var city = 'Unknown';
    if (status == google.maps.GeocoderStatus.OK) {

      var details = results[0].address_components;
      console.log(JSON.stringify(details));
      for (var i = details.length - 1; i >= 0; i--) {
        for (var j = 0; j < details[i].types.length; j++) {
          if (details[i].types[j] == 'locality') {
            city = details[i].long_name;
          } else if (details[i].types[j] == 'sublocality') {
            city = details[i].long_name;
          } else if (details[i].types[j] == 'neighborhood') {
            city = details[i].long_name;
          } else if (details[i].types[j] == 'postal_town') {
            city = details[i].long_name;
            console.log("postal_town=" + city);
          } else if (details[i].types[j] == 'administrative_area_level_2') {
            city = details[i].long_name;
            console.log("admin_area_2=" + city);
          }
        }
      }
      console.log("city=" + city);
      document.getElementById('city').innerHTML = "city=" + city;
    }
  })
}
google.maps.event.addDomListener(window, "load", initialize);

html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}

<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="city"></div>
<div id="map_canvas"></div>

这篇关于Google Maps API,获取用户城市/最近的城市/一般区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆