我正在使用Android应用,我需要确保设备已达到lat和lon描述的特定点。我唯一能想到的就是拥有这样的东西:
Location initLoc = theManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double lat = initLoc.getLatitude();
double lon = initLoc.getLongitude();
MyPoint firstPoint = getPoints().get(0);
double dist = CalcHelper.getDistance1(lat, lat, firstPoint.getLat(), firstPoint.getLon());
while(dist > 30){
initLoc = theManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
lat = initLoc.getLatitude();
lon = initLoc.getLongitude();
dist = CalcHelper.getDistance1(lat, lon, firstPoint.getLat(), firstPoint.getLon());
}
但这会导致程序崩溃。如果你能引导我走向正确的方向,我将非常感激。
让我借此机会提出进一步的问题。正如我所说,我是Android和GPS的新手,并且考虑到如何正确开发与GPS一起工作的应用程序的文档和信息很少,我基本上是在盲目工作。所以我的问题是:
这是onLocationChanged方法的样子:
public void onLocationChanged(Location location) {
double lat = location.getLatitude();
double lon = location.getLongitude();
MyPoint firstPoint = MainScreen.dataset.getPoints().get(0);
double dist = CalcHelper.getDistance1(lat, firstPoint.getLat(),lon, firstPoint.getLon());
if(dist < 10){
Context context = getApplicationContext();
CharSequence text = "Start reached. Starting moving on track";
int duration = 6000;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}else{
Context context = getApplicationContext();
CharSequence text = "Distance until start: "+dist;
int duration = 6000;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
我要做的是让程序确定何时到达我作为一组点提供的轨道的开始。所以,当我启动程序时,我会得到一个合理的距离估计。问题是,当我开始移动时,距离似乎没有更新,它会更新,但移动50米后,它说我只移动了5个说。另一方面,当我启动应用程序并且距离起点不到10米时,它会正确检测到它。所以基本上,当我使用设备时,onLocationChanged方法似乎没有给我正确的位置,我现在。你能告诉我我可能做错了什么吗?
答案 0 :(得分:2)
我认为你想要做的是让你的活动实现LocationListener,即订阅GPS修复。例如。
public class MyActivity extends Activity implements LocationListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// request current location updates
LocationManager locMan = (LocationManager) getSystemService(LOCATION_SERVICE);
// set here the criteria for location provider accuracy
Criteria locationProviderCriteria = new Criteria();
locationProviderCriteria.setAccuracy(Criteria.ACCURACY_FINE);
String locationProvider = locMan.getBestProvider(locationProviderCriteria, true);
locMan.requestLocationUpdates(locationProvider, MIN_TIME_BETWEEN_UPDATES_IN_MILLIS, MIN_DISTANCE_BETWEEN_UPDATES_IN_METERS, this);
}
/*
* This method will be called on each location update
*/
@Override
public void onLocationChanged(Location loc) {
//put here your logic to see whether the user reached the destination
}
}
答案 1 :(得分:1)
看看这可能正是你所需要的