简体   繁体   中英

Android Google Maps draw polygon in meters

I want to draw a triangle on my google map fragment. It should look like this: i.stack.imgur.com/880kV.png

To create the polygon i need 3 locations(A,B,C). The variables i have to create it are my current location(Point A), the current angle of the phone, the lenght of the 2 longer sides(in m) and the angle between them.

First i get the lenght of 1° in meters based on the latitude

ncgia.ucsb.edu/education/curricula/giscc/units/u014/tables/table01.html

 double factor = ((double)1)/111230

Then i get the angle from north (viewAngleTolerance = angle between the 2 longer lines):

float angleToLocB = angle - (viewAngleTolerance / 2);
float angleToLocC = angle + (viewAngleTolerance / 2);

http://i.stack.imgur.com/x5VF9.png (sorry for the bad paint skills)

Next i calculate the orange and the green lines (a,b) as seen in the picture.

double Ba = 0;
double Bb = 0;

if (angleToLocB >= 0 && angleToLocB <= 90) {


    Ba = Math.sin(Math.toRadians(angleToLocB)) * maxDistance * factor;
    Bb = Math.sin(Math.toRadians(90 - angleToLocB)) * maxDistance * factor;

} else if (angleToLocB > 90 && angleToLocB <= 180) {
    Ba = Math.sin(Math.toRadians(angleToLocB - 90)) * maxDistance * factor;
    Bb = Math.sin(Math.toRadians(90 - (angleToLocB - 90))) * maxDistance * factor;

} else if (angleToLocB > 180 && angleToLocB <= 270) {
    Ba = Math.sin(Math.toRadians(angleToLocB - 180)) * maxDistance * factor;
    Bb = Math.sin(Math.toRadians(90 - (angleToLocB - 180))) * maxDistance * factor;

} else if (angleToLocB > 270 && angleToLocB <= 360) {
    Ba = Math.sin(Math.toRadians(angleToLocB - 270)) * maxDistance * factor;
    Bb = Math.sin(Math.toRadians(90 - (angleToLocB - 270))) * maxDistance * factor;
}

double Ca = 0;
double Cb = 0;

if (angleToLocC >= 0 && angleToLocC <= 90) {
    Ca = Math.sin(Math.toRadians(angleToLocC)) * maxDistance * factor;
    Cb = Math.sin(Math.toRadians(90 - angleToLocC)) * maxDistance * factor;

} else if (angleToLocC > 90 && angleToLocC <= 180) {
    Ca = Math.sin(Math.toRadians(angleToLocC - 90)) * maxDistance * factor;
    Cb = Math.sin(Math.toRadians(90 - (angleToLocC - 90))) * maxDistance * factor;

} else if (angleToLocC > 180 && angleToLocC <= 270) {
    Ca = Math.sin(Math.toRadians(angleToLocC - 180)) * maxDistance * factor;
    Cb = Math.sin(Math.toRadians(90 - (angleToLocC - 180))) * maxDistance * factor;

} else if (angleToLocC > 270 && angleToLocC <= 360) {
    Ca = Math.sin(Math.toRadians(angleToLocC - 270)) * maxDistance * factor;
    Cb = Math.sin(Math.toRadians(90 - (angleToLocC - 270))) * maxDistance * factor;
}

After that i calculate lat and long for B and C.

Location B = new Location("");
Location C = new Location("");

if (angleToLocB > 90 && angleToLocB <= 270) {
    B.setLatitude(currentLoc.getLatitude() - Bb);
} else if (angleToLocB > 0 && angleToLocB <= 90) {
    B.setLatitude(currentLoc.getLatitude() + Bb);
} else if (angleToLocB > 270 && angleToLocB <= 360) {
    B.setLatitude(currentLoc.getLatitude() + Bb);
}

if (angleToLocC > 90 && angleToLocC <= 270) {
    C.setLatitude(currentLoc.getLatitude() - Cb);
} else if (angleToLocC > 0 && angleToLocC <= 90) {
    C.setLatitude(currentLoc.getLatitude() + Cb);
} else if (angleToLocC > 270 && angleToLocC <= 360) {
    C.setLatitude(currentLoc.getLatitude() + Cb);
}

if (angleToLocB > 0 && angleToLocB <= 180) {
    B.setLongitude(currentLoc.getLongitude() + Ba);
} else if (angleToLocB > 180 && angleToLocB <= 360) {
    B.setLongitude(currentLoc.getLongitude() - Ba);
}

if (angleToLocC > 0 && angleToLocC <= 180) {
    C.setLongitude(currentLoc.getLongitude() + Ca);
} else if (angleToLocC > 180 && angleToLocC <= 360) {
    C.setLongitude(currentLoc.getLongitude() - Ca);
}

My last step is to create the polygon and add it to the map:

polygonOptions = new PolygonOptions()
        .add(new LatLng(currentLoc.getLatitude(), currentLoc.getLongitude()),
                new LatLng(B.getLatitude(), B.getLongitude()),
                new LatLng(C.getLatitude(), C.getLongitude()),
                new LatLng(currentLoc.getLatitude(), currentLoc.getLongitude()))
        .strokeColor(Color.BLACK)
        .fillColor(Color.GRAY);

if(polygon != null) {
    if (polygon.isVisible()) {
        polygon.remove();
    }
}
polygon = map.addPolygon(polygonOptions);

This method works but I think there is an easyer method to create this polygon. My first question is if there is any easyer method.

Also if one angle is exactly 0,90,180,270 or 360 or the point gets created wrong. My second question is why this happens.

Thank you for your help.

There is a very easy way to do this by importing these three libraries:

import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;
import com.google.android.gms.maps.model.PolylineOptions;

Follow the step by step tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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