Google Maps API-Radius是否使用Places搜索标记? [英] Google Maps API - Radius search for markers using Places?

查看:69
本文介绍了Google Maps API-Radius是否使用Places搜索标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用API​​ v3设置了Google Map.地图上有许多带有附加信息框的标记.我希望在地图外设置搜索框,以便用户输入地址,然后根据距离返回最近的标记(例如半径搜索).

I have set up a Google Map using API v3. The map has a number of markers with infoboxes attached. I am looking to set up a search box outside of the map for the user to input an address and then have the nearest markers returned based on the distance away (such as a radius search).

从API文档中,我认为我需要使用Places服务.谁能指出我正确的方向?

From the API documentation I think I need to uses the Places services. Can anyone point me in the right direction?

推荐答案

要使用API​​进行半径搜索,请使用 google.maps.geometry. spheric.computeDistanceBetween 方法来计算每个标记与该地址的地理编码结果之间的距离.如果该距离小于要求的半径,请显示标记,否则将其隐藏.

To do a radius search with the API, use the Geometry Library google.maps.geometry.spherical.computeDistanceBetween method to calculate the distance between each marker and the geocoded result from the address. If that distance is less than the requested radius, show the marker, else hide it.

代码假定:

  1. 称为gmarkers的google.maps.Markers数组
  2. google.maps.Map对象,称为地图

  1. array of google.maps.Markers called gmarkers
  2. google.maps.Map object called map

function codeAddress() {
  var address = document.getElementById('address').value;
  var radius = parseInt(document.getElementById('radius').value, 10)*1000;
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location
      });
      if (circle) circle.setMap(null);
      circle = new google.maps.Circle({center:marker.getPosition(),
                                     radius: radius,
                                     fillOpacity: 0.35,
                                     fillColor: "#FF0000",
                                     map: map});
      var bounds = new google.maps.LatLngBounds();
      for (var i=0; i<gmarkers.length;i++) {
        if (google.maps.geometry.spherical.computeDistanceBetween(gmarkers[i].getPosition(),marker.getPosition()) < radius) {
          bounds.extend(gmarkers[i].getPosition())
          gmarkers[i].setMap(map);
        } else {
          gmarkers[i].setMap(null);
        }
      }
      map.fitBounds(bounds);

    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}

示例

这篇关于Google Maps API-Radius是否使用Places搜索标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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