【问题标题】:how to draw polygon using Markers and midpoints of polylines in google maps如何在谷歌地图中使用标记和折线的中点绘制多边形
【发布时间】:2020-06-25 11:01:06
【问题描述】:

我想在地图上绘制一个手绘多边形。我从简单的谷歌地图开始并绘制多边形,它工作正常,但现在我正在寻找用户如何通过单击地图上的点并在中间拉伸标记来绘制多边形多边形上的点。

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

我想实施:

这是我的代码:

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

我在这个主题上做了很多研究和开发,但没有找到一个完美的方法来在谷歌地图中实现这样的事情。如果有人知道这种方法,那么请帮助我找出解决方案。提前谢谢你:)

【问题讨论】:

    标签: android-studio google-maps polygon draw


    【解决方案1】:

    使用 MapDrawingTools 库在 Google Map 中绘制多边形、折线和点,并将坐标返回到您的应用程序。该库对于选择多个点或绘制土地边界以从用户那里获取数据的应用程序很有用。

    使用指南

    在您的应用中添加以下代码:

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

    绘制元素并点击完成后,数据将返回到您的活动中

     @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

    快乐编码:)

    【讨论】:

    • 我从 github 下载 MapDrawingTools 代码并运行,但它显示 error: package rx.functions does not exist 这个错误
    • 是的,我明白了。直接使用它。我希望它对您的案例实现有用 'com.github.bkhezry:MapDrawingTools:1.1.3'
    • 请这样使用。从库中复制所有包并添加到您的项目中。然后在 Gradle 上添加这个库。实现'io.reactivex:rxjava:1.3.0'
    • 这不是我想要的。我正在尝试绘制可编辑的多边形。
    猜你喜欢
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    相关资源
    最近更新 更多