gpt4 book ai didi

Android - 谷歌地图 - LocationListener - 添加标记 onLocationChanged 使应用程序崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 13:45:22 24 4
gpt4 key购买 nike

我想要一个标记来显示我当前的位置。添加所需的所有权限。当我注释掉 mMap.addMarker 和 mMap.moveCamera 时,该应用程序正在运行并显示了 Googlemaps。如果我在我的代码中使用这两个中的一个,应用程序甚至在 map 打开之前就崩溃了。

如果标记不为空,我尝试删除它,但这并不能解决问题。

你们知道我怎样才能让应用正常运行吗?

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener {


private GoogleMap mMap;
private List<LatLng> fountain = null;
private LocationManager locationManager;
private double posLat;
private double posLng;

private LatLng position;
private Marker mPosition;


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
startGPS();


}

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

// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(48.16786112327462, 16.383984438313828);
mPosition = mMap.addMarker(new MarkerOptions().position(sydney).title("Your Position").icon(BitmapDescriptorFactory.fromResource(R.drawable.location)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));


}


//--------------------------------------------GPS Listener---------------------------------------
public void startGPS() {

if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 5);
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 5, this);
onLocationChanged(locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER));

}

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

} else {
getDialog2("Keine Erlaubnis für GPS").show();
}
}
}
}


@Override
public void onLocationChanged(Location location) {
posLat = location.getLatitude();
posLng = location.getLongitude();

position = new LatLng(posLat, posLng);

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

mPosition = mMap.addMarker(new MarkerOptions().position(position).title("Your position").
icon(BitmapDescriptorFactory.fromResource(R.drawable.location)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 11));

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

//----------------------------Helper Methods---------------- --------------------------------

public Dialog getDialog2(String string) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(string);
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override

public void onClick(DialogInterface dialog, int which) {
finish();
}
});
return builder.create();
}


public Dialog getDialog(String string) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(string);
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return builder.create();
}

最佳答案

好的,我已经解决了这个问题。所以我在这里发布解决方案。

我已经在 MapsActivity 上实现了 LocationListener 接口(interface),但出于某种原因,它不能以这种方式工作。我可以检索地理坐标,但只要我想移动相机或添加标记,应用程序就会在打开时崩溃。

我不知道为什么,但不是:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 5, this)

我撤消了 LocationListener 的实现,只是在 ,,this"的位置创建了一个新的:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 5, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
posLat = location.getLatitude();
posLng = location.getLongitude();

position = new LatLng(posLat, posLng);

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

mPosition = mMap.addMarker(new MarkerOptions().position(position).title("Your position").
icon(BitmapDescriptorFactory.fromResource(R.drawable.location)));

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 11));

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
}


);

这样它就没有问题了。

关于Android - 谷歌地图 - LocationListener - 添加标记 onLocationChanged 使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43455134/

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