0

I am trying to make an android project about polygon.I have created multiple polygons that overlap each other on the map.

Screenshot:

enter image description here

I want to detect through GPS when the position is within certain polygon with toast notification but I do not know how to detect it and which code I should use. Could someone give me full code example by reusing my code instead of the algorithm so I can learn it ? I know it's a lot to ask but to be honest, I can't fully understand if you explain by algorithm. Thank you for your understanding.

Below are the codes :

public class MainActivity extends FragmentActivity  {

    private GoogleMap gMap;

    static final LatLng SEA = new LatLng(2.765967, 106.221805);

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

        SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
        gMap = mapFragment.getMap();
        gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(SEA,10));

        Marker tamanmini = gMap.addMarker(new MarkerOptions()
        .position(SEA)
        .title("SEA")
        .snippet("Trying to detect user in overlay polygons with hole ")
        .icon(BitmapDescriptorFactory.defaultMarker()));

        Polygon outterseapolygon = gMap.addPolygon(new PolygonOptions()
        .add(new LatLng(2.825774, 106.119004),
             new LatLng(2.789591, 106.098893),    
             new LatLng(2.739712, 106.099523),
             new LatLng(2.690211, 106.134120),
             new LatLng(2.679279, 106.239343),
             new LatLng(2.697027, 106.299454),
             new LatLng(2.748146, 106.339380),
             new LatLng(2.821887, 106.298886),
             new LatLng(2.855026, 106.198899),
             new LatLng(2.836552, 106.150396),
             new LatLng(2.825774, 106.119004))
             .strokeWidth(2)
             .strokeColor(Color.YELLOW));

        Polygon middleseapolygon = gMap.addPolygon(new PolygonOptions()
        .add(new LatLng(2.817431, 106.135201),
             new LatLng(2.790726, 106.119387),
             new LatLng(2.742071, 106.117457),    
             new LatLng(2.701881, 106.154551),
             new LatLng(2.692536, 106.240001),
             new LatLng(2.709008, 106.292690),
             new LatLng(2.749754, 106.321146),
             new LatLng(2.815021, 106.284109),
             new LatLng(2.843902, 106.198348),
             new LatLng(2.829535, 106.164275),
             new LatLng(2.817431, 106.135201))
             .strokeWidth(2)
             .strokeColor(Color.YELLOW));


    //  inside sea polygon  with hole 
        ArrayList<LatLng> insideseapolygon  = new ArrayList<LatLng>();  
        insideseapolygon.add(new LatLng(2.812380, 106.150146));
        insideseapolygon.add(new LatLng(2.788605, 106.135970));
        insideseapolygon.add(new LatLng(2.745256, 106.140335));
        insideseapolygon.add(new LatLng(2.714304, 106.177117));
        insideseapolygon.add(new LatLng(2.701618, 106.241262));
        insideseapolygon.add(new LatLng(2.712527, 106.286355));
        insideseapolygon.add(new LatLng(2.754506, 106.300315));
        insideseapolygon.add(new LatLng(2.810780, 106.278805));
        insideseapolygon.add(new LatLng(2.831407, 106.199850));
        insideseapolygon.add(new LatLng(2.819866, 106.176925));
        insideseapolygon.add(new LatLng(2.812380, 106.150146));

        ArrayList<LatLng> insideholeseapolygon   = new ArrayList<LatLng>(); 
        insideholeseapolygon.add(new LatLng(2.799302, 106.183565));
        insideholeseapolygon.add(new LatLng(2.775367, 106.200907));
        insideholeseapolygon.add(new LatLng(2.762797, 106.163308));
        insideholeseapolygon.add(new LatLng(2.736975, 106.178228));
        insideholeseapolygon.add(new LatLng(2.730792, 106.228215));
        insideholeseapolygon.add(new LatLng(2.709368, 106.244945));
        insideholeseapolygon.add(new LatLng(2.715907, 106.284297));
        insideholeseapolygon.add(new LatLng(2.765031, 106.278703));
        insideholeseapolygon.add(new LatLng(2.819536, 106.211695));
        insideholeseapolygon.add(new LatLng(2.817278, 106.191385));
        insideholeseapolygon.add(new LatLng(2.799302, 106.183565));

         PolygonOptions polygonOptions = 
           new PolygonOptions()
            .addAll(insideseapolygon)
            .addHole(insideholeseapolygon)
           .strokeColor(Color.YELLOW) 
            .fillColor(Color.argb(127, 56, 56, 245))
            .strokeWidth(2);
         gMap.addPolygon(polygonOptions);
         PolygonOptions options = new PolygonOptions(); 

        initializeUiSettings();
        initializeMapLocationSettings();

          }


    public void initializeUiSettings() {
        gMap.getUiSettings().setCompassEnabled(true);
        gMap.getUiSettings().setRotateGesturesEnabled(false);
        gMap.getUiSettings().setTiltGesturesEnabled(true);
        gMap.getUiSettings().setZoomControlsEnabled(true);
        gMap.getUiSettings().setMyLocationButtonEnabled(true);

    }

    public void initializeMapLocationSettings() {
        gMap.setMyLocationEnabled(true);
    }
    @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;
    }
}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • Looks like you can use the solution in the linked duplicate. Just check if the point is both within Area 3 and not within the Hole area. – Daniel Nugent Jun 28 '17 at 21:22
  • Thanks for the info, I'll check it later. But I want to be detect in area 1, area 2, and area 3. Not just area 3. For example a notification will appear if you in area 2 or 1 or 3. I didn't give area 1 and 2 colors because the hole also covered with overlap colors – AndroidNewbie95 Jun 29 '17 at 06:54

0 Answers0