我正在製作一個應用程序來獲取用戶位置並將其顯示在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));
}
}
androidhive的另一個受害者所謂的教程 – Selvin
https://stackoverflow.com/a/43082164/115145 – CommonsWare
@Selvin這是肯定的。非常棒。 – ADM