地图上特定位置的用户当前位置
User Current Location on specific position on MAP
- MKMapView
- 核心位置
- Swift3.0
我有这样的需求:
创建一个应用程序,我在其中从 webservice 获得 Lat and Long
的不同位置。使用 Custom AnnotationView
在地图上绘制这些位置。除此之外,我正在展示 User Current Location with Custom Image
。
一切正常fine
。
现在,当用户移动时,用户当前位置 Pin 会改变其位置,因此对此有一个重要的要求,我需要使当前位置位置保持在地图底部(just 100 pixel
) 从下往上。
我做了一些 RND,找到了一些有用的链接:
- Centering MKMapView on spot N-pixels below pin
- How can I center my mapview so that the selected pin is not in the middle of the map but in 1/3 of it?
- set current location icon lower side in MKMapView
但这不再有效了。
请建议我如何在 iOS
地图中设置用户当前位置图钉,使其始终位于地图底部(距底部上方 100 像素)
所需的引脚位置:
目前显示在其他位置
注意 - **不希望 Google 映射此类功能。
如果您一直在使用这样的东西mapView.setUserTrackingMode(.Follow, animated: true)
那么用户位置将始终显示在地图的中心,zoom/pan将在用户位置更新时自动调整。
每次用户位置更改时,您都需要禁用 UserTrackingMode
,然后使用 func setVisibleMapRect(_ mapRect: MKMapRect, edgePadding insets: UIEdgeInsets, animated animate: Bool)
手动调整地图 zoom/pan。
我在类似情况下使用了以下代码
let annotationPoint = MKMapPointForCoordinate(mapView.userLocation.coordinate);
var zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 1, 1);
annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
let pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 1, 1);
zoomRect = MKMapRectUnion(zoomRect, pointRect);
mapView.setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsetsMake(40, 50, 160, 50), animated: true)
p.s。 edgePadding
在这种情况下是你的朋友。
以下是用于以自动调整 UserLocation pin 的方式定位地图的技巧:
func setUserLocationOnLowerPositiononMap(coordinate: CLLocationCoordinate2D) {
var region = MKCoordinateRegion(center: coordinate, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
region.center = coordinate
self.map.setRegion(region, animated: true)
//self.map.moveCenterByOffSet(offSet: CGPoint(x: 0, y: -130), coordinate: coordinate)
self.map.moveCenterByOffSet(offSet: CGPoint(x: 0, y: SCREEN_HEIGHT - SCREEN_HEIGHT * 4/3 + 0 ), coordinate: coordinate)
}
- MKMapView
- 核心位置
- Swift3.0
我有这样的需求:
创建一个应用程序,我在其中从 webservice 获得 Lat and Long
的不同位置。使用 Custom AnnotationView
在地图上绘制这些位置。除此之外,我正在展示 User Current Location with Custom Image
。
一切正常fine
。
现在,当用户移动时,用户当前位置 Pin 会改变其位置,因此对此有一个重要的要求,我需要使当前位置位置保持在地图底部(just 100 pixel
) 从下往上。
我做了一些 RND,找到了一些有用的链接:
- Centering MKMapView on spot N-pixels below pin
- How can I center my mapview so that the selected pin is not in the middle of the map but in 1/3 of it?
- set current location icon lower side in MKMapView
但这不再有效了。
请建议我如何在 iOS
地图中设置用户当前位置图钉,使其始终位于地图底部(距底部上方 100 像素)
所需的引脚位置:
目前显示在其他位置
注意 - **不希望 Google 映射此类功能。
如果您一直在使用这样的东西mapView.setUserTrackingMode(.Follow, animated: true)
那么用户位置将始终显示在地图的中心,zoom/pan将在用户位置更新时自动调整。
每次用户位置更改时,您都需要禁用 UserTrackingMode
,然后使用 func setVisibleMapRect(_ mapRect: MKMapRect, edgePadding insets: UIEdgeInsets, animated animate: Bool)
手动调整地图 zoom/pan。
我在类似情况下使用了以下代码
let annotationPoint = MKMapPointForCoordinate(mapView.userLocation.coordinate);
var zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 1, 1);
annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
let pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 1, 1);
zoomRect = MKMapRectUnion(zoomRect, pointRect);
mapView.setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsetsMake(40, 50, 160, 50), animated: true)
p.s。 edgePadding
在这种情况下是你的朋友。
以下是用于以自动调整 UserLocation pin 的方式定位地图的技巧:
func setUserLocationOnLowerPositiononMap(coordinate: CLLocationCoordinate2D) {
var region = MKCoordinateRegion(center: coordinate, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
region.center = coordinate
self.map.setRegion(region, animated: true)
//self.map.moveCenterByOffSet(offSet: CGPoint(x: 0, y: -130), coordinate: coordinate)
self.map.moveCenterByOffSet(offSet: CGPoint(x: 0, y: SCREEN_HEIGHT - SCREEN_HEIGHT * 4/3 + 0 ), coordinate: coordinate)
}