开发者

How do you measure how far the map moved?

In my mapView:regionDidChangeAnimated method I'm making a call to find places on the map but I only want to make the call if the map has moved a significant amount.

Here is scenario:

  1. User moves map or map load
  2. HTTP call to get find places
  3. Add places to the map.
  4. PROBLEM! User clicks on an annotation opening the title bubble and it's close to the edge so it moves the map. Since the loading of data is tied to the map move event the marker disappears and is re-added.

How should I watch both the span and the center point for change?

@Scott Thanks for the visibleMapRect idea. This is what I have working so far, it still needs to account for zooming in and out.

MKMapRect newRect = _mapView.visibleMapRect;
MKMapRect oldRect = currentRect;

float leftBoundry = (newRect.origin.x-(newRect.size.width/4));
float rightBoundry = (newRect.origin.x+(newRect.size.width/4));    
float topBoundry = (newRect.origin.y-(newRect.size.height/4));
float bottomBoundry = (newRect.origin.y+(newRect.size.height/4));

NSLog(@"Origin x %f, y %f", oldRect.origin.x, oldRect.origin.y);
NSLog(@"Boundries left %f, top %f, right %f, bottom %f", leftBoundry, topBoundry, rightBoundry, bottomBoundry);    

if (oldRe开发者_开发百科ct.origin.x < leftBoundry || oldRect.origin.x > rightBoundry || oldRect.origin.y < topBoundry || oldRect.origin.y > bottomBoundry) {
    [self loadLocations];
    currentRect = newRect;
}


Hmm. It sounds like you're refreshing your map by removing all annotations, and then (re-)displaying all annotations that fall within the visibleMapRect – and that solving this problem may require a more nuanced approach to updating the map.

One approach might be to use MKMapRectIntersection to identify the overlap between your "old" and "new" visibleMapRects, and (if there is one) exclude the annotations in this region from being removed or re-added. Or, you could calculate the L-shaped area that's scrolling on-screen and only make an HTML call for data within that region.

Or you could just check whether the map's "old" center is still within visibleMapRect, and if so arbitrarily decide that the map hasn't moved a significant amount. That may leave you with areas on-screen that should have annotations but don't, though.

Or, finally, you could just store the coordinate of the user-selected annotation, and if that coordinate is still on-screen after the map moves, find it and re-select the annotation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