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

时间:2016-11-17 07:20:00

标签: android android-6.0-marshmallow android-maps-v2

我正在申请中,以列表的形式显示数据。当用户点击列表项时,显示特定列表项的详细信息。  直到现在我能够显示我的应用程序中的所有数据。但我在谷歌地图中遇到问题,我必须显示位置。当我的应用程序第一次启动时,我可以在地图中看到位置(Image),但是当我的应用程序第二次启动时,我无法在地图中看到该位置(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;

    }

}

1 个答案:

答案 0 :(得分:2)

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

的方法

每当您需要重新加载地图时都会调用此方法

 my_map_fragment.getMapAsync(this);