Msimao
Msimao

Reputation: 33

Android GoogleMap a square appears rectangle

I've created a GoogleMap object and drawn a polygon with a square vertex as shown in the figure, but the square appears an oblong. What am I doing wrong?

This is a fragment of the code:

@Override
public void onMapReady(GoogleMap googleMap) {

    mMap = googleMap;


    // Add a marker in Sydney and move the camera
    LatLng enidh = new LatLng(38.6925785, -9.2955145);
    mMap.addMarker(new MarkerOptions().position(enidh).title("Marker in ENIDH"));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(enidh, 10.0f));


    mBoats[0] = mMap.addPolygon(new PolygonOptions()
            .add(new LatLng(38.680026, -9.2846651),
                    new LatLng(38.690026, -9.2846651),
                    new LatLng(38.690026, -9.2946651),
                    new LatLng(38.680026, -9.2946651),
                    new LatLng(38.680026, -9.2846651))
            .fillColor(Color.CYAN)
            .strokeColor(Color.BLUE)
            .strokeWidth(5));

This is the square (rectangle) appearance in android app emulator

Upvotes: 3

Views: 62

Answers (2)

miensol
miensol

Reputation: 41648

You've specified coordinates of the Polygon using LatLng. However the distances (i.e. in km) represented by 1 longitude and 1 latitude are not the same:

Each degree of latitude is approximately 69 miles (111 kilometers) apart. The range varies (due to the earth's slightly ellipsoid shape) from 68.703 miles (110.567 km) at the equator to 69.407 (111.699 km) at the poles. This is convenient because each minute (1/60th of a degree) is approximately one mile.

A degree of longitude is widest at the equator at 69.172 miles (111.321) and gradually shrinks to zero at the poles. At 40° north or south the distance between a degree of longitude is 53 miles (85 km).

Thus even though the edges lengths of polygon you've created have the same unit-less value you get a rectangle that is not a square.

Upvotes: 3

Rahul Kumar
Rahul Kumar

Reputation: 2344

You are not adding polygon properly. You have to use 4 different coordinates as corners of rectangle like this.

// Instantiates a new Polyline object and adds points to define a rectangle
PolylineOptions rectOptions = new PolylineOptions()
        .add(new LatLng(37.35, -122.0))
        .add(new LatLng(37.45, -122.0))  // North of the previous point, but at the same longitude
        .add(new LatLng(37.45, -122.2))  // Same latitude, and 30km to the west
        .add(new LatLng(37.35, -122.2))  // Same longitude, and 16km to the south
        .add(new LatLng(37.35, -122.0)); // Closes the polyline.

// Get back the mutable Polyline
Polyline polyline = myMap.addPolyline(rectOptions);

Source : https://developers.google.com/maps/documentation/android-api/shapes

Upvotes: 1

Related Questions