Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to draw polygon using Markers and midpoints of polylines in google maps

I want to draw a Free Hand Polygon on the Map.I started with simple Google Map & draw polygon & it is working properly but now I am looking for how a user can draw polygon by clicking points on map and stretching markers mid Points on the polygon.

now my map with polygon looks like:

this

and i want to implement :

this

here is my code:

     public class MapActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
Button save_field;


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

    // Retrieve the content view that renders the map.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    FrameLayout Frame_map = (FrameLayout) findViewById(R.id.frame_map);
    Button btn_draw_State = (Button) findViewById(R.id.btn_draw_State);
    final Boolean[] Is_MAP_Moveable = {false}; // to detect map is movable

    // Button will change Map movable state
    btn_draw_State.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Is_MAP_Moveable[0] = !Is_MAP_Moveable[0];
        }
    });
}

public GoogleMap getmMap() {
    return mMap;
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    /*polygon should be declared as member of the fragment class if you want just one polygon at a time*/
    final List<LatLng> latLngList = new ArrayList<>(); // list of polygons
    final List<Marker> markerList = new ArrayList<>();

    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(final LatLng latLng) {


            MarkerOptions markerOptions = new MarkerOptions(); //create marker options
            markerOptions.position(latLng);
            markerOptions.title(latLng.latitude + ":" + latLng.longitude);
            mMap.clear();
            mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
            mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
            Marker marker = mMap.addMarker(markerOptions);
            latLngList.add(latLng);
            markerList.add(marker);


            Polygon polygon = null;
            if (polygon != null ) polygon.remove(); // remove the previously drawn polygon
            PolygonOptions polygonOptions = new PolygonOptions().addAll(latLngList).clickable(true);
            polygon = mMap.addPolygon(new PolygonOptions().addAll(latLngList).fillColor(Color.BLUE).strokeColor(Color.RED));//add new polygon

        }
    });
             save_field = findViewById(R.id.save);
             save_field.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            startActivity(new Intent(MapActivity.this, Save_Fields.class));
            finish();
        }
    });
  }
 }

I have done lots of Research and Development on this topic but didn't get a perfect way to implement such a thing in google Maps.if anyone know the way then please help me to find out solution. thank you in advance:)

like image 299
Mrunal Avatar asked Mar 13 '20 12:03

Mrunal


People also ask

How do I import a polygon into Google Maps?

The answer is no, an encoded polygon cannot be imported. For a shape to be imported to My Maps, it must first be converted to a KML file. BlueCollar laid out the first step, which is to use Google's encoding API to decode the encoded shapes into Lat/Lng pairs. The KML file can then be imported.


1 Answers

Use MapDrawingTools library to drawing polygon, polyline and points in Google Map and return coordinates to your App. This library is useful for an application that picks multiple points or drawing the border of land to get data from users.

Use Guideline

in your app add this code:

DrawingOption.DrawingType currentDrawingType = DrawingOption.DrawingType.POLYGON;
Intent intent =
new DrawingOptionBuilder()
    .withLocation(35.744502, 51.368966)
    .withMapZoom(14)
    .withFillColor(Color.argb(60, 0, 0, 255))
    .withStrokeColor(Color.argb(100, 255, 0, 0))
    .withStrokeWidth(3)
    .withRequestGPSEnabling(false)
    .withDrawingType(currentDrawingType)
    .build(getApplicationContext());
startActivityForResult(intent, REQUEST_CODE);

After drawing element and click on a done, data will be a return to your activity

 @Override
protected void onActivityResult(int requestCode, int resultCode, 
Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE && data != 
null) {
DataModel dataModel =
                data.getExtras().getParcelable(MapsActivity.POINTS);
LatLng[] points=dataModel.getPoints();
 }
}

MapDrawingTools

Youtube Demo

Happy Coding :)

like image 168
Daxesh Vekariya Avatar answered Oct 12 '22 01:10

Daxesh Vekariya