简体   繁体   English

获取两个位置 android 谷歌地图之间的距离并画线

[英]Get distance and draw line between two location android google maps

I have a app that opens the map and zooms on my location with a marker and then if i longClick on any place it saves that location in database everything works fine but one thing i cannot manage is draw a line of path between two location markers.我有一个应用程序,它打开map并用标记放大我的位置,然后如果我长按任何位置,它将该位置保存在数据库中一切正常,但我无法管理的一件事是在两个位置标记之间绘制一条路径。

these are three global variables这是三个全局变量

 Location myLocation;
 LatLng onmaplongclicklastlatlng;
 private GoogleMap mMap;

mylocation is assigned by LocationListener and onmaplongclicklastlatlng is assigned when i long click on any place on the map. mylocationLocationListener分配, onmaplongclicklastlatlng是在我长按 map 上的任何位置时分配的。

placing the marker on both location is done i already have the two points i want to draw the line on.将标记放置在两个位置都完成了我已经有了我想要画线的两个点。

i just need to draw a line between those two locations (my current location periodically updated in the myLocation variable)我只需要在这两个位置之间画一条线(我的当前位置会在myLocation变量中定期更新)

and onmaplongclicklastlatlng changed whenever i long click on the map.每当我长按 map 时, onmaplongclicklastlatlng就会发生变化。

the whole code is done and i have a button that i want to use to draw the line since i can access the two location variables from this button method, Other code need not be changed.整个代码已经完成,我有一个button ,我想用它来draw线,因为我可以从这个button方法访问两个位置variables ,其他代码不需要更改。

public void getLine(View view) {

    // need to draw a line here from user gps posiution to the marker location    
}

i have tried many tutorials but they do not work for some reason, any help would be really appreciated.我已经尝试了很多教程,但由于某种原因它们不起作用,任何帮助将不胜感激。

oh and last but not least哦,最后但并非最不重要

onmaplongclicklastlatlng

is a LatLng and是一个 LatLng 并且

myLocation

is a Location是一个位置

but they can be interchanged to fit the purpose但它们可以互换以适应目的

ex. Location maplocation = new Location(onmaplongclicklastlatlng
.latitude, onmaplongclicklastlatlng.longitude);

and

Latlng myLatLng = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());

As i said any help would be really appreciated.正如我所说,任何帮助将不胜感激。 And let me know if anything else needed i will edit the question then.并让我知道是否需要其他任何内容,然后我将编辑该问题。

To calculate accurate distance between two loaction you have to use Google Map Distance Matrix API . 要计算两个机车之间的准确距离,您必须使用Google Map Distance Matrix API Construct your url like below. 如下构建您的网址。

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

And to draw line between two location you have to first use the Google Map Direction Api , construct your url like below 要在两个位置之间画线,您必须首先使用Google Map Direction Api ,如下所示构建您的网址

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

above api call return a list of latlng add the list into polylines 上面的api调用返回列表列表,将列表添加到折线中

 Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
                .clickable(true)
                .addAll(latlngList));

hope it helps. 希望能帮助到你。

To add a polyline -添加折线 -

 val polyline1 = map?.addPolyline(
            PolylineOptions().clickable(true)
                .add(LatLng(23.8103, 90.4125), LatLng(4.0383, 21.7587))
                .geodesic(true) // to make the line curvw
        )
        // To add styles in the line
        stylePolyline(polyline1)

 private fun stylePolyline(polyline: Polyline?) {
        polyline?.let {
            it.color = ContextCompat.getColor(binding.mapPlaceholder.context, R.color.lineColor)
            it.pattern = mutableListOf(Dot(), Gap(5F))
            it.startCap = RoundCap()
            it.jointType = JointType.ROUND
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM