2017-11-25 146 views
-4

我正在製作一個應用程序來獲取用戶位置並將其顯示在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)); 
} 

}

+3

androidhive的另一個受害者所謂的教程 – Selvin

+1

https://stackoverflow.com/a/43082164/115145 – CommonsWare

+0

@Selvin這是肯定的。非常棒。 – ADM

回答

0

有了新的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" /> 

希望它有幫助!

0
  1. 嘗試更新谷歌Play服務

  2. 添加到搖籃,classpath 'com.google.android.gms:play-services-location:11.8.0'

  3. maven { url "https://maven.google.com" },添加此allprojects->倉庫內

  4. 添加implementation 'com.google.android.gms:play-services-location:11.8.0'內的build.gradle的依賴條件(Modeule.app)