gpt4 book ai didi

android - 如何在 Android 的谷歌地图中绘制徒手多边形?

转载 作者:行者123 更新时间:2023-11-30 04:56:40 25 4
gpt4 key购买 nike

我想在android studio的 map 上徒手画一个多边形,但我不知道该怎么做我已经有 map 了公共(public)类 MapsActivity 扩展 FragmentActivity 实现 OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}


/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}}

最佳答案

如果您所说的“免提”指的是用户触摸 map 创建的多边形,我建议您拦截 map 对象上的触摸或点击事件,并获取准确点击位置的 LatLng。然后将该点添加到多边形的点列表中。您可以随意决定:每次有新的点击或您想到的其他事情时重新绘制多边形。

在下面的示例中,我拦截点击并为每次新点击使用当前存储的点重新绘制多边形。我可以决定拥有多少个多边形,但我现在只想要一个。

@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> latLngs = new ArrayList<>(); // list of polygons
final GoogleMap X = this.mMap; // the map

this.googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
latLngs.add(latLng);//add the point to the list
if (polygon != null ) polygon.remove(); // remove the previously drawn polygon
polygon = X.addPolygon(new PolygonOptions().addAll(latLngs).fillColor(Color.BLUE).strokeColor(Color.RED));//add new polygon

}
});
}}

关于android - 如何在 Android 的谷歌地图中绘制徒手多边形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59002576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com