获取两个位置 android google 地图之间的距离并画线
Get distance and draw line between two location android google maps
我有一个应用可以打开地图并使用标记放大我的位置
然后如果我在任何地方 longClick 它会将该位置保存在数据库中
一切正常,但我无法管理的一件事是在两个位置标记之间画一条路径。
这是三个全局变量
Location myLocation;
LatLng onmaplongclicklastlatlng;
private GoogleMap mMap;
mylocation
由LocationListener
分配,onmaplongclicklastlatlng
在我长按地图上的任何地方时分配。
在两个位置上放置标记已完成,我已经有了要在其上画线的两个点。
我只需要在这两个位置之间画一条线 (我的当前位置在 myLocation 变量中定期更新)
和 onmaplongclicklastlatlng 在我长按地图时更改。
整个代码已完成,我有一个 button
想用于 draw
行
因为我可以从这个 button
方法访问两个位置 variables
,所以其他代码不需要更改。
public void getLine(View view) {
// need to draw a line here from user gps posiution to the marker location
}
我尝试了很多教程,但由于某种原因它们不起作用,我们将不胜感激。
哦,最后但并非最不重要的一点
onmaplongclicklastlatlng
是 LatLng 并且
myLocation
是一个地点
但它们可以互换以适应目的
ex. Location maplocation = new Location(onmaplongclicklastlatlng
.latitude, onmaplongclicklastlatlng.longitude);
和
Latlng myLatLng = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
正如我所说,任何帮助将不胜感激。
让我知道是否还有其他需要,然后我会编辑问题。
这些怎么样:
https://github.com/zhenyanghua/MeasureTool-GoogleMaps-V3
http://zhenyanghua.github.io/google-maps/measure-tool/
此致
PS: 我不是该软件的作者
要计算两个位置之间的准确距离,您必须使用 Google Map Distance Matrix API。像下面这样构建你的 url。
https://maps.googleapis.com/maps/api/distancematrix/json?" +"origin=" + origin.latitude + "," + origin.longitude+ "&" +"destination=" + dest.latitude + "," + dest.longitude +"&sensor=false&units=metric&mode=driving" + "&" + key=YOUR_API_KEY
要在两个位置之间画线,您必须首先使用 Google Map Direction Api,像下面那样构造您的 url
https://maps.googleapis.com/maps/api/directions/json?" +"origin=" + origin.latitude + "," + origin.longitude+ "&" +"destination=" + dest.latitude + "," + dest.longitude +"&sensor=false&units=metric&mode=driving" + "&" + key=YOUR_API_KEY
上面 api 调用 return latlng 列表将列表添加到折线中
Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
.clickable(true)
.addAll(latlngList));
希望对您有所帮助。
我有一个应用可以打开地图并使用标记放大我的位置 然后如果我在任何地方 longClick 它会将该位置保存在数据库中 一切正常,但我无法管理的一件事是在两个位置标记之间画一条路径。
这是三个全局变量
Location myLocation;
LatLng onmaplongclicklastlatlng;
private GoogleMap mMap;
mylocation
由LocationListener
分配,onmaplongclicklastlatlng
在我长按地图上的任何地方时分配。
在两个位置上放置标记已完成,我已经有了要在其上画线的两个点。
我只需要在这两个位置之间画一条线 (我的当前位置在 myLocation 变量中定期更新)
和 onmaplongclicklastlatlng 在我长按地图时更改。
整个代码已完成,我有一个 button
想用于 draw
行
因为我可以从这个 button
方法访问两个位置 variables
,所以其他代码不需要更改。
public void getLine(View view) {
// need to draw a line here from user gps posiution to the marker location
}
我尝试了很多教程,但由于某种原因它们不起作用,我们将不胜感激。
哦,最后但并非最不重要的一点
onmaplongclicklastlatlng
是 LatLng 并且
myLocation
是一个地点
但它们可以互换以适应目的
ex. Location maplocation = new Location(onmaplongclicklastlatlng
.latitude, onmaplongclicklastlatlng.longitude);
和
Latlng myLatLng = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
正如我所说,任何帮助将不胜感激。 让我知道是否还有其他需要,然后我会编辑问题。
这些怎么样:
https://github.com/zhenyanghua/MeasureTool-GoogleMaps-V3 http://zhenyanghua.github.io/google-maps/measure-tool/
此致
PS: 我不是该软件的作者
要计算两个位置之间的准确距离,您必须使用 Google Map Distance Matrix API。像下面这样构建你的 url。
https://maps.googleapis.com/maps/api/distancematrix/json?" +"origin=" + origin.latitude + "," + origin.longitude+ "&" +"destination=" + dest.latitude + "," + dest.longitude +"&sensor=false&units=metric&mode=driving" + "&" + key=YOUR_API_KEY
要在两个位置之间画线,您必须首先使用 Google Map Direction Api,像下面那样构造您的 url
https://maps.googleapis.com/maps/api/directions/json?" +"origin=" + origin.latitude + "," + origin.longitude+ "&" +"destination=" + dest.latitude + "," + dest.longitude +"&sensor=false&units=metric&mode=driving" + "&" + key=YOUR_API_KEY
上面 api 调用 return latlng 列表将列表添加到折线中
Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
.clickable(true)
.addAll(latlngList));
希望对您有所帮助。