首页 文章

如何使用谷歌api找到距离

提问于
浏览
4

我正在使用Google pLaces api开发一款应用 . 我能够获得正在使用我的应用程序的用户附近的咖啡馆列表 . 结果列表包括地点的详细信息,例如地点名称,地址,纬度经度值和电话号码 . 我想知道的是我如何请求place api返回用户与结果列表中每个条目之间的距离 . 我已经在stackoverflow上发布了一些讨论类似主题的链接 . 我也浏览了Googles文档,但没有提到任何关于获取这些距离的信息 . 我想避免使用任何数学公式,如大圆算法,因为在这种情况下返回的距离将是直线距离,这将不是一个准确的距离估计 . 所以有人请告诉我如何请求api给我用户与结果中的每个条目之间的距离?

我经历的一些链接是:How do i find distance between one place to another using Geolocation or Similiar API without embedding a Google Map?

find Distance in kilometers in Android using google places Api

How to get Distance Kilometer in android?

5 回答

  • 2

    我想如你所说 - 你想避免使用

    任何数学公式,所以你也可以尝试这个 -

    您可以距离地点的距离

    http://maps.googleapis.com/maps/api/distancematrix/xml?origins="+a+"&destinations="+b+"&mode=driving&language=en-EN&sensor=false with a=addressFrom, b=addressTo
    

    如果你有lat,很长的位置,你可以获得该位置的地址:

    double latiTude = 10.3929393;
     double longiTude = 150.93984884;
    
     Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault());
      GeoPoint p= new GeoPoint((int) (latiTude * 1E6), (int) (longiTude * 1E6));
      List<Address> address = geocoder.getFromLocation(
                                                        p.getLatitudeE6() / 1E6,
                                                        p.getLongitudeE6() / 1E6, 1);
        if (address.size() > 0) 
        {
        String addressFroLatLong= "";
        for (int i = 0; i < address.get(0).getMaxAddressLineIndex(); i++) 
        {   addressFroLatLong+= address.get(0).getAddressLine(i) + "\n";
        }   
       }
    

    PLease upvote if helped. nothing else. Thanks, happy to help enjoy..

  • 3

    下面给出了Haversine的实现 . 但是我仍然觉得google places api应该提供一些服务,这有助于找到两点之间的确切距离 . 如果有人知道那么请告诉我..截至目前,Haversine是一个很好的替代品..

    import java.lang.*;
    import java.io.*;
    import java.math.*;
    public class DistanceCalculation {
    
    public static void main(String[] args)
    {
        double lon11 =-97.116121;
          double lat11=32.734642;
    
          double lon22=-97.111658;
          double lat22=32.731918;
    
        //   calculateDistance(lat11,lon11, lat22, lon22);
          distFrom(lat11, lon11, lat22, lon22);
    
    }
    public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
        double earthRadius = 3958.75;
        double dLat = Math.toRadians(lat2-lat1);
        double dLng = Math.toRadians(lng2-lng1);
        double sindLat = Math.sin(dLat / 2);
        double sindLng = Math.sin(dLng / 2);
        double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
                * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        double dist = earthRadius * c;
        System.out.println(dist);
        return dist;
        }
    }
    
  • 0

    两点之间的距离可以通过这里描述的等式找到:http://williams.best.vwh.net/avform.htm#Dist记住,使用弧度,而不是度数!

    您似乎对在每个版本中更新 Map 密钥感到担忧吗?我在市场上有一个基于 Map 的应用程序,你得到1个prod密钥和1个调试密钥(我相信它是每个开发人员,但如果没有,它是每个应用程序) . 它与您用来签署apk的密钥有关 . 我所说的是,如果你得到你的prod api密钥,并且只开发这个应用程序,你可以发布许多更新而无需再次触摸密钥(假设你不在本地进行调试密钥测试) . 如果您对此感到担心,可以将它们保存在AndroidManifext.xml文件中,只是注释掉不相关的文件,具体取决于您是部署到设备还是模拟器 .

  • -1

    另一个解决方案:你也可以这样做:

    //your two locations
    Location locationA;
    Location locationB;
    
    //distance between them
    float distanceBetweenPoints = locationA.distanceTo(locationB);
    

    有点晚,但希望这有助于:)

  • -1

    两个地理位置之间的计算并不困难,特别是对于您的任务,找到附近酒店的距离 .

    你可以使用非常适合你的任务的哈斯金公式 .

    但是这些地理公式给出了您需要路线服务的空中距离,而不是道路距离 .

相关问题