【问题标题】:getLastKnownLocation() always returning null, all providers are DummyLocationProvidergetLastKnownLocation() 总是返回 null,所有提供者都是 DummyLocationProvider
【发布时间】:2012-10-05 08:10:34
【问题描述】:

我看过很多关于这个问题的帖子,但没有一个为我提供解决方案。

我正在尝试通过 GPS(或最终通过任何其他有效提供商)获取用户位置。但我总是得到空位置。我确实得到了一张地图,并且可以设置任意位置,是提供者不起作用。

我的 MAPS 密钥设置正确(还检查了它是否正确),所有必要的权限设置(android.permission.INTERNETandroid.permission.ACCESS_COARSE_LOCATIONandroid.permission.ACCESS_FINE_LOCATION)事实上,所有昨天好像还好好的!

我尝试使用此代码获取所有可能的提供者:

List<String> providers = locManager.getAllProviders();
for (String provider : providers) {
    printProvider(provider);
}

这表明所有提供者(在我的例子中是 4 个)都是 DummyLocationProviders。

我正在尝试在手机上运行我的应用,而不是在模拟器上(虽然我也尝试在模拟器上启用 GPS,但无济于事)。

这是我用来获取位置的代码(昨天再次运行良好):

//Get criteria
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);

        //Get best provider
        String  bestProvider = locManager.getBestProvider(criteria, false);
        printProvider(bestProvider);



        //get the current location (last known location) from the location manager
        Location location = locManager.getLastKnownLocation(bestProvider);

        //if location found display as a toast the current latitude and longitude
        if (location != null) {
            Toast.makeText(this, "Current location:\nLatitude: " + location.getLatitude() + "\n" + "Longitude: " + location.getLongitude(), Toast.LENGTH_LONG).show();

            point = new GeoPoint((int) (location.getLatitude()*1E6),(int)(location.getLongitude() *1E6));
            controller.animateTo(point);
        } else {
            Toast.makeText(this, "Cannot fetch current location!", Toast.LENGTH_LONG).show();
        }

我的清单文件:

 <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="10" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".activities.MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <uses-library android:name="com.google.android.maps" />

        <activity
            android:name=".activities.GoogleMapsActivity"
            android:label="@string/title_activity_maps" >
        </activity>

        <activity
            android:name=".activities.MyMapsActivity"
            android:label="@string/title_activity_maps" >
        </activity>
</application>
</manifest>

