android中在地图上绘制路线

发布于 2024-11-10 07:12:45 字数 2773 浏览 3 评论 0原文

在我的应用程序中,我在地图上绘制路线路径,无论我在哪里移动,并固定源和目的地。所以我使用 LocationManager 类来获取位置更新,因为它myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,new myLocationListener());。我走了 10 米的距离,没有完成位置更新。所以为了检查我的代码。我只是将功能放入 onStatusChanged 函数中(因为它至少被调用一次)。我运行应用程序 onStatusChanged 被调用。我的问题是固定源和目标以及连续调用的路由绘制类,即使未调用onStatusChanged。即使调用了它们的类,pin 也没有被指向,root 也没有被绘制

我的代码:

public class Map extends MapActivity 
  {  
    // class for pin the location 
class MapOverlay extends com.google.android.maps.Overlay
    {
      ............
      Log.e("loc","true");
    }
   public void onCreate(Bundle savedInstanceState) 
   {
      .................. 
      myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,new myLocationListener());        
   }
@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

class myLocationListener implements LocationListener {
    public void onLocationChanged(Location loc) {       
    }
    ........
   public void onStatusChanged(String provider, int status, Bundle extras) {
     Log.e("MAP","onStatusChanged - called");
     ....// finding lat and lng getting.
     Log.e("Updated Location",""+latPointDst+" , "+lngPointDst);
      ..........
      // here i called MyOverlay class -- root drawing.
      // here i called MapOverlay class  -- pin the location
   }  

} 

 public class MyOverlay extends Overlay {
  .............
  Log.e("location change","drawing");
  }

}

我的logcat:

 05-30 13:29:54.653: ERROR/MAP(3850): onStatusChanged - called
 05-30 13:29:54.673: ERROR/Updated Location(3850): 9.909228086471558 , 78.10081958770752
 05-30 13:29:54.743: ERROR/loc(3850): true
 05-30 13:29:54.783: ERROR/loc(3850): true
 05-30 13:29:54.793: ERROR/loc(3850): true
 05-30 13:29:54.803: ERROR/loc(3850): true
 05-30 13:29:54.813: ERROR/location change(3850): drawing
 05-30 13:29:54.983: ERROR/loc(3850): true
 05-30 13:29:54.993: ERROR/loc(3850): true
 05-30 13:29:54.993: ERROR/loc(3850): true
 05-30 13:29:55.003: ERROR/loc(3850): true
 05-30 13:29:55.013: ERROR/location change(3850): drawing
 05-30 13:29:55.193: ERROR/loc(3850): true
 05-30 13:29:55.203: ERROR/loc(3850): true
 05-30 13:29:55.223: ERROR/loc(3850): true
 05-30 13:29:55.233: ERROR/loc(3850): true
 05-30 13:29:55.243: ERROR/location change(3850): drawing
 05-30 13:29:55.473: ERROR/loc(3850): true
 05-30 13:29:55.483: ERROR/loc(3850): true
 05-30 13:29:55.573: ERROR/loc(3850): true
 05-30 13:29:55.603: ERROR/loc(3850): true
 05-30 13:29:55.633: ERROR/location change(3850): drawing
 05-30 13:29:55.693: ERROR/loc(3850): true

等等。 我在状态更改时编写类调用,但我不知道如何在状态更改的情况下连续调用......如果我错了,请协助满足我的需要。请帮我。

in my app i am drawing route path on map where ever i am moving and pin the source and destination. so i use LocationManager class for get location update as itmyManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,new myLocationListener());. i go 10 meter distance no location update is done. so for the checking my code. i just put functionality in the onStatusChanged function(because it is called at least one time).i run the app onStatusChanged is called. what my problem is pin the source and destination and route drawing class called continuously even though the onStatusChanged is not called. and pin is not pointed and root is not drawn even though their class is called.

my code:

public class Map extends MapActivity 
  {  
    // class for pin the location 
class MapOverlay extends com.google.android.maps.Overlay
    {
      ............
      Log.e("loc","true");
    }
   public void onCreate(Bundle savedInstanceState) 
   {
      .................. 
      myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,new myLocationListener());        
   }
