Android Google maps locationlistener – adding a tag on onlocationchanged will crash the application

I want a tag to show my current location. Add all the permissions required. When I comment out mmap.addmarker and mmap.movecamera, the application is running and displays Google maps. If I use one of these two in my code, the application will crash before the map is opened

I tried to delete non - null tags, but that didn't solve the problem

Do you know how I can make this application work?

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

}

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

}

resolvent:

OK, I have solved the problem. So I publish the solution here

I have implemented the locationlistener interface on mapsactivity. For some reason, it cannot work in this way. I can retrieve geographic coordinates, but as long as I want to move the camera or add a tag, the application will crash when I open it

I don't know why, but:

Locationmanager.requestlocationupdates (locationmanager.gps_provider, 3000, 5, this)

I undo the implementation of locationlistener and just create a new one at the "this" position:

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

                    }
                }


        );

In this way, it can work without problem

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>