2011-03-10 58 views
2

我正在开发Android应用程序,我需要确保设备已经达到了经纬度和经度描述的某个点。我能想到的唯一的事情就是有这样的事情:Android gps:如何检查某个点是否已经达到?

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

如果你没有崩溃,你可以找到在您的logcat堆栈跟踪有用的信息。而对于进一步的错误分析,我们也需要。 – mreichelt 2011-03-10 22:13:16

+0

感谢您的回答:mreichelt:其实它不会崩溃,我的坏,只是停止响应和冻结。我想,基本上,使用while()循环并请求位置更新不起作用。我只是在onLocationChanged方法中使用if语句。 – Petar 2011-03-10 23:35:11

回答

1

看这可能是你需要什么

Proximity Alert

+0

太棒了!我怎么可能以前没有看到它。肯定会帮助我很多。 – Petar 2011-03-10 23:15:01

+0

不客气。很高兴我能帮上忙。如果您将正确答案标记为正确(点击左侧的复选标记),您的计算器接受评分将提高,人们更可能帮助您。欢迎来到stackoverflow ;-) – n3utrino 2011-03-10 23:19:13

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 
    } 
} 
相关问题