谷歌地图API,获取用户城市/最近的城市/一般区域

时间:2018-04-28 21:30:45

标签: google-maps google-maps-api-3

我一直在尝试编写一个函数来返回一个人所在或靠近的最合理,中等规模的城市/城镇/地区。

yik yak等应用程序使用这样的算法将人们聚集在一起。有没有现成的alogorithims这样做?目前我正在使用它:

 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}

并且未知。当看到地理编码器返回的结果时,我看到圣海伦斯将成为最合理规模的群体。如果不存在,我会看到Merseysid也会被退回,这将是下一个最好的事情。

1 个答案:

答案 0 :(得分:1)

在我看来,圣海伦斯和默西塞德都是逆向地理编码器的结果。

反向地理编码器结果:

function sum(root, path) {
  if(!path) {
    path = 'root';
  }
  console.log(path);
  if(root === null) return 0;
  return root.val + sum(root.left, path+'.left') + sum(root.right, path+'.right'); 
}

如果您将这些类型添加到[ {"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

proof of concept fiddle

代码段

&#13;
&#13;
  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);
}
&#13;
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);
&#13;
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
&#13;
&#13;
&#13;