【问题讨论】:

    标签: java android maps location


    【解决方案1】:

    试试这个代码:

      import java.io.IOException;
      import java.util.List;
      import java.util.Locale;
    
      import android.content.Context;
      import android.graphics.Canvas;
      import android.location.Address;
      import android.location.Geocoder;
      import android.location.Location;
      import android.location.LocationListener;
      import android.location.LocationManager;
      import android.os.Bundle;
      import android.widget.Toast;
    
      import com.google.android.maps.GeoPoint;
      import com.google.android.maps.MapActivity;
      import com.google.android.maps.MapController;
      import com.google.android.maps.MapView;
      import com.google.android.maps.Overlay;
    
      public class MyMapsActivity extends MapActivity 
       {    
    
            MapView mapView;
            MapController mapController;
            LocationManager locationManager;
            LocationListener locationListener;
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState)
                 {
                   super.onCreate(savedInstanceState);
                   setContentView(R.layout.main);
    
                    mapView = (MapView) findViewById(R.id.mapView);
                    // enable Street view by default
                    mapView.setStreetView(true);
    
                    // enable to show Satellite view
                   // mapView.setSatellite(true);
    
                  // enable to show Traffic on map
                   // mapView.setTraffic(true);
    
                    mapView.setBuiltInZoomControls(true);
    
                    mapController = mapView.getController();
                    mapController.setZoom(5); 
    
    
          locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
          locationListener = new GPSLocationListener();
          locationManager.requestLocationUpdates(
          LocationManager.GPS_PROVIDER, 0, 0, locationListener);
          Touchy t = new Touchy();
          List<Overlay> overlayList = mapView.getOverlays();
          overlayList.add(t);
          }
         @Override
          protected boolean isRouteDisplayed() {
            // TODO Auto-generated method stub
              return false;
           }
    
    
          class Touchy extends Overlay
          {
            public boolean onTap(GeoPoint point, MapView mapView) 
               {
                Context contexto = mapView.getContext();
                String msg = "Latitude : " + point.getLatitudeE6()/1E6 + " - " + "Longitude : " + point.getLongitudeE6()/1E6;
    
                 Toast toast = Toast.makeText(contexto, msg, Toast.LENGTH_SHORT);
                 toast.show();
    
                return true;
              }
           }
    
    
          private class GPSLocationListener implements LocationListener 
             {
              public void onLocationChanged(Location location)
                {
                 if (location != null)
                  {
                   GeoPoint point = new GeoPoint((int) (location.getLatitude() * 1E6),(int) (location.getLongitude() * 1E6));
    
                   Toast.makeText(getBaseContext(),"Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude(), 
                   Toast.LENGTH_SHORT).show();
    
             mapController.animateTo(point);
             mapController.setZoom(5);
             mapView.invalidate();
           }
    
              if (location != null)
                 {
                   GeoPoint point=null;
                   String address = ConvertPointToLocation(point);
                   Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
    
                 }
             }
    
            public String ConvertPointToLocation(GeoPoint point) {   
                   String address = "";
                   Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
                   try {
                         List<Address> addresses = geoCoder.getFromLocation(point.getLatitudeE6()  / 1E6, 
                         point.getLongitudeE6() / 1E6, 1);
    
               if (addresses.size() > 0) {
                      for (int index = 0; 
                        index < addresses.get(0).getMaxAddressLineIndex(); index++)
                        address += addresses.get(0).getAddressLine(index) + " ";
                    }
                }
            catch (IOException e) {        
                        e.printStackTrace();
                 }   
    
         return address;
        }
    

    }

    布局编码:

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout 
              xmlns:android="http://schemas.android.com/apk/res/android" 
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent">
    
         <com.google.android.maps.MapView 
                  android:id="@+id/mapView"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:enabled="true"
                  android:clickable="true"
                  android:apiKey="Your MAP API Key"/>
    
         <LinearLayout android:id="@+id/zoom" 
                 android:layout_width="wrap_content" 
                 android:layout_height="wrap_content" 
                 android:layout_alignParentBottom="true" 
                 android:layout_centerHorizontal="true" />
    
         </RelativeLayout>
    

    获取自己的 API 密钥流程的链接:

    http://sanathnandasiri.blogspot.in/2011/04/obtaining-google-maps-api-key-for.html

    【讨论】:

    • 你能贴出你的logcat截图吗?
    • 我有。我当前的纬度和经度没有得到任何祝酒,这是我应该从您的代码中得到的。
    • 我认为,关于您的 Map API 密钥的问题。检查。
    • 将 setContentView(R.layout.main) 更改为 setContentView(R.layout.mapview)
    【解决方案2】:

    //从位置管理器获取当前位置(最后已知位置) 位置 location = locManager.getLastKnownLocation(bestProvider);

    用这个代替上面的行

              location = locManager.getLastKnownLocation(bestProvider);
            locManager.requestLocationUpdates(bestProvider,0, 0, locationListener);
            location = locManager.getLastKnownLocation(bestProvider);
            locManager.requestLocationUpdates(bestProvider,0, 0, locationListener);
    

    第一次位置为空,所以设置你的当前位置为最后一次位置。

    【讨论】:

    • 我仍然遇到同样的问题,正如我所说,我认为 Android 没有正确检测到我的提供程序,因为它们都被列为 DummyLocationProviders
    • 我想你试试模拟器,试试移动设备。它的工作!
    • 不,我正在尝试我的 Galaxy Nexus。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-06
    • 2013-12-24
    • 1970-01-01
    • 2012-05-28
    相关资源
    最近更新 更多