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

时间:2017-04-17 16:12:06

标签: android google-maps geolocation gps marker

我想要一个标记来显示我当前的位置。添加所需的所有权限。当我发表评论mMap.addMarker和mMap.moveCamera时,应用程序正在运行并显示Googlemaps。如果我在我的代码中让这两个中的一个在地图打开之前应用程序崩溃。

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

你们有什么想法让我的应用程序有效吗?

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

}

// ----------------------------助手方法--------------- --------------------------------

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

}

1 个答案:

答案 0 :(得分:3)

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

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

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

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

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

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

                    }
                }


        );

这种方式没有问题。