2015-11-27 93 views
2

我想放大地圖上用戶的當前位置。該位置是絕對可訪問的,因爲它在地圖上顯示爲藍色點 enter image description hereGoogleMap.getMyLocation()返回null

但是,GoogleMap.getMyLocation()返回null。

爲了啓用位置數據,我遵循Google documentation並在我的清單中添加了適當的權限,此外還添加了必要的API密鑰。

如果是在我所有的MyActivity.java有所幫助:

package com.example.beckah.helloworld; 

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; 
import android.location.Location; 
import android.location.LocationListener; 
import android.content.Context; 
import android.location.LocationManager; 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap mMap; 
    private LatLng location; 
    Location mLocation; 
    LocationManager locationManager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     // 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; 

     mMap.setMyLocationEnabled(true); 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     mLocation = googleMap.getMyLocation(); 


     if(mLocation != null) location = new LatLng(mLocation.getLatitude(), mLocation.getLongitude()); 
     else location = new LatLng(31, 31); 

     mMap.addMarker(new MarkerOptions().position(location).title("Marker in Sydney")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(location)); 


    } 
} 

我的清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.beckah.helloworld"> 

    <!-- 
     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.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_GPS" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

    <permission android:name="com.example.beckah.helloworld.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     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> 

</manifest> 

我怎樣才能確保GoogleMap.getMyLocation()沒有返回null,並能獲取位置數據?

+1

該方法已棄用:https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.html#getMyLocation()查看此答案中的代碼: http://stackoverflow.com/a/30255219/4409409 –

回答

7

下面的代碼應該工作對於用戶的最後已知位置

LocationManager service = (LocationManager) 

getSystemService(LOCATION_SERVICE); 
Criteria criteria = new Criteria(); 
String provider = service.getBestProvider(criteria, false); 
Location location = service.getLastKnownLocation(provider); 
LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude()); 
+0

第一次我得到的位置空,你能告訴我該怎麼做當前位置在創建時,我會第一次啓動我的應用程序? –

4

嘗試這種方法添加到您的代碼,以便找到最後已知的位置:

private Location getMyLocation() { 
    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    if (myLocation == null) { 
     Criteria criteria = new Criteria(); 
     criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
     String provider = lm.getBestProvider(criteria, true); 
     myLocation = lm.getLastKnownLocation(provider); 
    } 
    return myLocation; 
} 

然後更換

mLocation = googleMap.getMyLocation(); 

mLocation = getMyLocation();