当前位置: 动力学知识库 > 问答 > 编程问答 >

android - How to bind the distance between two Markers to my polyline?

问题描述:

First of all I apologise for my poor English.

I currently working on an android-app and I am using the google api v2. I have got 2 Markers on my google map and calculate the distance between them, with the .distancebetween method. Then I draw a Polyline between the markers.

Now i want to bind the distance to my Polyline. The distance should be under the polyline and if i move a marker the distance should still under the polyline.

网友答案:

This method may help you to find the distance, it finds the distance between the starting points LatLng and ends point LatLng

public double CalculationByDistance(LatLng StartP, LatLng EndP) {
    int Radius=6371;//radius of earth in Km         
    double lat1 = StartP.latitude;
    double lat2 = EndP.latitude;
    double lon1 = StartP.longitude;
    double lon2 = EndP.longitude;
    double dLat = Math.toRadians(lat2-lat1);
    double dLon = Math.toRadians(lon2-lon1);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
    Math.sin(dLon/2) * Math.sin(dLon/2);
    double c = 2 * Math.asin(Math.sqrt(a));
    double valueResult= Radius*c;
    double km=valueResult/1;
    DecimalFormat newFormat = new DecimalFormat("####");
    int kmInDec =  Integer.valueOf(newFormat.format(km));
    double meter=valueResult%1000;
    int  meterInDec= Integer.valueOf(newFormat.format(meter));
    Log.i("Radius Value",""+valueResult+"   KM  "+kmInDec+" Meter   "+meterInDec);

    return Radius * c;
}

you can also try this :

Location locationA = new Location("location A");  

locationA.setLatitude(locationA.getLatitudeE6() / 1E6);  
locationA.setLongitude(locationA.getLongitudeE6() / 1E6);  

Location locationB = new Location("location B");  

locationB.setLatitude(locationB.getLatitudeE6() / 1E6);  
locationB.setLongitude(locationB.getLongitudeE6() / 1E6);  

double distance = locationA.distanceTo(locationB);
网友答案:

This is not easy to do, but certainly possible.

Here are the steps:

  • calculate center of each line
  • for that center latitude, calculate point that is 50dp below it (using Projection)
  • put a marker on that position
  • set marker icon to Bitmap with text
  • update markers position when you get calls to OnCameraChangeListener.onCameraChange or in Handler's Runnable posted every few milliseconds
分享给朋友:
您可能感兴趣的文章:
随机阅读: