获取android中两个位置之间的距离?

编程入门 行业动态 更新时间:2024-10-20 00:34:08
本文介绍了获取android中两个位置之间的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要获取两个位置之间的距离,但我需要获取图片中蓝线之类的距离.

i need to get distance between two location, but i need to get distance like blue line in the picture.

我尝试下一个:

public double getDistance(LatLng LatLng1, LatLng LatLng2) { double distance = 0; Location locationA = new Location("A"); locationA.setLatitude(LatLng1.latitude); locationA.setLongitude(LatLng1.longitude); Location locationB = new Location("B"); locationB.setLatitude(LatLng2.latitude); locationB.setLongitude(LatLng2.longitude); distance = locationA.distanceTo(locationB); return distance; }

但我得到了红线距离.

推荐答案

使用 Google Maps DirectionsAPI.您需要通过 HTTP 请求路线.您可以直接从 Android 或通过您自己的服务器执行此操作.

Use the Google Maps Directions API. You'll need to request the directions over HTTP. You can do this directly from Android, or via your own server.

例如,来自蒙特利尔的路线到多伦多:

GET maps.googleapis/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false

你最终会得到一些 JSON.在routes[].legs[].distance中,你会得到一个像这样的对象:

You'll end up with some JSON. In routes[].legs[].distance, you'll get an object like this:

"legs" : [ { "distance" : { "text" : "542 km", "value" : 542389 },

您也可以直接从响应对象中获取折线信息.

You can also get the polyline information directly from the response object.

更多推荐

获取android中两个位置之间的距离?

本文发布于:2023-11-12 20:20:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582438.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:距离   位置   两个   android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!