0
我正在應用程序中以列表的形式顯示數據。當用戶點擊列表項時,會顯示特定列表項的詳細信息。直到現在我能夠顯示我的應用程序中的所有數據。但我在谷歌地圖中遇到問題,我必須顯示位置。當我的應用程序第一次啓動時,我可以看到地圖中的位置(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;
}
}
我猜你應該c中的範圍變量lat和長使用的是在你的地圖準備 – Ak9637
當過應用程序開始我Lat Long網檢查土司,和我得到的值 –
嘗試顯示的答案地圖下面,當你想首先加載你的地圖時解決 – Ak9637