Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中使用MapAPI计算以公里为单位的距离_Java_Android_Api_Google Maps_Latitude Longitude - Fatal编程技术网

Java 如何在android中使用MapAPI计算以公里为单位的距离

Java 如何在android中使用MapAPI计算以公里为单位的距离,java,android,api,google-maps,latitude-longitude,Java,Android,Api,Google Maps,Latitude Longitude,我有我的API来计算距离。但是,我无法获得使用API的代码。我是android新手。请提供使用API的代码。我使用的是经纬度,但距离显示不正确。伙计们,救命 public String[] Distance(final double lat1, final double lon1, final double lat2, final double lon2) { final String[] parsedDistance = new String[1]; final String[

我有我的API来计算距离。但是,我无法获得使用API的代码。我是android新手。请提供使用API的代码。我使用的是经纬度,但距离显示不正确。伙计们,救命

public String[] Distance(final double lat1, final double lon1, final double lat2, final double lon2) {
    final String[] parsedDistance = new String[1];
    final String[] response = new String[1];
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {

                Request request = new Request.Builder()
                        .url("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=lat1%2Clon1&destinations=lat2%2Clon2")
                        .get()
                        .addHeader("key", "AIzaSyBP1g2YfJmsZ8zVhQBKffnGxL7TyqTYFxk")
                        .addHeader("cache-control", "no-cache")
                        .build();

            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return parsedDistance;
}

找到距离计算的解决方案:

double radlat1 = Math.PI * currentLat/ 180;
double radlat2 = Math.PI * mAtm[i].lat / 180;
double radlon1 = Math.PI * currentLng/ 180;
double radlon2 = Math.PI * mAtm[i].lon / 180;
double theta = lnglocation - mAtm[i].lon;
double radtheta = Math.PI * theta / 180;
double dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
                            dist = Math.acos(dist);
                            dist = dist * 180 / Math.PI;
                            dist = dist * 60 * 1.1515;

  DecimalFormat df = new DecimalFormat("####0.00");
  double dist = Double.parseDouble((df.format(dist * 1.609344)));

让我们看看你试过什么。你的问题应该有一个答案:你的url是错误的,这不是你在字符串中使用参数的方式。使用
++
连接字符串。我也使用了它-“+lat1+”、“+lon1+”&destination=“+lat2+”、“+lon2+”&sensor=false&units=metric&mode=driving”在代码中编辑它。您应该从一个新的示例开始,并在internet上查找更多教程