- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我目前正在尝试让用户向 map 添加图钉,然后 map 将绘制一个连接这些图钉的多边形。但是我想扩展它以允许用户能够拖动图钉并且多边形将相应地更新。 MKMapView 根据它们在数组中的排列(如果我没记错的话)从坐标数组中绘制多边形。我现在面临的问题是如何在用户重新定位引脚后更新多边形。
var touchCoordinatesWithOrder: [(coordinate: CLLocationCoordinate2D, order: Int)] = []
var counter = 0
func addLongPressGesture() {
let longPressRecogniser = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
longPressRecogniser.minimumPressDuration = 1.0
mapView.addGestureRecognizer(longPressRecogniser)
}
func handleLongPress(gestureRecognizer: UIGestureRecognizer) {
if gestureRecognizer.state != .Began {
return
}
let touchPoint = gestureRecognizer.locationInView(self.mapView)
let touchMapCoordinate = mapView.convertPoint(touchPoint, toCoordinateFromView: mapView)
let annotation = MKPointAnnotation()
annotation.coordinate = touchMapCoordinate
mapView.addAnnotation(annotation)
touchCoordinatesWithOrder.append((coordinate: touchMapCoordinate, order: counter))
counter += 1
}
@IBAction func drawAction(sender: AnyObject) {
if touchCoordinatesWithOrder.count <= 2 {
print("Not enough coordinates")
return
}
var coords = [CLLocationCoordinate2D]()
for i in 0..<touchCoordinatesWithOrder.count {
coords.append(touchCoordinatesWithOrder[i].coordinate)
}
let polygon = MKPolygon(coordinates: &coords, count: coords.count)
mapView.addOverlay(polygon)
counter = 0
}
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
// if the user repositioned pin number2 then how to I update my array?
}
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKPolygon {
let polygonView = MKPolygonRenderer(overlay: overlay)
polygonView.strokeColor = UIColor.blackColor()
polygonView.lineWidth = 0.5
return polygonView
}
return MKPolylineRenderer()
}
最佳答案
要使引脚可拖动,您需要在 MKAnnotationView
上设置 draggable = true
。实现 viewForAnnotation
并出队或创建注释,然后设置 draggable = true
。确保设置了 MKMapView
委托(delegate),否则将不会调用任何委托(delegate)方法。
您可能还会发现将注释存储在数组中比仅存储坐标更容易。 map View 保留对数组中注释的引用,因此当点在 map 中移动时,注释会自动更新。
您的问题没有说明您是否需要绘制一条路径围绕这些点,或者通过这些点。如果要绘制围绕点的叠加层,则还需要计算坐标的凸包。代码示例执行此操作,尽管它很容易删除。
例子:
class MapAnnotationsOverlayViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var mapView: MKMapView!
// Array of annotations - modified when the points are changed.
var annotations = [MKPointAnnotation]()
// Current polygon displayed in the overlay.
var polygon: MKPolygon?
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
addLongPressGesture()
}
func addLongPressGesture() {
let longPressRecogniser = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
longPressRecogniser.minimumPressDuration = 0.25
mapView.addGestureRecognizer(longPressRecogniser)
}
func handleLongPress(gestureRecognizer: UIGestureRecognizer) {
guard gestureRecognizer.state == .Began else {
return
}
let touchPoint = gestureRecognizer.locationInView(self.mapView)
let touchMapCoordinate = mapView.convertPoint(touchPoint, toCoordinateFromView: mapView)
let annotation = MKPointAnnotation()
// The annotation must have a title in order for it to be selectable.
// Without a title the annotation is not selectable, and therefore not draggable.
annotation.title = "Point \(annotations.count)"
annotation.coordinate = touchMapCoordinate
mapView.addAnnotation(annotation)
// Add the new annotation to the list.
annotations.append(annotation)
// Redraw the overlay.
updateOverlay()
}
@IBAction func drawAction(sender: AnyObject) {
updateOverlay()
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
var view = mapView.dequeueReusableAnnotationViewWithIdentifier("pin")
if let view = view {
view.annotation = annotation
}
else {
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")
// Allow the pin to be repositioned.
view?.draggable = true
}
return view
}
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
// The map view retains a reference to the same annotations in the array.
// The annotation in the array is automatically updated when the pin is moved.
updateOverlay()
}
func updateOverlay() {
// Remove existing overlay.
if let polygon = self.polygon {
mapView.removeOverlay(polygon)
}
self.polygon = nil
if annotations.count < 3 {
print("Not enough coordinates")
return
}
// Create coordinates for new overlay.
let coordinates = annotations.map({ $0.coordinate })
// Sort the coordinates to create a path surrounding the points.
// Remove this if you only want to draw lines between the points.
var hull = sortConvex(coordinates)
let polygon = MKPolygon(coordinates: &hull, count: hull.count)
mapView.addOverlay(polygon)
self.polygon = polygon
}
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKPolygon {
let polygonView = MKPolygonRenderer(overlay: overlay)
polygonView.strokeColor = UIColor.blackColor()
polygonView.lineWidth = 0.5
return polygonView
}
return MKPolylineRenderer()
}
}
这是凸包排序算法(改编自 Gist on GitHub )。
func sortConvex(input: [CLLocationCoordinate2D]) -> [CLLocationCoordinate2D] {
// X = longitude
// Y = latitude
// 2D cross product of OA and OB vectors, i.e. z-component of their 3D cross product.
// Returns a positive value, if OAB makes a counter-clockwise turn,
// negative for clockwise turn, and zero if the points are collinear.
func cross(P: CLLocationCoordinate2D, _ A: CLLocationCoordinate2D, _ B: CLLocationCoordinate2D) -> Double {
let part1 = (A.longitude - P.longitude) * (B.latitude - P.latitude)
let part2 = (A.latitude - P.latitude) * (B.longitude - P.longitude)
return part1 - part2;
}
// Sort points lexicographically
let points = input.sort() {
$0.longitude == $1.longitude ? $0.latitude < $1.latitude : $0.longitude < $1.longitude
}
// Build the lower hull
var lower: [CLLocationCoordinate2D] = []
for p in points {
while lower.count >= 2 && cross(lower[lower.count-2], lower[lower.count-1], p) <= 0 {
lower.removeLast()
}
lower.append(p)
}
// Build upper hull
var upper: [CLLocationCoordinate2D] = []
for p in points.reverse() {
while upper.count >= 2 && cross(upper[upper.count-2], upper[upper.count-1], p) <= 0 {
upper.removeLast()
}
upper.append(p)
}
// Last point of upper list is omitted because it is repeated at the
// beginning of the lower list.
upper.removeLast()
// Concatenation of the lower and upper hulls gives the convex hull.
return (upper + lower)
}
这是凸包排序的样子(围绕点绘制的路径):
这是没有排序的样子(按顺序从一个点到另一个点绘制的路径):
关于ios - MKMapKit 可拖动注释和绘制多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38344993/
我正在尝试制作一个应用程序来查看我要去的大学,但我无法只查看一个城市。我试图确保用户无法滚动经过城市。然后我尝试覆盖该区域。我认为 setRegion 方法将有助于解决该问题,但显然不会。关于如何设置
我想确定 MKMapViewDelegate 中的最小 + 最大可见纬度和经度值委托(delegate)方法: - (void)mapView:(MKMapView *)mapView regionD
我想知道如何确保 callout 气泡不会在 MKMapView 上被取消选择。 每当我按下 map (背景)时,此 View : 转向: 这是我不想允许的。但我确实想保留对 callOutButto
似乎我为高斯模糊找到的所有东西都需要像 GPUImage 这样的图像或视频。有谁知道 GPUimage 的分支或在 MKMapKit map 区域上放置高斯模糊的方法?目标是 map 仍然可以缩放和拖
如何让我的 MKMapView 跟随我的用户直到他们滚动,然后再有一个按钮来跟随用户? 这是我希望它拥有的流程。 查看负载:放大并以用户当前位置为中心,然后跟随用户四处走动。 用户滚动:在按下按钮之前
我的应用程序上有用户位置,但如何在当前用户位置上添加注释?我是否必须获取用户位置的经度和纬度并以这种方式删除注释?或者我该怎么做? 最佳答案 首先导入必要的框架(CoreLocation 和 MapK
默认情况下,我们可以在 map View 上显示用户的位置。在点击注释图钉时,它将显示“当前位置”。但我想将用户的地址显示为街道、城市和国家。我如何使用 CLGeoCoder 类来做到这一点? 最佳答
我目前正在尝试让用户向 map 添加图钉,然后 map 将绘制一个连接这些图钉的多边形。但是我想扩展它以允许用户能够拖动图钉并且多边形将相应地更新。 MKMapView 根据它们在数组中的排列(如果我
每当我设置 MKMapKit 的区域属性时,它都会使跨度加倍。 例如: MKCoordinateRegion currentRegion = map.region; // assume the cur
大家好, 我正在尝试让 MKMapView 启动并运行,但是我似乎无法克服似乎是引用错误的问题。我花了几个小时在谷歌上搜索这个以及昨晚。 标题: #import #import @interfac
我已经被困在这个EXC_BAD_ACCESS错误2天了。我有一个reloadAnnotations方法,可以在添加新注释之前删除所有注释。在删除注释之前,此方法应检查新集合是否包含相同的位置,以便不会
我在 map 上有 2-10 个注释图钉。当我单击一个按钮时,我希望 map 缩小并居中,以便用户可以看到所有图钉。我怎样才能做到这一点? 提前致谢 最佳答案 假设您有一个包含所有注释的数组,您可以执
在代码中构建布局,一切都很好,直到我需要设置 map 区域: if let coordinate = item.location?.coordinate { let regio
我正在 iOS 中使用 Mapkit 创建一个应用程序,我真正想做的是在 map 上显示一个特定位置......暂停...... ..然后缩放到用户位置。 我的代码中有以下内容,但不知道如何应用暂停或
我制作了一个具有 map 功能的应用程序。 每当我的 Controller 打开包含 map 的屏幕时,内存使用量直接从 45mb 增加到 180MB。 我刚刚使用了一个 MKMap 并设置了它的 d
我正在尝试将一些 MKOverlays 添加到 MKMapKit。我将使用我们的 WMS 服务器根据谷歌的网格位置标准获取图 block 并添加它们。要调用电话,我需要告诉我们的 WMS 服务器谷歌图
如何获取所有可见的 MKAnnotationViews - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotati
请有人帮忙解决一个 MapView 问题。我有一个自定义注释。当用户点击注释时,我希望它向上移动屏幕然后消失。作为动画代码的测试(因为我是新手!),我尝试了以下操作: func mapView(_ m
我认为这将是相当普遍的事情 - 但无法在任何地方找到它。我知道如何将图像放在 map 上。我使用在这里找到的 CSMapAnnotation http://spitzkoff.com/craig/?p
Apple 示例代码 TileMap 很好地展示了如何使用 gdal2tiles 添加光栅图像叠加层,但这取决于设备上的 tile 目录。调整代码以从 Web 加载文件的最佳方法是什么? 我注意到它做
我是一名优秀的程序员,十分优秀!