我是Blackberry的新手。我试图找到纬度和经度。我使用了下面的代码,但它经常为纬度和经度返回0.0,0.0。可以确定我是否已经选择了正确的代码。(或)我在某些地方做错了。
即使我也试过在BB模拟器中设置纬度和经度。 (模拟 - > GPS定位 - >添加了一个新的Loaction),但仍然得到了lat&离经(0.0,0.0)。
请找到我尝试过的代码,
// Locationfinder.java
package com.beacon.bb.app;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class LocationFinder extends MainScreen {
private int _interval = -1;
private double mLatitude, mLongitude;
public LocationFinder() {
super();
// Set criteria for selecting a location provider:
Criteria cr= new Criteria();
cr.setCostAllowed(true);
cr.setPreferredResponseTime(60);
cr.setHorizontalAccuracy(5000);
cr.setVerticalAccuracy(5000);
cr.setAltitudeRequired(true);
cr.isSpeedAndCourseRequired();
cr.isAddressInfoRequired();
add(new RichTextField("Getting Coordinates...."));
try{
LocationProvider lp = LocationProvider.getInstance(cr);
if( lp!=null ){
lp.setLocationListener(new LocationListenerImpl(), _interval, 1, 1);
}
add(new RichTextField("Calulating GPS Cordinates :"));
add(new RichTextField("Latitude :" + mLatitude + "," + "Longitude :" + mLongitude));
//System.out.println("Lon" + longitude + " Lat "+ latitude + " course "+course+" speed "+speed+" timestamp "+timestamp);
}
catch(LocationException le)
{
add(new RichTextField("Location exception "+le));
}
}
private class LocationListenerImpl implements LocationListener {
public void locationUpdated(LocationProvider provider, Location location) {
if(location.isValid()) {
double longitude = location.getQualifiedCoordinates().getLongitude();
double latitude = location.getQualifiedCoordinates().getLatitude();
double altitude = location.getQualifiedCoordinates().getAltitude();
float speed = location.getSpeed();
System.out.println("Lon" + longitude + " Lat "+ latitude + " speed "+speed);
mLatitude = latitude;
mLongitude = longitude;
}
}
public void providerStateChanged(LocationProvider provider, int newState) {
// MUST implement this. Should probably do something use ful with it as well.
}
}
}
答案 0 :(得分:0)
你已经将_interval设置为-1,这应该是以秒为单位的有效时间。我的意思是_interval值表示听取位置的时间。
也可以试试这个链接: