Finding streets (get their coordinates) and calculating distances between markers

社会主义新天地 提交于 2019-12-31 04:18:05

问题


I have trouble finding any information on how to use the API to:

  1. Search for streets and get some clickable results that returns a LatLng object or something (at least coordinates). So If I search for a street and click on a result, I'll pan to that street (for example).

  2. How can I calculate distance between markers? and possibly, draw lines between them.

All I get is the API but no guides so it's fairly hard figuring out what types to use.


回答1:


Here is a fiddle showing how this can be achieved:

http://jsfiddle.net/foxwisp/vQGMr/1/

To convert street names to lat/lng you need to use a Geocoder such as the one provided by Google Maps API.

Then, when you get back the results from the geocode, you use the lat lng properties to create a marker. Once your first marker is placed, you repeat the process for your second street address. We nest these calls so that we can be assured of the order of execution due to their asynchronous nature.

Once we have our second marker we use Google's polyline function to draw a line between the two latlng marker points.

Then we use a slightly complicated mathematical equation to do some distance calculations and voila.

The Google Maps API is fantastically documented, hopefully this fiddle will put it into context for you and you can explore each element step by step by reviewing functions and properties in the documentation



来源:https://stackoverflow.com/questions/10906805/finding-streets-get-their-coordinates-and-calculating-distances-between-marker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!