使用Google地图API查找最近的地点 [英] Finding nearest locations using Google Maps API

查看:179
本文介绍了使用Google地图API查找最近的地点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在编写一个应用程序,显示地址附近的自行车站。我有一个服务的每个自行车站的经纬度位置列表。



我可以标记我目前的位置或目前为止的任何地址。如何在地图上显示我所在位置附近的所有自行车站。我是否首先从当前位置获取最近的Google地点,然后访问数据库获取自行车位置并在地图上制作标记?最好的方法是什么?

解决方案

如果您已经拥有自行车站的坐标,并且您信任这些数据,用它来绘制你的标记。现在你必须定义近的含义。

你有不同的解决方案。您可以选择为地图范围内的所有自行车站绘制标记(但可能会根据缩放级别绘制多个标记,或者必须防止脚本在达到某个缩放级别之前绘制标记)或者你可以在一个位置周围的 n 公里内绘制标记(可以是用户位置,地图中心坐标等)。



对于第二种解决方案,如果您将自行车站存储在MySQL数据库中,则可以这样查询:

  (弧度(lat))* cos(弧度(lng) - 弧度(弧度(lat))弧度(弧度(弧度) )。* db-> real_escape_string($ lng)。))+ sin(radians(。$ db-> real_escape_string($ lat)。))* sin(radians(lat))))AS distance FROM your_table_name具有<15的距离; 

$ lat $ lng 作为您的中心坐标
$ b $ lat lng 是你的MySQL列名称



15 是半径(单位为公里)您的坐标



这使用Haversine公式。好的信息可以在这里找到。



希望这会有所帮助,但我们并不真正了解如何在应用中组织数据。给我们更多的信息,如果你需要更多的帮助!


Hi I'm writing an app that shows the bike stations near an address. I have the list of latitude and longitude locations of each bike station from a service.

I can mark my current location, or any address so far. How do I show all the bike stations near my location on map. Do I first get nearest Google places from the current location and then access the database for bike location and make markers on the map? What is the best approach?

解决方案

If you already have the coordinates of your bike stations and if you trust this data, then just use it to draw your markers. Now you have to define what "near" means.

You have different solutions for this. Either you choose to draw the markers for all the bike stations within the map bounds (but that could be many markers to draw depending on the zoom level, or you have to prevent your script from drawing the markers before a certain zoom level is reached) or you can draw the markers within n kilometers around a location (can be the user location, the map center coordinates, etc.).

For the second solution, and if you are storing your bike stations in a MySQL database, you could do a query like that:

$sql = "SELECT *, ( 6371 * acos( cos( radians(" . $db->real_escape_string($lat) . ") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(" . $db->real_escape_string($lng) . ") ) + sin( radians(" . $db->real_escape_string($lat) . ") ) * sin( radians( lat ) ) ) ) AS distance FROM your_table_name HAVING distance < 15";

$lat and $lng being your center coordinates

lat and lng being your MySQL column names

15 being the radius (in km) around your coordinates

This uses the Haversine formula. Good information can be found here.

Hope this helps, but we don't really know how you organized your data in your app. Give us more info if you need more help on this!

这篇关于使用Google地图API查找最近的地点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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