有没有办法访问GPS一次,而不是有一个不断检查位置更新的looper?
在我的场景中,我感兴趣的是找到当前的坐标而不是与GPS卫星的连续连接.有没有人有任何想法如何做到这一点?提前致谢.
我想找到一个Android项目的当前位置.加载应用程序时,我的当前位置始终为null.我已经在清单等中设置了权限.当我找到当前位置时,我打算使用坐标来查找到地图上其他位置的距离.我的代码片段如下.为什么我总是得到一个空值?
locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
towers = locMan.getBestProvider(crit, false);
location = locMan.getLastKnownLocation(towers);
if (location != null) {
System.out.println("Location is not null!");
lat = (int) (location.getLatitude() *1E6);
longi = (int) (location.getLongitude() * 1E6);
GeoPoint ourLocation = new GeoPoint(lati, longi);
OverlayItem overlayItem = new OverlayItem(ourLocation, "1st String",
"2nd String");
CustomPinpoint custom = new CustomPinpoint(d, MainMap.this);
custom.insertPinpoint(overlayItem);
overlayList.add(custom);
overlayList.clear();
} else {
System.out.println("Location is null! " + towers);
Toast.makeText(MainMap.this, "Couldn't get provider",Toast.LENGTH_SHORT)
.show();
}
Run Code Online (Sandbox Code Playgroud) 我刚刚开始工作,但是当我关闭我的Wifi以确定我是否可以使用我的GPS获得更准确的位置时,它现在给我带来了一个NullPointer
错误,我看不出原因.
首先调用下面的代码;
public void onConnected(Bundle dataBundle) {
connected = true;
//Request an update from the location client to get current Location details
mLocationClient.requestLocationUpdates(mLocationRequest,this);
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)
然后需要调用此方法
public void onLocationChanged(android.location.Location location) {
// Report to the UI that the location was updated
String msg = "Updated Location: " +
Double.toString(location.getLatitude()) + "," +
Double.toString(location.getLongitude());
if(tmp != location.getLatitude())
{
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
tmp = location.getLatitude();
}
Run Code Online (Sandbox Code Playgroud)
你可以看到我在那里有两个吐司,他们用实际的GPS坐标回来了,但是当我调用下面的代码时,它会在新的位置线上崩溃 getLastKnownLocation
public String getLocation()
{
// Get the location manager …
Run Code Online (Sandbox Code Playgroud) 我想通过最好的提供商从gps/netwrok获取位置,但总是返回相同的位置,我也可以看到谷歌地图上有一个标志到我的正确位置.请有人能告诉我我做错了什么吗?
我的活动是:
public class MainMenu2 extends Activity implements LocationListener, OnClickListener
Run Code Online (Sandbox Code Playgroud)
在我的onCreate我有:
if(isGooglePlay()){
setContentView(R.layout.menu_2);
setUpMapIfNeeded();
}
Run Code Online (Sandbox Code Playgroud)
isGooglePlay方法:
private boolean isGooglePlay() { // looks if googlemaps is available
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(status == ConnectionResult.SUCCESS){
return true;
}
else{
((Dialog) GooglePlayServicesUtil.getErrorDialog(status, this, 10)).show();
//Toast.makeText(this, "Google Play is not available", Toast.LENGTH_SHORT).show();
return(false);
}
}//isGooglePlay
Run Code Online (Sandbox Code Playgroud)
setUpMapIfNeeded方法:
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.menu_map)) …
Run Code Online (Sandbox Code Playgroud) 我有一个代码应该返回用户位置,但经度不断返回null
,因此我的应用程序不断崩溃.我该如何防止这种情况?
//get the user longitude and latitude
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Geocoder geocoder; //return meaningful data from user location
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
address = addresses.get(0).getAddressLine(0);
city = addresses.get(0).getLocality();
Run Code Online (Sandbox Code Playgroud)