找到街道(获得他们的坐标)并计算标记之间的距离 [英] Finding streets (get their coordinates) and calculating distances between markers

查看:121
本文介绍了找到街道(获得他们的坐标)并计算标记之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到关于如何使用API​​的任何信息:


  1. 搜索街道并获得一些可点击的结果返回一个LatLng对象或其他东西(至少是坐标)。因此,如果我搜索街道并单击结果,我会平移到该街道(例如)。


  2. 如何计算标记之间的距离?并可能在它们之间画线。

  3. 我得到的只是API,但没有指导,因此很难找出答案

    解决方案

    下面是一个演示如何实现的小提琴:



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

    要将街道名称转换为经纬度,您需要使用Geocoder,例如Google Maps API提供的地理编码。

    然后,当您从地理编码返回结果时,可以使用lat lng属性创建标记。一旦你的第一个标记被放置,你重复这个过程为你的第二个街道地址。我们嵌套这些调用,以便我们可以确保执行的顺序,因为它们是异步的。



    一旦我们有了第二个标记,我们就可以使用Google的折线函数在两个标记点之间绘制一条线。



    <然后我们用一个稍微复杂的数学公式来做一些距离计算,并且瞧。


    Google Maps API具有丰富的文档记录,希望这个小提示可以将它放入上下文中,您可以通过查看文档中的函数和属性来逐步探索每个元素

    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.

    解决方案

    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

    这篇关于找到街道(获得他们的坐标)并计算标记之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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