在我的应用程序中我尝试获取用户位置,但它经常为0.0纬度和经度。我见过黑莓论坛但找不到我做错了什么???
代码:
package com.MyChamberApp;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;
public class GPS_Location {
static double longi;
static double lati;
public GPS_Location() {
}
public void location()
{
new LocationTracker();
}
class LocationTracker
{
private LocationProvider provider;
Criteria cr;
public LocationTracker()
{
resetGPS();
}
public void resetGPS() {
try {
cr = new Criteria();
cr.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH);
cr.setPreferredResponseTime(120000);
cr.setCostAllowed(true);
provider = LocationProvider.getInstance(cr);
provider.getLocation(120);
if (provider != null)
{
provider.setLocationListener(new MyLocationListener(), 1,1,1);
}
} catch (Exception e) {}
}
public void run(){}
private class MyLocationListener implements LocationListener
{
public void providerStateChanged(LocationProvider provider,int newState)
{
if (newState == LocationProvider.TEMPORARILY_UNAVAILABLE)
{
provider.reset();
resetGPS();
}
if (newState == LocationProvider.OUT_OF_SERVICE)
{
provider.reset();
resetGPS();
}
}
public void locationUpdated(LocationProvider provider,Location location)
{
if (location != null && location.isValid()) {
try {
lati = location.getQualifiedCoordinates().getLatitude();
longi = location.getQualifiedCoordinates().getLongitude();
} catch (Exception e) {}
}
}
}
}
}
我在curve8300,bold9000上试过这段代码 它在模拟器上运行良好,但即使我从选项 - >高级选项 - > gps手动刷新我的gps,也不会在设备上获取价值。 提前谢谢!!!!!!!!!!!!!!!!
答案 0 :(得分:1)
Criteria criteria = new Criteria();
criteria.setHorizontalAccuracy(5000);
criteria.setVerticalAccuracy(5000);
criteria.setCostAllowed(true);
criteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_HIGH);
_locationProvider = LocationProvider.getInstance(criteria);
Location location =_locationProvider.getLocation(-1);
_longitude = location.getQualifiedCoordinates().getLongitude();
_latitude = location.getQualifiedCoordinates().getLatitude();
_altitude = location.getQualifiedCoordinates().getAltitude();
_speed = location.getSpeed();
if(_locationProvider!=null)
{
_locationProvider.setLocationListener(new LocationListenerImpl(), _interval, 1, 1);
}
这段代码对我有用!!!!!!!
答案 1 :(得分:1)
criteria.setHorizontalAccuracy(500);
criteria.setVerticalAccuracy(500);
将水平和垂直精度设置为低值。这在某种程度上有助于准确性