gpt4 book ai didi

iOS- MKMapKit-如何在用户位置上放置注释?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:20:37 27 4
gpt4 key购买 nike

我的应用程序上有用户位置,但如何在当前用户位置上添加注释?我是否必须获取用户位置的经度和纬度并以这种方式删除注释?或者我该怎么做?

最佳答案

  1. 首先导入必要的框架(CoreLocation 和 MapKit)。
  2. 然后创建 Objective-C NSObject 类 Annotation
  3. 设置它的.h:

    #import <Foundation/Foundation.h>

    #import <CoreLocation/CoreLocation.h>

    #import <MapKit/MapKit.h>

    @interface Annotation : NSObject <MKAnnotation>

    @property (nonatomic) CLLocationCoordinate2D coordinate;
    @property (nonatomic, copy) NSString *title;
    @property (nonatomic, copy) NSString *subtitle;

    @end
  4. 设置它的 .m:

    #import "Annotation.h"

    @implementation Annotation
    @synthesize coordinate, title, subtitle;

    @end
    1. 设置viewDidLoad

         if ([CLLocationManager locationServicesEnabled]) {

      locationManager = [[CLLocationManager alloc] init];

      [locationManager setDelegate:self];

      [locationManager setDesiredAccuracy: kCLLocationAccuracyBestForNavigation];

      [locationManager startUpdatingLocation];

      }

      self.mapView.delegate = self;
    2. 设置didUpdateToLocation

         // IMPORT ANNOTATION

      - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {


      [locationManager stopUpdatingLocation];

      double miles = 3.10686;
      double scalingFactor = ABS((cos(2 * M_PI * newLocation.coordinate.latitude / 360.0)));

      MKCoordinateSpan span;

      span.latitudeDelta = miles/69.0;
      span.longitudeDelta = miles/(scalingFactor * 69.0);

      MKCoordinateRegion region;
      region.span = span;
      region.center = newLocation.coordinate;

      [self.mapView setRegion:region animated:YES];

      Annotation *annot = [[Annotation alloc] init];
      annot.coordinate = newLocation.coordinate;

      [self.mapView addAnnotation:annot];

      }

关于iOS- MKMapKit-如何在用户位置上放置注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11918775/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com