无法获取Android设备上的GPS位置提供商的位置 [英] Unable to get location with gps location provider on android device
问题描述
我正在开发基于小型位置的android应用程序,其中我需要用户当前的位置。我的代码看起来像是:
private final LocationListener locationListener = new LocationListener(){
public void onLocationChanged(Location location){
updateWithNewLocation(location);
$ b public void onProviderDisabled(String provider){} $ b $ public void onProviderEnabled(String provider){} $ b $ public void onStatusChanged(String provider,int status,
Bundle extras){}
};
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
String svcName = Context.LOCATION_SERVICE;
locationManager =(LocationManager)getSystemService(svcName);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);
String provider = locationManager.getBestProvider(criteria,true);
//位置l = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
位置l = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(l);
locationManager.requestLocationUpdates(provider,2000,10,locationListener);
$ b private void updateWithNewLocation(Location location)
{
TextView myLocationText;
myLocationText =(TextView)findViewById(R.id.myLocationText);
字符串latLongString =找不到位置;
if(location!= null){
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString =Lat:+ lat +\\\
Long:+ lng;
}
myLocationText.setText(您当前的位置是:\ n+
latLongString);
$ / code>
我的问题是,当我使用提供者作为网络时,工作正常。但是,当它选择gps作为提供者时,它会给出空值。我第一次知道它给了我空值。我也使用onLocationChanged方法,但它仍然没有给我适当的输出。
当我打开我的应用程序时,它显示我输出空值,并且它也启动gps来处理我的位置。我等待一段时间才能获得更新的位置,但它没有给我有效的输出。
我的代码有什么问题吗?
我正在使用android设备。
需要帮助...谢谢...
更改此行
位置l = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider,2000,10,locationListener);
至
Location l = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000,10,locationListener);
然后我认为它会起作用。
或者您可以按照这个教程来获取使用gps的位置
I am developing small location based android application in which I need users current location. I am also updating users current location as soon as some change in location is occurred.My code looks like:
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status,
Bundle extras) {}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
String svcName = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(svcName);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);
String provider = locationManager.getBestProvider(criteria, true);
//Location l = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
Location l = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(l);
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
}
private void updateWithNewLocation(Location location)
{
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);
String latLongString = "No location found";
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
}
myLocationText.setText("Your Current Position is:\n" +
latLongString);
}
My problem is that when I use provider as network then it's working fine. But when it select gps as provider then it's giving null value. I know for the first time it gives me null value. I also used onLocationChanged method but it's still not giving me proper output. When I open my application it shows me output null value and it also start gps for serching my location. I wait for some time to get updated location but it not giving me valid output. Is there any thing wrong with my code. I am using android device.
Need Help... Thank you...
Change this line
Location l = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
to
Location l = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
then i think it will work.
Or you can follow this tutorial to get the location using gps
这篇关于无法获取Android设备上的GPS位置提供商的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!