MapKit mapView:viewForAnnotation 对图钉颜色没有影响

发布于 2024-12-09 18:33:25 字数 2421 浏览 1 评论 0原文

我似乎无法更改图钉颜色。

我让我的视图控制器扩展 并实现 mapView:viewForAnnotation 我已经很接近了,但一定缺少一些东西。任何帮助将不胜感激。

MainViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "StopAnnotation.h"

#define METERS_PER_MILE 1609.344

@interface MainViewController : UIViewController <MKMapViewDelegate> {

}
@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@end

MapViewController.m

#import "MainViewController.h"

@implementation MainViewController
@synthesize mapView=_mapView;

- (void)viewWillAppear:(BOOL)animated
{
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = 43.066667;
    zoomLocation.longitude = -89.4;

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, METERS_PER_MILE, METERS_PER_MILE);

    MKCoordinateRegion adjustRegion = [_mapView regionThatFits:viewRegion];

    [_mapView setRegion:adjustRegion animated:YES];
    [_mapView addAnnotation:[[StopAnnotation alloc] initWithCoordinate:zoomLocation]];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKPinAnnotationView *pav = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
    pav.pinColor = MKPinAnnotationColorPurple;
    return pav;
}

// the rest of the methods are default, i.e. viewDid* and shouldAutorotate*, etc...

StopAnnotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface StopAnnotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}
- (id)initWithCoordinate:(CLLocationCoordinate2D)c;
@end

StopAnnotation.m

#import "StopAnnotation.h"

@implementation StopAnnotation
@synthesize coordinate;

- (NSString *)subtitle {
    return @"subtitle";
}

- (NSString *)title {
    return @"title";
}

- (id)initWithCoordinate:(CLLocationCoordinate2D)c {
    coordinate = c;
    NSLog(@"%f,%f", c.latitude, c.longitude);
    return self;
}

@end

我正在做练习&代码主要来自这里

谢谢!

I can't seem to change the pin color.

I have my view controller extend <MKMapViewDelegate> and implement mapView:viewForAnnotation I'm close but must be missing something. any help would be appreciated.

MainViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "StopAnnotation.h"

#define METERS_PER_MILE 1609.344

@interface MainViewController : UIViewController <MKMapViewDelegate> {

}
@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@end

MapViewController.m

#import "MainViewController.h"

@implementation MainViewController
@synthesize mapView=_mapView;

- (void)viewWillAppear:(BOOL)animated
{
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = 43.066667;
    zoomLocation.longitude = -89.4;

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, METERS_PER_MILE, METERS_PER_MILE);

    MKCoordinateRegion adjustRegion = [_mapView regionThatFits:viewRegion];

    [_mapView setRegion:adjustRegion animated:YES];
    [_mapView addAnnotation:[[StopAnnotation alloc] initWithCoordinate:zoomLocation]];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKPinAnnotationView *pav = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
    pav.pinColor = MKPinAnnotationColorPurple;
    return pav;
}

// the rest of the methods are default, i.e. viewDid* and shouldAutorotate*, etc...

StopAnnotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface StopAnnotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}
- (id)initWithCoordinate:(CLLocationCoordinate2D)c;
@end

StopAnnotation.m

#import "StopAnnotation.h"

@implementation StopAnnotation
@synthesize coordinate;

- (NSString *)subtitle {
    return @"subtitle";
}

- (NSString *)title {
    return @"title";
}

- (id)initWithCoordinate:(CLLocationCoordinate2D)c {
    coordinate = c;
    NSLog(@"%f,%f", c.latitude, c.longitude);
    return self;
}

@end

I'm doing an exercise & the code was mostly from here

Thanks!!

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

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

发布评论

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

评论(1

旧夏天 2024-12-16 18:33:25

您必须将 MainViewController 设置为 mapView 的委托,即如果您没有设置,那么地图视图可能会生成默认引脚,如果您在其中放置断点,

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

它实际上会被调用吗?

You must set MainViewController as the delegate of mapView, ie if you are not then the map view might be generating default pins, if you put a breakpoint in

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

does it actually get called?

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