Java getLastKnownLocation()始终返回null,所有提供程序都是DummyLocationProvider

Java getLastKnownLocation()始终返回null,所有提供程序都是DummyLocationProvider,java,android,maps,location,Java,Android,Maps,Location,我看过很多关于这个问题的帖子,但是没有一个能为我提供解决方案 我试图通过GPS(或最终通过任何其他有效提供商)获取用户位置。但我总是得到空位置。我确实得到了一张地图,并且可以设置任意的位置,这是不起作用的提供者 我已经正确设置了我的地图键(还检查了它是否正确),设置了所有必要的权限(android.permission.INTERNET,android.permission.ACCESS\u rough\u LOCATION,android.permission.ACCESS\u FINE\u

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

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

我已经正确设置了我的地图键(还检查了它是否正确),设置了所有必要的权限(
android.permission.INTERNET
android.permission.ACCESS\u rough\u LOCATION
android.permission.ACCESS\u FINE\u LOCATION
),事实上,昨天一切似乎都很好

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

List<String> providers = locManager.getAllProviders();
for (String provider : providers) {
    printProvider(provider);
}
我的清单文件:


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

用这个代替上面的线

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

第一次位置为空,所以将当前位置设置为最后一个位置

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

用这个代替上面的线

          location = locManager.getLastKnownLocation(bestProvider);
        locManager.requestLocationUpdates(bestProvider,0, 0, locationListener);
        location = locManager.getLastKnownLocation(bestProvider);
        locManager.requestLocationUpdates(bestProvider,0, 0, locationListener);
第一次位置为空,所以将当前位置设置为最后一个位置

请尝试以下代码:

  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;
    }
import java.io.IOException;
导入java.util.List;
导入java.util.Locale;
导入android.content.Context;
导入android.graphics.Canvas;
导入android.location.Address;
导入android.location.Geocoder;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.widget.Toast;
导入com.google.android.maps.GeoPoint;
导入com.google.android.maps.MapActivity;
导入com.google.android.maps.MapController;
导入com.google.android.maps.MapView;
导入com.google.android.maps.Overlay;
公共类MyMapsActivity扩展了MapActivity
{    
地图视图;
地图控制器;
地点经理地点经理;
LocationListener LocationListener;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView=(mapView)findViewById(R.id.mapView);
//默认情况下启用街景
mapView.setStreetView(真);
//启用以显示卫星视图
//mapView.setSatellite(真);
//启用以在地图上显示流量
//mapView.setTraffic(true);
mapView.SetBuilTinZoomControl(真);
mapController=mapView.getController();
mapController.setZoom(5);
locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
locationListener=新的GPSLocationListener();
locationManager.RequestLocationUpdate(
LocationManager.GPS_提供程序,0,0,locationListener);
Touchy t=新Touchy();
List overlayList=mapView.getOverlays();
叠加列表。添加(t);
}
@凌驾
受保护的布尔值isRouteDisplayed(){
//TODO自动生成的方法存根
返回false;
}
类Touchy扩展覆盖
{
公共布尔onTap(地理点,地图视图,地图视图)
{
Context contexto=mapView.getContext();
String msg=“Latitude:”+point.getLatitudeE6()/1E6+“-“+”经度:“+point.getLongitudeE6()/1E6;
Toast Toast=Toast.makeText(contexto、msg、Toast.LENGTH\u SHORT);
toast.show();
返回true;
}
}
私有类GPSLocationListener实现LocationListener
{
已更改位置上的公共无效(位置)
{
如果(位置!=null)
{
地质点=新的地质点((int)(location.getLatitude()*1E6),(int)(location.getLatitude()*1E6));
Toast.makeText(getBaseContext(),“纬度:”+location.getLatitude()+“经度:”+location.getLatitude(),
吐司。长度(短)。show();
mapController.animateTo(点);
mapController.setZoom(5);
mapView.invalidate();
}
如果(位置!=null)
{
地球点=零;
字符串地址=转换点位置(点);
Toast.makeText(getBaseContext(),地址,Toast.LENGTH_SHORT).show();
}
}
公共字符串转换点定位(地理点){
字符串地址=”;
Geocoder Geocoder=新的地理编码器(getBaseContext(),Locale.getDefault());
试一试{
列表

请尝试以下代码:

  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;
    }
import java.io.IOException;
导入java.util.List;
导入java.util.Locale;
导入android.content.Context;
导入android.graphics.Canvas;
导入android.location.Address;
导入android.location.Geocoder;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.widget.Toast;
导入com.google.android.maps.GeoPoint;
导入com.google.android.maps.MapActivity;
导入com.google.android.maps.MapController;
导入com.google.android.maps.MapView;
导入com.google.android.maps.Overlay;
公共类MyMapsActivity扩展了MapActivity
{    
地图视图;
地图控制器;
    <?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>