我怎么找到附近的位置使用谷歌地图API给定的位置 [英] how do i find near by locations to a given location using google map api

查看:101
本文介绍了我怎么找到附近的位置使用谷歌地图API给定的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想找到给定地点或地址的所有附近地点。有没有谷歌地图API?或者我们需要按照一系列步骤进行操作,例如


  1. 查找该地点的经纬度。

  2. >
  3. 进行一些处理并以近似long long来获得。

  4. 用步骤2并找到每个经纬度长对的位置。


澄清我。此外,如果你可以推广第2步,这将是伟大的。



这种方法可行吗?您可以澄清这一点。

解决方案

这真的取决于您正在寻找的附近的东西是否在Google数据库中如果他们是地址/位置,则类似于您指定的内容是正确的。


  1. 将您想要包含的所有地点进行地理编码并将它们存储在数据库中(使用纬度/经度)

  2. 对您所在的地点进行地理编码在(您需要信息的地方)

  3. 在球体上使用距离数学例程列出指定距离内的地点

数学是:


  1. 获取Radians当前的纬度和经度(LatOrLon * PI / 180)

  2. 获取Radians中的目标纬度和经度(LatOrLon * PI / 180)获得球体的半径(在这种情况下,earth = 3963.1里程)

  3. 使用复杂而复杂的forumla

下面(其中Lat1和Lon1是Radians中的当前位置,Lat2和Lon2是Radians中的目标):


$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $($)$ Cos $($)$ Cos(Lat1)* Cos(Lon1)* Cos(Lat2)* Cos(Lon2)+
Cos( Lat1)* Sin(Lon1)* Cos(Lat2)* Sin(Lon2)+
Sin(Lat1)* Sin(Lat2)
)* EarthRadiusInMiles

可以使用SQL函数(MSSQL,但您可以轻松将此逻辑编码为大多数其他语言)来完成此操作。

  CREATE FUNCTION [dbo]。[CoordinateDistanceMiles](
@ Latitude1 float,
@ Longitude1 float,
@ Latitude2 float,
@ Longitude2 float

RETURNS float
AS
BEGIN

- SAME LOCATION,RETURN 0
IF @ Latitude1 = @ Latitude2 AND @ Longitude1 = @ Longitude2 RETURN 0.00

- CONSTANTS
DECLARE @EarthRadiusInMiles float;
SET @EarthRadiusInMiles = 3963.1
DECLARE @PI float;
SET @PI = PI();

- RADIANS转换
DECLARE @ lat1Radians float;
DECLARE @ long1Radians float;
DECLARE @ lat2Radians float;
DECLARE @ long2Radians float;

SET @ lat1Radians = @ Latitude1 * @PI / 180;
SET @ long1Radians = @ Longitude1 * @PI / 180;
SET @ lat2Radians = @ Latitude2 * @PI / 180;
SET @ long2Radians = @ Longitude2 * @PI / 180;

返回Acos(
Cos(@ lat1Radians)* Cos(@long1Radians)* Cos(@ lat2Radians)* Cos(@long2Radians)+
Cos(@ lat1Radians)* Sin (@ long1Radians)* Cos(@ lat2Radians)* Sin(@ long2Radians)+
Sin(@ lat1Radians)* Sin(@ lat2Radians)
)* @EarthRadiusInMiles;
END

然后从任何调用查询中,您可以按距离排序,例如:

  CREATE PROCEDURE [dbo]。[NearbyThings] 

@Lat float,
@ Lon float,
@Num int = 10


AS

SET NOCOUNT ON

选择顶部(@Num )
'距离'=圆形(dbo.CoordinateDistanceMiles(@Lat,@Lon,Lat,Lon),1),
ThingId,
邮编,
Lat,
Lon
From
物品
订单
dbo.CoordinateDistanceMiles(@Lat,@Lon,Lat,Lon)ASC
GO


would like to find all the nearby places for a given place or address. Is there any google map api ? or we need to follow a sequence of steps like

  1. Find the lat long of the place.

  2. Do some processing and get near by lat longs.

  3. do reverse geo coding with the obtained lat longs from step 2 and find the places for each lat long pairs.

clarify me. Also if you can elobrate step 2 that would be great.

Will this approach works? can you clarify on this.

解决方案

It really depends if the Nearby things you are looking for are in the Google database as places of interest, or if they are arbitrary addresses / locations.

If they are addresses / locations, something similar to what you specify is correct.

  1. Geocode all the places you want to include and store them in a DB (with Lat/Lon)
  2. Geocode the place you are at (where you want information for)
  3. Use a maths routine for distance on a sphere to list the places within a specified distance

The Maths is:

  1. Get the current latitude and longitude in Radians (LatOrLon * PI / 180)
  2. Get the target latitude and longitude in Radians (LatOrLon * PI / 180)
  3. Get the Radius of your sphere (in this case earth = 3963.1 Miles)
  4. Use the long complex and confusing forumla

Below (where Lat1 and Lon1 are your current location in Radians and Lat2 and Lon2 are your target in Radians):

Acos(
    Cos(Lat1) * Cos(Lon1) * Cos(Lat2) * Cos(Lon2) + 
    Cos(Lat1) * Sin(Lon1) * Cos(Lat2) * Sin(Lon2) + 
    Sin(Lat1) * Sin(Lat2)
    ) * EarthRadiusInMiles

This can be done using an SQL Function (MSSQL, but you can easily code this logic into most other languages).

CREATE FUNCTION [dbo].[CoordinateDistanceMiles](
@Latitude1 float,
@Longitude1 float,
@Latitude2 float,
@Longitude2 float
)
RETURNS float 
AS  
BEGIN

-- SAME LOCATION, RETURN 0
IF @Latitude1 = @Latitude2 AND @Longitude1 = @Longitude2 RETURN 0.00

-- CONSTANTS
DECLARE @EarthRadiusInMiles float;
SET @EarthRadiusInMiles = 3963.1
DECLARE @PI float;
SET @PI = PI();

-- RADIANS conversion
DECLARE @lat1Radians float;
DECLARE @long1Radians float;
DECLARE @lat2Radians float;
DECLARE @long2Radians float;

SET @lat1Radians = @Latitude1 * @PI / 180;
SET @long1Radians = @Longitude1 * @PI / 180;
SET @lat2Radians = @Latitude2 * @PI / 180;
SET @long2Radians = @Longitude2 * @PI / 180;

RETURN Acos(
Cos(@lat1Radians) * Cos(@long1Radians) * Cos(@lat2Radians) * Cos(@long2Radians) + 
Cos(@lat1Radians) * Sin(@long1Radians) * Cos(@lat2Radians) * Sin(@long2Radians) + 
Sin(@lat1Radians) * Sin(@lat2Radians)
) * @EarthRadiusInMiles;
END

Then from any calling query you can sort by the distance, for example:

CREATE PROCEDURE [dbo].[NearbyThings]
(
@Lat float,
@Lon float,
@Num int = 10
)

AS

SET NOCOUNT ON

Select Top (@Num)
'Distance' = Round(dbo.CoordinateDistanceMiles(@Lat, @Lon, Lat, Lon), 1),
ThingId, 
Postcode, 
Lat, 
Lon
From 
Things
Order by
dbo.CoordinateDistanceMiles(@Lat, @Lon, Lat, Lon) ASC
GO

这篇关于我怎么找到附近的位置使用谷歌地图API给定的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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