getLastKnownLocation()始终返回null,所有提供者都是DummyLocationProvider
我看过很多关于这个问题的post,但没有一个为我提供解决方案。
我试图通过GPS(或最终,任何其他有效的提供商)获得用户位置。 但我总是得到零位置。 我确实得到了一张地图,并且可以设置一个任意位置,是不起作用的提供者。
我正确设置了我的MAPS键 (也检查了它是正确的),所有必要的权限设置( android.permission.INTERNET
, android.permission.ACCESS_COARSE_LOCATION
, android.permission.ACCESS_FINE_LOCATION
),事实上,所有似乎都在工作昨天很好!
我尝试使用此代码获取所有可能的提供程序:
List 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(); }
我的清单文件:
试试这段代码:
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 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 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; }
}
布局编码:
获取自己的API密钥流程的链接:
http://sanathnandasiri.blogspot.in/2011/04/obtaining-google-maps-api-key-for.html
//从位置管理器获取当前位置(最后一个已知位置)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);
第一次位置为空,因此设置当前位置是最后一个位置。
- 无法在Android 4.4 KitKat上快速关闭ChunkedInputStream
- 通过Jsoup post方法登录网站无效
- Android camera2 api galaxy s7
- 来自URL的Android位图始终为null
- Volley JsonObjectRequest发布请求忽略参数
- ADT Eclipse SDK的麻烦 – 没有找到DDMS
- 我的Android应用程序中由Theme.AppCompat引起的IllegalStateException
- 找不到类’android.graphics.drawable.RippleDrawable’,无法执行android的方法:onClick,Android Studio
- 片段和baseadapter之间的Android通信