Android Google Map API 检测位置变化

发布于 2024-11-23 18:43:31 字数 138 浏览 0 评论 0原文

我有一个叠加层,我想根据地图中心的范围对其进行整理。

我想检测当前地图中心的变化,以便如果一个人不断移动地图,平移到另一个区域,我可以整理并重新填充新区域中的数据。

是否有一个侦听器,或者我是否必须创建一个线程来定期检查地图中心?

I have an overlay which I want to declutter based on the range from the map center.

I want to detect a change in the current map center so that If a person is constantly moving the map around, panning to another area, I can declutter and repopulate data in the new area.

Is there a listener for this or am I going to have to create a thread to periodically check the map center?

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

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

发布评论

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

评论(2

夜深人未静 2024-11-30 18:43:31

我忘记了它的名字......你必须阅读API文档......但是MapActivity的属性/方法定义了当前显示的边界(以纬度/经度为单位)您可以使用该地图来确定中心值。如果 GPS 报告用户当前位于 XXX 值,但他们已将地图平移到使计算的中心 YYYY 远离 GPS 报告的位置的方向,那么您可以确定地图“距中心有多远”现在已定位。顺便说一句,在做数学计算时使用大圆法。

I forget the name of it ..... you'll have to read the API documentation ..... but there are properties/methods of the MapActivity that define the boundries (in terms of lat / long) of the currently displayed map that you can use to determine what the center value would be. If the GPS is reporting the user is currently at XXX value, but they have panned the map in a direction that puts the calculated center YYYY away from where the GPS is reporting, then you can determine "how far away from center" the Map is now positioned. Use the Great Circle method, by the way, when doing the math.

寻找一个思念的角度 2024-11-30 18:43:31

我认为这样的事情可能会起作用

当您第一次加载地图时获取投影并存储使用 MapView 的 Projection 类显示地图的视图中间的中心 GeoPoint

mapPosition = myMapView.getProjection().fromPixels(x,y) 

将返回与此中的 x,y 相关的 GeoPoint case xy 需要成为 MapView 的中心

扩展 MapView 类

public myMapView extends Mapview { }

因此您可以覆盖 onDispatchDraw

在该方法中您可以检查缩放级别是否已更改,如果没有则可能是因为地图正在平移,也可能是其他原因我想这取决于你正在做什么,但如果你排除缩放,则可以合理地认为大多数重绘都是平移将

屏幕中心的当前投影 GeoPoint 与您在启动时存储的投影进行比较。如果它满足您的距离标准,则执行您的逻辑并更新 mapPosition 地理点。

如果我正确理解你的问题,类似的事情应该有效。当然,如果您不想检查每次重绘,您也可以超越扩展类的其他成员,也可以使用运动事件,但您说您在这些方面遇到了问题。

更新:

您可能可以直接在 MapView 上执行此操作,而不是在自定义容器中执行此操作,但我过去所做的是在 MapView 所在的扩展容器类上重写 DispatchTouchEvent,在本例中是Framelayout 所以

public class myFrameLayout extends FrameLayout {

然后在里面我使用类似的东西来跟踪触摸动作的状态,我会将其减少到应该工作的最小值,“模式”是我创建的枚举,你可以在其中执行
逻辑是当用户在 case 语句底部抬起手指时

@Override
public boolean dispatchTouchEvent(MotionEvent event) {

  switch (event.getAction() & MotionEvent.ACTION_MASK) {

     case MotionEvent.ACTION_POINTER_UP: // Second Finger
     case MotionEvent.ACTION_DOWN:       // First finger  
        mode = GESTURE_MODE.PRE_DRAG;
     break;

     case MotionEvent.ACTION_MOVE:
      if (GESTURE_MODE.PRE_DRAG == mode) // Pan
          mode = GESTURE_MODE.DRAG;
      else                               
      {
      }
     break;

     case MotionEvent.ACTION_POINTER_DOWN:
        mode = GESTURE_MODE.MULTI_TOUCH;
     break;

     case MotionEvent.ACTION_UP: // All fingers removed 
        if (GESTURE_MODE.DRAG == mode) 
        { 
            // DO YOUR PANNING MOVED FAR LOGIC HERE 
        }  
        mode = GESTURE_MODE.NONE;
     break;

 }

 return super.dispatchTouchEvent(event);

} 

I think something like this might work

When you first load the map get a projection and store the center GeoPoint of the middle of the view the map is being displayed on by using the MapView's Projection class

mapPosition = myMapView.getProjection().fromPixels(x,y) 

Will return a GeoPoint related to x,y in this case x y needs to be he center of the MapView

Extend the MapView class

public myMapView extends Mapview { }

So you can override onDispatchDraw

In that method you can check if the zoom level has changed, if it has not then it's probably because the map is being panned, could be other reasons I suppose dependent on what you are doing but it's reasonable to think most redraws are pans if you exclude zooms

Compare the current projection GeoPoint of the center of the screen with the one you stored at startup. If it meets your distance critera perform your logic and update the mapPosition geopoint.

If I understand your question correctly something like that should work. Of course you could over ride other members of the extended class as well if you dont want to check on every redraw, the motion events could be used as well but you said you were having problems with those.

UPDATE:

You might be able to do this directly on MapView vs in a custom container but what I have done in the past is override dispatchTouchEvent on an extended container class that the mapview lives inside, in this case a Framelayout so

public class myFrameLayout extends FrameLayout {

Then inside of that I use something like this to track the state of the touch motions, I will reduce it to the minimum that should work, the "modes" are enums I created, where you would do
logic is when the user lifts the finger at the bottom of the case statement

@Override
public boolean dispatchTouchEvent(MotionEvent event) {

  switch (event.getAction() & MotionEvent.ACTION_MASK) {

     case MotionEvent.ACTION_POINTER_UP: // Second Finger
     case MotionEvent.ACTION_DOWN:       // First finger  
        mode = GESTURE_MODE.PRE_DRAG;
     break;

     case MotionEvent.ACTION_MOVE:
      if (GESTURE_MODE.PRE_DRAG == mode) // Pan
          mode = GESTURE_MODE.DRAG;
      else                               
      {
      }
     break;

     case MotionEvent.ACTION_POINTER_DOWN:
        mode = GESTURE_MODE.MULTI_TOUCH;
     break;

     case MotionEvent.ACTION_UP: // All fingers removed 
        if (GESTURE_MODE.DRAG == mode) 
        { 
            // DO YOUR PANNING MOVED FAR LOGIC HERE 
        }  
        mode = GESTURE_MODE.NONE;
     break;

 }

 return super.dispatchTouchEvent(event);

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