Android Studio 如何使用谷歌Map中的标记和折线中点绘制多边形

dpiehjr4  于 2023-10-23  发布在  Android
关注(0)|答案(1)|浏览(158)

我想在Map上画一个自由手多边形。我开始与简单的谷歌Map和绘制多边形和它是正常工作,但现在我正在寻找如何用户可以通过点击Map上的点和拉伸标记多边形中点绘制多边形。

现在我的多边形Map看起来像:

我想实现:

这里是我的代码:

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();
        }
    });
  }
 }

我已经做了大量的研究和开发这个主题,但没有得到一个完美的方法来实现这样的事情在谷歌Map。如果有人知道的方式,那么请帮助我找出解决方案。谢谢你提前:)

pes8fvy9

pes8fvy91#

MapDrawingTools库可让您在GoogleMap上轻松绘制多边形、折线和点,并为您的应用检索它们的坐标。它非常适合需要收集多个点或使用用户提供的数据概述土地面积的应用程序。

使用指南

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);

绘制完元素并点击一个done,数据就会返回到你的活动中

@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

相关问题