【问题标题】:Android how to use location.distanceTo()Android 如何使用 location.distanceTo()
【发布时间】:2015-03-28 08:58:23
【问题描述】:

我正在使用一个片段,我需要获取我当前的位置(纬度和经度) 我需要用目的地位置计算我当前的位置。

你能帮我一把吗?计算位置的最佳方法是什么:

如何使用 lat 和 lng 获取我当前位置与目标位置的距离;

    LocationManager locationManager;
    Location locatio;



locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);

location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);





String product_latitude     = "3.121191";
            String product_longtitude   = "101.673298";

            Double latitude     = Double.valueOf(product_latitude);
            Double longtitude   = Double.valueOf(product_longtitude);

            // Calculate the Distance
            String product_distance = location.distanceTo(latitude, longtitude);

【问题讨论】:

  • 你有什么问题?
  • 如何使用 lat 和 lng 获取当前位置与目标位置的距离?

标签: java android string google-maps location


【解决方案1】:

试试这个

double latitude=lat;
double longitude=lng;    
float distance=0;
Location crntLocation=new Location("crntlocation");
crntLocation.setLatitude(currentLatitude);
crntLocation.setLongitude(currentLongitude);

Location newLocation=new Location("newlocation");
newLocation.setLatitude(latitude);
newLocation.setLongitude(longitude);


//float distance = crntLocation.distanceTo(newLocation);  in meters
distance =crntLocation.distanceTo(newLocation) / 1000; // in km

【讨论】:

    【解决方案2】:

    您可以使用 Location: distanceBetween 的静态方法来获取 2 个位置之间的距离。

    float [] results = new float[5]; 
    Location.distanceBetween(startLatitude, startLongitude, endLatitude,
    endLongitude, results)
    

    文档:here

    【讨论】:

      【解决方案3】:

      使用 Direction API 查找从您当前位置到目的地的距离。它返回一个 JSON 对象作为距离,为您提供以英里为单位的距离,您可以解析该 JSON 并将其显示在您的应用屏幕上。

      有关 Google 的官方文档,请阅读 here

      如需教程,请阅读here

      希望这会有所帮助!!!

      【讨论】:

        【解决方案4】:

        "distanceTo" 给出一个近似的结果...

        您可以使用 DistanceMatrix API 来获得接近准确的结果。

        DistanceMatrix API

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-12
          • 1970-01-01
          • 1970-01-01
          • 2014-08-04
          • 2011-10-15
          • 2011-07-24
          相关资源
          最近更新 更多