ios - 是否可以定位 map View ,使用户的当前位置不在 View 的中心?

标签 ios mkmapview

我在覆盖整个设备屏幕空间的 View 中绘制 map 。

在此 View 之上是占据屏幕下半部分的另一个 View 。顶 View 是半透明的,因此用户可以看到其下方的覆盖 map 。

我在 map 中显示了用户的当前位置。

map View 会自动定位 map ,使用户的位置在 View 中居中 - 因此它是设备屏幕的中心,但是该中心也被顶 View 覆盖。

但是我希望用户的位置位于 map View 未被顶 View 覆盖的部分的中心。

最佳答案

最简单的解决方案是对坐标应用偏移量以稍微移动 map ,利用 MKMapView 区域的跨度。

CLLocationCoordinate2D newCenter = userCoordinate;
newCenter.latitude -= _mapView.region.span.latitudeDelta * 0.50;
[self.mapView setCenterCoordinate:newCenter animated:YES];

0.50 的值只是为了给您举个例子。通过更改它,您可以调整偏移量。

您还可以获取当前中心位置,将其转​​换为 CGPoint,然后将所需的偏移量(以像素为单位)添加到 CGPoint 并将其转换回 CLLocationCoordinate2D :

UIOffset offset = UIOffsetMake(30.0f, 40.0f);
         offset.horizontal
         offset.vertical

CGPoint point = [_mapView convertCoordinate:userCoordinate
                              toPointToView:_mapView];
point.x += offset.horizontal;
point.y += offset.vertical;

CLLocationCoordinate2D newCenter = [_mapView convertPoint:point            
                                     toCoordinateFromView:_mapView];
[_mapView setCenterCoordinate:newCenter animated:YES];

希望对您有所帮助。

关于ios - 是否可以定位 map View ,使用户的当前位置不在 View 的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22106589/

相关文章:

ios - 无法将大多数对象拖动到 Storyboard

ios - 如何在不达到全局GCD限制的情况下并行处理许多(100+)个任务?

ios - Xcode:项目导航器,黄色文件夹和蓝色文件夹的区别

ios - 如何将 MKMapPoint 转换为 CGPoint

objective-c - MKMapView 中 userLocation 的中心区域

iphone - CGContextDrawImage 跨设备的字节值不一致?

ios - Xcode 6.2 beta 4 是否让 watchKit 接口(interface)与页面无法打开推送接口(interface)?

ios - MKMapView - 保持用户居中并覆盖屏幕上 map View 上绘制的路线

ios - 从 map 中删除注释并替换为更新的注释 (qTree)

iphone - 如何在iPhone中的mapview中的两个地方之间画线?