【问题标题】:User Current Location on specific position on MAP地图上特定位置的用户当前位置
【发布时间】:2023-03-27 23:01:02
【问题描述】:
  • MKMapView
  • 核心位置
  • Swift3.0

我有这样的要求:

创建一个应用程序,在该应用程序中,我从 webservice 获取不同位置的 Lat and Long。使用自定义 AnnotationView 在地图上绘制这些位置。除此之外,我正在展示User Current Location with Custom Image

这里一切正常fine

现在,当用户移动时,用户当前位置图钉会改变其位置,因此对此有一个重要要求,我需要使当前位置位置保持在地图底部 (just 100 pixel)从下往上。

我做了一些 RND,发现了一些有用的链接:

但这不再起作用了。 请建议我如何在iOS 地图中设置用户当前位置图钉,使其始终位于地图底部(距底部 100 像素以上)

所需的引脚位置:

目前显示在其他位置。

注意 - **不希望 Google 地图提供此类功能

【问题讨论】:

  • 我不太明白您的问题,您是否希望 1.即使用户四处移动时也可以将图钉显示在该位置,或者 2.当用户移动时将地图固定到该位置一个动作或 3. 地图是否改变了大头针在屏幕上的位置?您可能想更清楚地描述您的问题或添加一些图像以显示您想要的结果
  • @Ben Ong 我想让图钉在该位置可见,即使用户四处移动
  • @BenOng 我用屏幕截图更新了问题,每当用户缩放和第一次屏幕显示时,我都需要相同的引脚位置
  • @vadian 请分享您对此的建议:stackoverflow.com/q/41057877/3400991

标签: ios swift3 mkmapview core-location


【解决方案1】:

如果您一直在使用类似mapView.setUserTrackingMode(.Follow, animated: true) 的东西,那么用户位置将始终显示在地图的中心,每当用户位置更新时,缩放/平移都会自动调整。

您需要禁用UserTrackingMode,然后每次更改用户位置时使用func setVisibleMapRect(_ mapRect: MKMapRect, edgePadding insets: UIEdgeInsets, animated animate: Bool) 手动调整地图缩放/平移。

我在类似情况下使用了以下代码

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)

附言edgePadding 在这种情况下是你的朋友。

【讨论】:

  • 所以我需要在哪里使用此代码,我的意思是哪个委托
  • 我用屏幕截图更新了问题,每当用户缩放和第一次屏幕显示时,我都需要相同的引脚位置
  • 我会选择这个答案,您可以在 CLLocationMangerDelegate 的 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 中使用这些代码 - 可能是您的视图控制器或您的超级视图地图视图
【解决方案2】:

以下是用于定位地图的技巧,即 UserLocation 引脚会自动调整:

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)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    相关资源
    最近更新 更多