0
Android Marshmallow API 23で緯度と経度の値が0.0になるという問題が発生します。その他のAPIは正常に動作しています。現在の位置情報を取得していないGoogleマップに問題があります。Android Marshmallow API 23で緯度と経度が表示されない
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(GPSTracker.this);
}
}
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
*
* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog On pressing Settings button will
* lauch Settings Options
* */
public void showSettingsAlert() {
final Dialog dialog = new Dialog(mContext,android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.upgrade_now_layout);
LinearLayout upgradeParent = (LinearLayout) dialog
.findViewById(R.id.upgradeParent);
LinearLayout laterParent = (LinearLayout) dialog.findViewById(R.id.laterParent);
TextView title = (TextView) dialog.findViewById(R.id.title);
TextView discription = (TextView) dialog.findViewById(R.id.discription);
TextView yes = (TextView) dialog.findViewById(R.id.yes);
TextView no = (TextView) dialog.findViewById(R.id.no);
title.setVisibility(View.GONE);
discription.setText(" GPS is not enabled. Do you want to go to settings menu? \n");
yes.setText("Setting");
no.setText("Cancel");
laterParent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Splash.again = true;
dialog.dismiss();
}
});
upgradeParent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// super.onBackPressed();
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
Splash.again = true;
dialog.dismiss();
}
});
dialog.show();
}
アンドロイドたManifest.xml
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION"/>
<メタデータ アンドロイド:名= "com.google.android.maps.v2.API_KEY" アンドロイド:値= "YOUR_KEY" />はい私はこれを使用するが、 –
を得ていない、あなたを持っています許可の設定を有効にしますか? –
yes.myコードはAndroid版で動作しています。私はこのエラー23 APIを取得しています。 –