@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

class myLocationListener implements LocationListener {
    public void onLocationChanged(Location loc) {       
    }
    ........
   public void onStatusChanged(String provider, int status, Bundle extras) {
     Log.e("MAP","onStatusChanged - called");
     ....// finding lat and lng getting.
     Log.e("Updated Location",""+latPointDst+" , "+lngPointDst);
      ..........
      // here i called MyOverlay class -- root drawing.
      // here i called MapOverlay class  -- pin the location
   }  

} 

 public class MyOverlay extends Overlay {
  .............
  Log.e("location change","drawing");
  }

}

my logcat:

 05-30 13:29:54.653: ERROR/MAP(3850): onStatusChanged - called
 05-30 13:29:54.673: ERROR/Updated Location(3850): 9.909228086471558 , 78.10081958770752
 05-30 13:29:54.743: ERROR/loc(3850): true
 05-30 13:29:54.783: ERROR/loc(3850): true
 05-30 13:29:54.793: ERROR/loc(3850): true
 05-30 13:29:54.803: ERROR/loc(3850): true
 05-30 13:29:54.813: ERROR/location change(3850): drawing
 05-30 13:29:54.983: ERROR/loc(3850): true
 05-30 13:29:54.993: ERROR/loc(3850): true
 05-30 13:29:54.993: ERROR/loc(3850): true
 05-30 13:29:55.003: ERROR/loc(3850): true
 05-30 13:29:55.013: ERROR/location change(3850): drawing
 05-30 13:29:55.193: ERROR/loc(3850): true
 05-30 13:29:55.203: ERROR/loc(3850): true
 05-30 13:29:55.223: ERROR/loc(3850): true
 05-30 13:29:55.233: ERROR/loc(3850): true
 05-30 13:29:55.243: ERROR/location change(3850): drawing
 05-30 13:29:55.473: ERROR/loc(3850): true
 05-30 13:29:55.483: ERROR/loc(3850): true
 05-30 13:29:55.573: ERROR/loc(3850): true
 05-30 13:29:55.603: ERROR/loc(3850): true
 05-30 13:29:55.633: ERROR/location change(3850): drawing
 05-30 13:29:55.693: ERROR/loc(3850): true

and so on.
i write class calling within on statuschanged but i do not know how is called continuously without on status changed.. . if i am wrong please assist to do my need. please help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无力看清 2024-11-17 07:12:45

我认为你需要在这里关注两件事:

1 - 你不应该依赖 onStatusChanged 来获取最近的位置。 时调用该方法

  • 当提供者状态改变
  • 。提供商无法获取位置,或者提供商在一段时间不可用后最近变得可用。

获取最新位置更改的唯一方法是在 onLocationChanged 中。

2 - 覆盖层的绘制方法将被调用很多。您不应该依赖该方法来添加标记。您可以在绘制方法之外向叠加层添加标记。当您填充叠加层或使地图无效时,您添加到叠加层的任何标记都将显示在地图上。无需在绘制方法中添加标记。

正如您从此处绘制一个Google 地图上的线/路径,draw() 方法用于绘制 2 个地理点之间的路径。如果您想向地图添加标记(图钉),可以在叠加层上执行此操作,请参阅此答案 如何在 Android 中清除/重置 ItemizedOverlay? 了解有关如何向地图添加标记的更多信息。

I think you need to focus on 2 things here :

1 - You shouldn't rely on the onStatusChanged to get a recent location. The method is called when

  • the provider status changes.
  • a provider is unable to fetch a location or if the provider has recently become available after a period of unavailability.

The only way to get up to date location changes is in onLocationChanged.

2 - The draw method of the overlay will be called a lot. You shouldn't rely on that method to add markers. You can add markers to your overlay outside the draw method. When you populate the overlay, or invalidate the map, any markers that you added to the overlay will be shown on the map. No need to add the markers in the draw method.

As you can see from the accepted answer here Drawing a line/path on Google Maps, the draw() method is used to draw a path between 2 geopoints. If you want to add markers (pins) to the map, you can do it on your overlay, see this answer How to clear / reset an ItemizedOverlay in Android? for more info on how to add markers to the map.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文