johnsonjp34
johnsonjp34

Reputation: 3299

Google Maps SphericalUtil computeOffset

I'm using the computeOffset() from the SpericalUtil in the google maps android library. The computeOffset takes the parameters:

public static LatLng computeOffset(LatLng from, double distance, double heading)

with the original coordinates of the first two markers I can use the computeOffset to generate another line after inputing the distance and the heading. My goal is to make parallel lines at a certain distance apart. But as you can see below it looks pretty good when the lines are nearly north and south, but when you go towards east and west it looks to me like the distance between the two lines isn't the same value. The end points appear to be the right distance but offset. How can I make it so all the lines look like the first image no matter what direction?

public void onMarkerDragEnd(Marker marker) {
    showDistance();


LatLng markerCPosition = SphericalUtil.computeOffset(mMarkerA.getPosition(), 50, 90); 

    mMarkerC = getMap().addMarker(new MarkerOptions().position(markerCPosition).draggable(true).visible(false));

LatLng markerDPosition = SphericalUtil.computeOffset(mMarkerB.getPosition(), 50, 90);

    mMarkerD = getMap().addMarker(new MarkerOptions().position(markerDPosition).draggable(true).visible(false));

    updatePolyline();
}

North South

east west

Upvotes: 2

Views: 4088

Answers (1)

Oliver Jonas
Oliver Jonas

Reputation: 1248

At the moment you're just shifting the points sideways using a constant angle (90 degrees). If I understand you correctly and you want both lines parallel to each other separated by some distance you need to take into account the heading of the original A-B line like so:

double heading = SphericalUtil.computeHeading(posA, posB);
LatLng markerCPosition = SphericalUtil.computeOffset(posA, 50, heading + 90);
LatLng markerDPosition = SphericalUtil.computeOffset(posB, 50, heading + 90);

Upvotes: 2

Related Questions