MKMapView自定义UserLocation注解

发布于 2024-10-21 05:53:30 字数 378 浏览 2 评论 0原文

我希望自定义 MKMapView userLocation 注释,以像官方地图应用程序一样绘制标题箭头。

我正在尝试在 viewForAnnotation: 中执行此操作,

 - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
 {

    if ([annotation isKindOfClass:[MKUserLocation class]]) {

    }
 }

但我无法找到下一步要做什么来获取 UserLocation 的视图并对其进行自定义。

非常感谢...

I am looking to customize the MKMapView userLocation annotation, to draw a heading arrow like the official map app.

I am trying to do this in viewForAnnotation:

 - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
 {

    if ([annotation isKindOfClass:[MKUserLocation class]]) {

    }
 }

but I am unable to find what to do next to get the view for UserLocation and customize it.

Many thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悟红尘 2024-10-28 05:53:30

我创建了 SVPulsingAnnotationView,它是 MKUserLocationView 的可自定义副本。

在此处输入图像描述

您可以像任何其他 MKAnnotationView 一样实例化它:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    if([annotation isKindOfClass:[MyAnnotationClass class]]) {
        static NSString *identifier = @"currentLocation";
        SVPulsingAnnotationView *pulsingView = (SVPulsingAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if(pulsingView == nil) {
            pulsingView = [[SVPulsingAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            pulsingView.annotationColor = [UIColor colorWithRed:0.678431 green:0 blue:0 alpha:1];
        }

        pulsingView.canShowCallout = YES;
        return pulsingView;
    }

    return nil;
}

I created SVPulsingAnnotationView, a customizable replica of MKUserLocationView.

enter image description here

You instantiate it just like any other MKAnnotationView:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    if([annotation isKindOfClass:[MyAnnotationClass class]]) {
        static NSString *identifier = @"currentLocation";
        SVPulsingAnnotationView *pulsingView = (SVPulsingAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if(pulsingView == nil) {
            pulsingView = [[SVPulsingAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            pulsingView.annotationColor = [UIColor colorWithRed:0.678431 green:0 blue:0 alpha:1];
        }

        pulsingView.canShowCallout = YES;
        return pulsingView;
    }

    return nil;
}
一场春暖 2024-10-28 05:53:30

默认用户位置标记的类(至少在 SDK 4.2 中)是 MKUserLocationView,但它不是公开的,因此您无法创建实例进行修改,否则会面临被应用商店拒绝的风险。您必须创建自己的 MKAnnotationView (或 MKAnnotationView 的子类)。

The class for the default user location marker (at least in SDK 4.2) is MKUserLocationView, but this is not public so you cannot create an instance to modify without risking rejection from the app store. You'll have to create your own MKAnnotationView (or subclass of MKAnnotationView) instead.

兔小萌 2024-10-28 05:53:30

导入 UIKit
导入地图工具包
import CoreLocation

class secondaryViewController: UIViewController, CLLocationManagerDelegate,MKMapViewDelegate{

@IBOutlet var mapview: MKMapView!

// var locationmanager = CLLocationManager()

let locationManager = CLLocationManager()
var currentLocation: CLLocation!

/* public func locationManager(_ manager: CLLocationManager, didUpdateLocationslocations: [CLLocation]) {

    let location = locations[0]

    let span = MKCoordinateSpanMake(0.1, 0.1)

   let my = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)

    print(my)
    let region = MKCoordinateRegionMake(my, span)

    mapview.setRegion(region, animated: true)

    self.mapview.showsUserLocation = true
}*/

override func viewDidLoad() {
    super.viewDidLoad()

 /*   mapview.mapType = MKMapType.standard
    locationmanager.delegate = self
    locationmanager.desiredAccuracy = kCLLocationAccuracyBest
    locationmanager.requestWhenInUseAuthorization()
 /*   isAuthorizedtoGetUserLocation()
    if CLLocationManager.locationServicesEnabled() {
        locationmanager.delegate = self
        locationmanager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    }*/

    locationmanager.startUpdatingLocation()*/

import UIKit
import MapKit
import CoreLocation

class secondViewController: UIViewController, CLLocationManagerDelegate,MKMapViewDelegate{

@IBOutlet var mapview: MKMapView!

// var locationmanager = CLLocationManager()

let locationManager = CLLocationManager()
var currentLocation: CLLocation!

/* public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let location = locations[0]

    let span = MKCoordinateSpanMake(0.1, 0.1)

   let my = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)

    print(my)
    let region = MKCoordinateRegionMake(my, span)

    mapview.setRegion(region, animated: true)

    self.mapview.showsUserLocation = true
}*/

override func viewDidLoad() {
    super.viewDidLoad()

 /*   mapview.mapType = MKMapType.standard
    locationmanager.delegate = self
    locationmanager.desiredAccuracy = kCLLocationAccuracyBest
    locationmanager.requestWhenInUseAuthorization()
 /*   isAuthorizedtoGetUserLocation()
    if CLLocationManager.locationServicesEnabled() {
        locationmanager.delegate = self
        locationmanager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    }*/

    locationmanager.startUpdatingLocation()*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文