gpt4 book ai didi

android - 无法在谷歌地图上绘制多边形

转载 作者:行者123 更新时间:2023-12-04 10:23:46 26 4
gpt4 key购买 nike

我创建了应用程序,我试图在 Google map 上的当前位置绘制多边形。但因为它没有抛出任何错误,我不明白为什么但我无法绘制多边形。如果有人知道我的代码有什么问题,我真的需要帮助,请帮助我:(

这是我的 map Activity ,我在其中编写了绘制多边形的代码:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

Location currentLocation;
private GoogleMap googleMap;
private Polygon polygon;
private ArrayList<LatLng> arrayPoints = null;
FusedLocationProviderClient fusedLocationProviderClient;
private static final int REQUEST_CODE = 101;
private boolean checkClick = false;

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

fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
fetchLastLocation();

}

private void fetchLastLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_CODE);
return;
}
Task<Location> task = fusedLocationProviderClient.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {

if (location != null){
currentLocation = location;
Toast.makeText(getApplicationContext(),currentLocation.getLatitude() +""+ currentLocation.getLongitude(),Toast.LENGTH_LONG).show();
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(MapsActivity.this);
}
}
});
}

public GoogleMap getmMap() {
return mMap;

}

@Override
public void onMapReady(final GoogleMap googleMap) {

final List<LatLng> latLngList = new ArrayList<>(); // list of polygons
final List<Marker> markerList = new ArrayList<>();
mMap = googleMap;

LatLng latLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
MarkerOptions markerOptions = new MarkerOptions().position(latLng).title(latLng.latitude + ":" + latLng.longitude);
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
googleMap.addMarker(markerOptions);
Marker marker =googleMap.addMarker(markerOptions);
markerList.add(marker);
latLngList.add(latLng);

mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

@Override
public void onMapClick(final LatLng latLng) {

if (polygon != null) {
polygon.remove();
}

PolygonOptions polygonOptions = new PolygonOptions();
polygonOptions.fillColor(Color.CYAN);
polygonOptions.strokeColor(Color.BLUE);
polygonOptions.strokeWidth(5);
polygonOptions.addAll(latLngList);
polygon = mMap.addPolygon(polygonOptions);

}
});
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case REQUEST_CODE:
if (grantResults.length> 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
fetchLastLocation();
}
}
}
}

最佳答案

使用此代码:

@Override
public void onMapReady(GoogleMap googleMap) {

final List<LatLng> latLngList = new ArrayList<>(); // list of polygons
final List<Marker> markerList = new ArrayList<>();
mMap = googleMap;

LatLng center = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
final MarkerOptions markerOptions = new MarkerOptions().position(center).title(center.latitude + ":" + center.longitude);
mMap.clear();
googleMap.animateCamera(CameraUpdateFactory.newLatLng(center));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(center, 10));
googleMap.addMarker(markerOptions);

mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

@Override
public void onMapClick(final LatLng latLng) {

Marker marker = mMap.addMarker(new MarkerOptions().position(latLng).draggable(true));
markerList.add(marker);
latLngList.add(latLng);

if (polygon != null) {
polygon.remove();
}

PolygonOptions polygonOptions = new PolygonOptions();
polygonOptions.fillColor(Color.CYAN);
polygonOptions.strokeColor(Color.BLUE);
polygonOptions.strokeWidth(5);
polygonOptions.addAll(latLngList);
polygon = mMap.addPolygon(polygonOptions);

}
});
}

关于android - 无法在谷歌地图上绘制多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60720603/

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