A. Finity
A. Finity

Reputation: 47

How to draw a Polygon (Rectangle) on Google Maps with defined distance (Meters) from user's input points

This Android app will be used to help define borders or city limits etc. The user will draw a rectangle defining the borders on the Google map, and the app will increase the the length and width of the rectangle the by the predefined amount of meters.

I know there is a method in the API that calculates distance between coordinates, but here I am starting with meters and want to find the coordinates of the new (to be drawn) larger polygon. Does anyone know how I would make this calculation? Thanks in advance!

Upvotes: 2

Views: 6054

Answers (2)

antonio
antonio

Reputation: 18242

You can use the SphericalUtil.computeOffset method from the Google Maps Android API Utility Library

// The distance you want to increase your square (in meters)
double distance = 104.52;

// A List of LatLng defining your user's input
// (Two latLng define a square)
List<LatLng> positions = new ArrayList<>();
positions.add(new LatLng(40.22861, -3.95567));
positions.add(new LatLng(40.22884, -3.95342));

// Create a LatLngBounds.Builder and include your positions
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (LatLng position : positions) {
    builder.include(position);
}

// Calculate the bounds of the initial positions
LatLngBounds initialBounds = builder.build();

// Increase the bounds by the given distance
// Notice the distance * Math.sqrt(2) to increase the bounds in the directions of northeast and southwest (45 and 225 degrees respectively)
LatLng targetNorteast = SphericalUtil.computeOffset(initialBounds.northeast, distance * Math.sqrt(2), 45);
LatLng targetSouthwest = SphericalUtil.computeOffset(initialBounds.southwest, distance * Math.sqrt(2), 225);

// Add the new positions to the bounds
builder.include(targetNorteast);
builder.include(targetSouthwest);

// Calculate the bounds of the final positions
LatLngBounds finalBounds = builder.build();

You can draw the bounds to see if everything is working using the following function:

private void drawBounds (LatLngBounds bounds, int color) {
    PolygonOptions polygonOptions =  new PolygonOptions()
            .add(new LatLng(bounds.northeast.latitude, bounds.northeast.longitude))
            .add(new LatLng(bounds.southwest.latitude, bounds.northeast.longitude))
            .add(new LatLng(bounds.southwest.latitude, bounds.southwest.longitude))
            .add(new LatLng(bounds.northeast.latitude, bounds.southwest.longitude))
            .strokeColor(color);

    mMap.addPolygon(polygonOptions);
}

For example:

drawBounds (initialBounds, Color.BLUE);
drawBounds (finalBounds, Color.RED);

Upvotes: 2

KENdi
KENdi

Reputation: 7741

In getting the coordinates of a polygon you can check this codepen in how to do that. Your output here will be set of coordinates(Lat and Lng). Since you want to get the coordinates in meter you can use spherical geometry in calculating the distance.

To compute this distance, call computeDistanceBetween(), passing it two LatLng objects. Distance results are expressed in meters.

Check also this SO questions for more information.

SO ticket 7997627

SO ticket 5072059

Upvotes: 0

Related Questions