如何在棉花糖中显示经纬度?

How to show latitude and longitude in marshmallow?

我正在做一个数据以列表形式显示的应用程序。当用户点击列表项时,会显示特定列表项的详细信息。 到目前为止,我能够在我的应用程序中显示所有数据。但是我在 google 地图中遇到问题,我必须在其中显示位置。当我的应用程序第一次启动时,我可以在地图中看到位置 (Image),but when my application is launched second time i cant see the location in map(Image)。我完全陷入困境,我没有明白我做错了什么。请指导我解决这个问题。

onCreate

mFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mFragment.getMapAsync(this);

mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addApi(LocationServices.API)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();
mGoogleApiClient.connect();

mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(5000); //5 seconds
mLocationRequest.setFastestInterval(3000); //3 seconds
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

我这样做是为了在地图上显示位置

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onResume() {
    super.onResume();
    if(mGoogleApiClient.isConnected()){
        requestLocationUpdates();
    }
}

@Override
protected void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();
}



@Override
public void onMapReady(GoogleMap googleMap) {




    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

            mGoogleMap = googleMap;

            LatLng latlng = new LatLng(lat, lon);
            mGoogleMap.addMarker(new MarkerOptions().position(latlng).title("Marker"));
            mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));


    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_FINE_LOCATION);
        }
    }

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case MY_PERMISSION_FINE_LOCATION:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

                    requestLocationUpdates();

                }
            } else {
                Toast.makeText(Property_Detail.this, "App Require Permission", Toast.LENGTH_SHORT).show();
                finish();
            }

            break;
    }

}


@Override
public void onConnected(Bundle bundle) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {


    requestLocationUpdates();

    }

}

@Override
public void onConnectionSuspended(int i) {
    Toast.makeText(this,"onConnectionSuspended",Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Toast.makeText(this,"onConnectionFailed",Toast.LENGTH_SHORT).show();
}

private void requestLocationUpdates() {

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

        mGoogleMap.clear();
        latLng = new LatLng(lat, lon);
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title("Position");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
        mCurrLocation = mGoogleMap.addMarker(markerOptions);
    }

}


@Override
public void onLocationChanged(Location location) {


    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        myLatitude = lat;
        myLongitude = lon;

    }

}

在准备好地图中的新 LatLng 调用上方放置一个日志或 toast,并使用调试器查看何时调用地图以及那里的位置值是什么,我认为您不会在每次需要或刷新时加载地图,有一个方法叫做

每当您需要重新加载地图时调用它

 my_map_fragment.getMapAsync(this);