当前位置不使用GPS更新 [英] Current Location Not Update Using GPS

查看:85
本文介绍了当前位置不使用GPS更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

- > Class下面是查找当前位置。



公共类CurrentLocation {



private LocationManager locationManager;

private Context context;

private位置位置;

private LocationListener locationListener;



public CurrentLocation(Context ctx){

this.context = ctx;



locationManager =(LocationManager)context

.getSystemService(Context.LOCATION_SERVICE);

@SuppressWarnings(unused)

List< string> providers = locationManager.getAllProviders();



Criteria locationCritera = new Criteria();

String providerName = locationManager.getBestProvider(locationCritera,

true);



location = locationManager.getLastKnownLocation(providerName);

locationListener = new MyLocationListener();



locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,

0,locationListener);

location = locationManager .getLastKnownLocation(providerName);



}

公共位置getLocation(){

返回位置;

}

}





- > Class下面是MyLocationListner。



公共类MyLocationListener实现LocationListener {



@Override

public void onLocationChanged(位置loc){

}



@Over骑行

public void onProviderDisabled(String arg0){

}



@Override

public void onProviderEnabled(String provider){

}



@Override

public void onStatusChanged(String provider,int status,Bundle extras){

}



}



- >我获取当前位置。



CurrentLocation currLocation = new CurrentLocation(MainActivity.this);

位置location = currLocation.getLocation ();



Toast.makeText(MainActivity.java,Lat+ location.getLatitude +Long:+ location.getLongitude,

->Below Class is Find Current Location.

public class CurrentLocation {

private LocationManager locationManager;
private Context context;
private Location location;
private LocationListener locationListener;

public CurrentLocation(Context ctx) {
this.context = ctx;

locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
@SuppressWarnings("unused")
List<string> providers = locationManager.getAllProviders();

Criteria locationCritera = new Criteria();
String providerName = locationManager.getBestProvider(locationCritera,
true);

location = locationManager.getLastKnownLocation(providerName);
locationListener = new MyLocationListener();

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locationListener);
location = locationManager.getLastKnownLocation(providerName);

}
public Location getLocation() {
return location;
}
}


->Below Class is MyLocationListner.

public class MyLocationListener implements LocationListener {

@Override
public void onLocationChanged(Location loc) {
}

@Override
public void onProviderDisabled(String arg0) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

}

->I Get Current Location.

CurrentLocation currLocation = new CurrentLocation(MainActivity.this);
Location location = currLocation.getLocation();

Toast.makeText(MainActivity.java,"Lat "+location.getLatitude+" Long : "+location.getLongitude,

Toast.LENGTH_LONG)
                    .show();







但我有问题。 ..

当我安装应用程序那个时间给出正确的位置纬度和经度,但当我

去Onther城并运行已安装的应用程序那时它给我以前的位置,表示最后一个位置纬度和经度。当位置更改时,导入我的位置未更新。



请告诉我如何更新位置,或刷新上一个已知位置?




But I have Problem...
When I Install Application That Time give correct Location Latitude and Longitude,But when I
go to Onther city and run installed Application that time it give me previous Location ,mean Last Known Location latitude and Longitude.Insort my Location is Not Updated when Location Change.

please Tell me how update Location,or refresh last known location?

推荐答案

@Override
public void onLocationChanged(Location loc) {
}



什么它做到了吗?没有。那不是你应该对LocationChanged事件做出反应的那一行吗?


What does it do? Nothing. Isn't that the line where you ought to react on the LocationChanged event?


这篇关于当前位置不使用GPS更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