【问题标题】:Google Maps API - using specific distance radius to display addressesGoogle Maps API - 使用特定的距离半径来显示地址
【发布时间】:2017-08-30 09:35:04
【问题描述】:

我希望创建一个函数,允许我使用 API 搜索地址。然后从该地址希望能够设置该位置的 50 英里半径,并能够在我们的数据库中查看 50 英里半径内的其他地址。 50 英里的半径可以改变,但这只是举例。

【问题讨论】:

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


    【解决方案1】:

    这应该对您有所帮助。如果更适合您,您可以从 Google 地图 API 或其他地方获取经度和纬度。然后用这个方法计算距离。

    http://www.geodatasource.com/developers/c-sharp

    【讨论】:

      【解决方案2】:

      你可以试试Places API

      Google Places API 网络服务允许您查询各种类别的地点信息,例如:场所、著名景点、地理位置等。您可以通过邻近度或文本字符串来搜索地点。地点搜索会返回地点列表以及每个地点的摘要信息;更多信息可通过Place Details 查询获得。

      正如此相关 SO post 中所建议的,Places API 响应采用 JSON 格式。社区向我们推荐 json2csharp 以轻松生成 C# 模型以响应 Google Places 查询。然后使用JSON.NET对查询结果进行反序列化。

      这是一个示例代码查询:

         using (var client = new HttpClient())
          {
              var response = await client.GetStringAsync(string.Format("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={0},{1}&radius=500&type=bar&key=YourAPIKey", latitude, longitude));
              var result = JsonConvert.DeserializeObject<PlacesApiQueryResponse>(response);
          }
      

      然后按照代码实现如何检查搜索地址与数据库中其他地址的距离。他们使用 computeDistanceBetween 返回两个 LatLng 之间的距离(以米为单位)。

      var marker_lat_lng = new google.maps.LatLng(location.lat, location.lng);
                                  var distance_from_location = google.maps.geometry.spherical.computeDistanceBetween(address_lat_lng, marker_lat_lng); //distance in meters between your location and the marker
                                  if (distance_from_location <= radius_km * 1000) {
                                      var new_marker = new google.maps.Marker({
                                          position: marker_lat_lng,
                                          map: map,
                                          title: location.name
                                      });  
      

      【讨论】:

      • 谢谢!我会试试这个
      猜你喜欢
      • 1970-01-01
      • 2014-10-21
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      • 2014-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多