2

I have made an android app, showing google maps, and on that map i am drawing a polygon whose LatLng is stored in the arrayList.. I want to find the area of that polygon drawn on the google map, I have tried my best, but didn't succeeded, I am unable to use Spherical util, compute area function, as it gives error, please provide me the best help,,,

Thanks in advance, I am providing my code below, plz suggest how to find area of the polygon, being drawn... thanks..

dpackage com.example.mapss;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolygonOptions;
import com.google.android.gms.maps.model.PolylineOptions;

public class MainActivity extends Activity {


GoogleMap map;
ArrayList<LatLng> arrayPoints =null;
PolylineOptions polylineOptions;
PolygonOptions polygonOptions;
RadioGroup rg_views;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    arrayPoints = new ArrayList<LatLng>();

    map=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    map.getUiSettings().setZoomControlsEnabled(true);
    map.getUiSettings().setCompassEnabled(true);
    map.getUiSettings().setMyLocationButtonEnabled(true);

    map.setMyLocationEnabled(true);

    rg_views =(RadioGroup)findViewById(R.id.rg_views);
    rg_views.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub

            if (checkedId==R.id.rb_map){
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

            }
            else if(checkedId==R.id.rb_satellite){
                map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

            }
            else if(checkedId==R.id.rb_terrain){
                map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            }

        }
    });

    map.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng point) {
            // TODO Auto-generated method stub

            MarkerOptions markerOptions = new MarkerOptions();

            markerOptions.position(point);

            markerOptions.title(point.latitude + ": " + point.longitude);

            map.clear();

            map.animateCamera(CameraUpdateFactory.newLatLng(point));


            map.addMarker(markerOptions);

            polygonOptions = new PolygonOptions();
            polygonOptions.fillColor(Color.RED);
            polygonOptions.strokeWidth(5);
            arrayPoints.add(point);
            polygonOptions.addAll(arrayPoints);
            map.addPolygon(polygonOptions);



        }
    });

    map.setOnMapLongClickListener(new OnMapLongClickListener() {

        @Override
        public void onMapLongClick(LatLng point) {
            // TODO Auto-generated method stub

            map.clear();
            arrayPoints.clear();

        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}
Mirnal Mannu
  • 93
  • 1
  • 10

3 Answers3

0

Google Map Api Support library working fine with me https://developers.google.com/maps/documentation/android/utility/

Pankaj Kant Patel
  • 2,050
  • 21
  • 27
  • This does not answer the question. Please read the question more closely. The question is not how to tell if a point is inside a polygon, it is asking how to compute the area of the polygon. – Tim Lewis Apr 15 '15 at 18:09
  • Tim is right, i want to know that how can i compute the area of the polygon drawn on the map... – Mirnal Mannu Apr 16 '15 at 06:19
  • See also here: http://stackoverflow.com/questions/28838287/calculate-the-area-of-a-polygon-drawn-on-google-maps-in-an-android-application – Leukipp Jan 09 '16 at 08:00
0

To use computeArea method you need to import

com.google.maps.android.SphericalUtil

In my case I imported it mannually. Then you can call

SphericalUtil.computeArea(listOfLatLng)
Shaishav Jogani
  • 2,111
  • 3
  • 23
  • 33
0

You should use this function

com.google.maps.android.SphericalUtil;
public static double computeArea(java.util.List<LatLng> path)

path must be closed path. Don't forget to add dependencies in your gradle.

compile 'com.google.maps.android:android-maps-utils:0.5+'

Check this link for more functions. http://googlemaps.github.io/android-maps-utils/

Shaishav Jogani
  • 2,111
  • 3
  • 23
  • 33