为什么我无法获得经纬度? 我已经检查了许可。问题出在哪里?
public class MapLocationActivity extends AppCompatActivity {
private ImageView image_map;
private Context mcontext = MapLocationActivity.this;
static final int REQUEST_LOCATION = 1;
LocationManager locationManager;
double longitude;
double latitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_location);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
getLocation();
image_map = (ImageView) findViewById(R.id.image_map);
image_map.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
image_map.getViewTreeObserver().removeOnGlobalLayoutListener(this);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
View v = (View) findViewById(R.id.image_map);
StaticMap map = new StaticMap()
.center(new StaticMap.GeoPoint(latitude, longitude))
.size(v.getWidth() / 2, v.getHeight() / 2)
.zoom(18)
.scale(2).marker(new StaticMap.GeoPoint(latitude, longitude));
try {
Picasso.with(mcontext)
.load(String.valueOf(map.toURL()))
.into(image_map);
} catch (MalformedURLException e) {
}
image_map.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
void getLocation(){
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
(this,Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String [] {Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_LOCATION);
}else{
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location !=null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}else{
Toast.makeText(this,"Unable to find location",Toast.LENGTH_LONG).show();
}
}
}
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResult){
super.onRequestPermissionsResult(requestCode,permissions,grantResult);
switch (requestCode){
case REQUEST_LOCATION:
getLocation();
break;
}
}
}
<permission
android:name="com.example.root.a1718_it114105_b2_mayihelpyoumum.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="com.weaverprojects.gpstest.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service android:name=".FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Login" />
<activity android:name=".DhMainActivity" />
<activity android:name=".MainAdminActivity" />
<activity android:name=".location" />
<activity android:name=".Register" />
<activity android:name=".employerRegister" />
<activity android:name=".dhResetPassword" />
<activity android:name=".addReceipt" />
<activity android:name=".viewToDoList" />
<!-- <activity android:name=".manageToDoList"></activity> -->
<activity android:name=".editToDoList" />
<activity android:name=".addToDoList" />
<activity android:name=".showRemovedToDoList" />
<activity android:name=".createTodoList" />
<activity android:name=".dailyworkTodoList" />
<activity android:name=".shoppingTodoList" />
<activity android:name=".ChatStartActivity" />
<activity android:name=".ChatMainActivity" />
<activity
android:name=".ChatRegisterActivity"
android:parentActivityName=".ChatStartActivity" />
<activity
android:name=".ChatLoginActivity"
android:parentActivityName=".ChatStartActivity" />
<activity android:name=".ChatSettingsActivity" />
<activity
android:name=".ChatStatusActivity"
android:parentActivityName=".ChatSettingsActivity" />
<activity
android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat" />
<activity
android:name=".ChatUsersActivity"
android:parentActivityName=".ChatMainActivity" />
<activity android:name=".ChatProfileActivity">
<intent-filter>
<action android:name="com.smartyway.smartchat_TARGET_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".ChatActivity"
android:parentActivityName=".ChatMainActivity" />
<activity android:name=".locationActivity" />
<activity android:name=".AndroidGPSTrackingActivity" />
<activity android:name=".MapLocationActivity"></activity>
</application>
我看到了这一点。
W /设置:设置airplane_mode_on已从android.provider.Settings.System移至android.provider.Settings.Global,返回只读值。