我的getLocation方法总是返回null
问题描述:
我正在制作一个应用程序来获取用户位置并将其显示在Google地图上,但由于getLocation方法返回null值,应用程序总是崩溃,任何人都可以告诉我什么是问题?我的getLocation方法总是返回null
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.getyourlocation">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
,这是创建用户
package com.example.android.getyourlocation;
import android.Manifest;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.ContextCompat;
import android.util.Log;
/**
* Created by Hazem_Khaled on 2017-11-24.
*/
public class GpsManager extends Service implements LocationListener {
private final Context context;
protected LocationManager mLocationManager;
boolean isGpsEnabled;
boolean isNetworkEnable;
boolean canGetLocation;
Location mLocation;
public GpsManager(Context context) {
this.context = context;
this.isGpsEnabled=false;
this.isNetworkEnable=false;
this.canGetLocation=false;
}
public Location getLocation(){
try {
mLocationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
//to find Location via GPS
String provider=LocationManager.GPS_PROVIDER;
isGpsEnabled=mLocationManager.isProviderEnabled(provider);
//to find Location via Network
provider=LocationManager.NETWORK_PROVIDER;
isNetworkEnable=mLocationManager.isProviderEnabled(provider);
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
//process to find Location via GPS
if(isGpsEnabled){
if(mLocation == null){
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000,10,this);
if (mLocationManager != null)
mLocation=mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
//if mLocation still null this means that user location could not be found via GPS then we will use the Network to get user Location
if(mLocation == null){
if(isNetworkEnable){
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,10000,10,this);
if (mLocationManager != null)
mLocation=mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
}
}catch (Exception e){
Log.e("Error : ","in getLocation Method");
e.printStackTrace();
}
return mLocation;
}
public IBinder onBind(Intent intent) {
return null;
}
public void onLocationChanged(Location location) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
}
获得位置我GpsManager.java类,这是我的MapsActivity.java
package com.example.android.getyourlocation;
import android.location.Location;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements
OnMapReadyCallback {
private GoogleMap mMap;
private Location mLocation;
private GpsManager mGpsManager;
double latitude;
double longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
mGpsManager = new GpsManager(getApplicationContext());
mLocation = mGpsManager.getLocation();
latitude = mLocation.getLatitude();
longitude = mLocation.getLongitude();
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng myLocation = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(myLocation).title("I Am Here"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation));
}
}
答
有了新的API,你可以试试这个
的build.gradle
implementation 'com.google.android.gms:play-services-location:11.6.0'
GpsManager.java
public class GpsManager extends LocationCallback {
private FusedLocationProviderClient client;
private Callback callback;
public interface Callback {
void onLocation(Location location);
}
public boolean start(Context context, Callback callback) {
this.callback = callback;
client = LocationServices.getFusedLocationProviderClient(context);
if (!checkLocationPermission(context)) return false;
client.requestLocationUpdates(getLocationRequest(), this, null);
return true;
}
public void stop() {
client.removeLocationUpdates(this);
}
@Override
public void onLocationResult(LocationResult locationResult) {
for (Location location : locationResult.getLocations()) {
callback.onLocation(location);
}
}
private boolean checkLocationPermission(Context context) {
int permissionCheck = ContextCompat.checkSelfPermission(
context, android.Manifest.permission.ACCESS_FINE_LOCATION);
return permissionCheck == PackageManager.PERMISSION_GRANTED;
}
private LocationRequest getLocationRequest() {
return LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(30_000L)
.setFastestInterval(20_000L);
}
}
...一次这样的连接到您的Activity
...
MapsActivity.java
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GpsManager.Callback {
private static final int PERMISSION_REQUEST_FINE_LOCATION = 1;
private GpsManager mGpsManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mGpsManager = new GpsManager(getApplicationContext(), this);
// check if user gave permissions, otherwise ask via dialog
if (!checkPermission()) {
getLocationPermissions();
return;
}
mGpsManager.start();
...
}
@Override
protected void onStop() {
super.onStop();
mGpsManager.stop();
}
@Override
public void onLocation(Location location) {
// do something with your location
}
// CHECK PERMISSIONS PART
private boolean checkPermission() {
return isGranted(ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION)) &&
isGranted(ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION));
}
@TargetApi(Build.VERSION_CODES.M)
private void getLocationPermissions() {
requestPermissions(new String[] {Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSION_REQUEST_FINE_LOCATION);
}
@Override
public void onRequestPermissionsResult(int code, @Nullable String permissions[], @Nullable int[] results) {
switch (code) {
case PERMISSION_REQUEST_FINE_LOCATION:
if (isPermissionGranted(results)) {
getLocationRequest();
}
}
}
private boolean isPermissionGranted(int[] results) {
return results != null && results.length > 0 && isGranted(results[0]);
}
private boolean isGranted(int permission) {
return permission == PackageManager.PERMISSION_GRANTED;
}
}
现在,你必须确保在你的
AndroidManifest.xml中
所有权限<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature android:name="android.hardware.location.network" />
希望它有帮助!
答
尝试更新谷歌Play服务
添加到摇篮,
classpath 'com.google.android.gms:play-services-location:11.8.0'
maven { url "https://maven.google.com" }
,添加此allprojects->仓库内添加
implementation 'com.google.android.gms:play-services-location:11.8.0'
内的build.gradle的依赖条件(Modeule.app)
androidhive的另一个受害者所谓的教程 – Selvin
https://stackoverflow.com/a/43082164/115145 – CommonsWare
@Selvin这是肯定的。非常棒。 – ADM