更改地图图钉(颜色或图像)

Posted

技术标签:

【中文标题】更改地图图钉(颜色或图像)【英文标题】:Change map pin (color or image) 【发布时间】:2014-03-07 19:52:04 【问题描述】:

我知道这是一个经常被问到的问题。一段时间以来,我一直在阅读有关此问题的许多问题。我希望有人能用更简单的方法使用我必须用图像替换地图上的图钉的代码。我对目标 c 仍然很陌生。我拥有的代码非常简短。我在网上找到的大部分用于在地图上张贴图钉的内容都很长而且很长。我的地图视图有以下类和控制器:

LocationsViewController.h

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


@interface LocationsViewController : UIViewController <MKMapViewDelegate>


    IBOutlet MKMapView *worldView;
    IBOutlet UIActivityIndicatorView *activityIndicator;


@end

LocationsViewController.m

 #import "LocationsViewController.h"
#import "MapAnnotation.h"
@interface LocationsViewController ()


@end

@implementation LocationsViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        // Initialization code


    
    return self;




- (void)viewDidLoad

    [super viewDidLoad];
    // Do any additional setup after loading the view.


    CLLocationCoordinate2D wisconsin = .latitude =  xx.xxxxxxx, .longitude =  -xx.xxxxxx;
    CLLocationCoordinate2D pennsyvainia = .latitude =  xx.xxxxxxx, .longitude =  -xx.xxxxxx;
    CLLocationCoordinate2D nevada = .latitude =  xx.xxxxxxx, .longitude =  -xx.xxxxxx;
    CLLocationCoordinate2D texas = .latitude =  xx.xxxxxxx, .longitude =  -xx.xxxxxx;
    MapAnnotation *pinWI = [[MapAnnotation alloc] initWithCoordinate: wisconsin];
    MapAnnotation *pinPA = [[MapAnnotation alloc] initWithCoordinate: pennsyvainia];
    MapAnnotation *pinNV = [[MapAnnotation alloc] initWithCoordinate: nevada];
    MapAnnotation *pinTX = [[MapAnnotation alloc] initWithCoordinate: texas];

    [worldView addAnnotation:pinTX];
    [worldView addAnnotation:pinNV];
    [worldView addAnnotation:pinWI];
    [worldView addAnnotation:pinPA];



MapAnnotation.h

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

@interface MapAnnotation : NSObject <MKAnnotation> 
    CLLocationCoordinate2D _coordinate;
    MKAnnotationView *mapView;


- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;

@end

MapAnnotation.m

#import "MapAnnotation.h"
#import "LocationsViewController.h"

@implementation MapAnnotation
@synthesize coordinate = _coordinate;

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate

    self = [super init];

    if (self != nil)
    
        _coordinate = coordinate;
    

    return self;

@end

我尝试过实现类似的方法:

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

使用各种类型的代码,但无济于事。不知道我做错了什么。没有比我目前在网上看到的更短的方法来做我在想的事情。

我希望有类似的东西:

MapAnnotation *pinWI = [[[MapAnnotation alloc] initWithCoordinate: wisconsin] initWithPinImage:@"imagename.png"];

或者我可以实现类似的东西: pinWI:MKPinAnnotationColorGreen; 仅定义了图像位置?

或类似的东西。提前感谢大家对新手的帮助和耐心。

我真的需要将图钉更改为自定义图像。我尝试过的一种编码模式是这样的:

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

    static NSString *AnnotationViewID = @"annotationViewID";

    MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil)
    
        annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
    

    annotationView.image = [UIImage imageNamed:@"location.png"];
    annotationView.annotation = annotation;

    return annotationView;

工作代码:

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


    static NSString *AnnotationViewID = @"annotationViewID";

    MKAnnotationView *annotationView = (MKAnnotationView *)[worldView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil)
    
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
    

    annotationView.image = [UIImage imageNamed:@"locations_marker.png"];
    annotationView.annotation = annotation;

    return annotationView;

【问题讨论】:

正如你所说,有 许多 问题(和答案)(在 SO 和文档中)。您要设置图钉颜色还是要使用自定义图像? 您能否举一个您在 viewForAnnotation 中尝试过的“各种类型的代码”的示例?用代码更新您的问题。 试试这个 更改标记图标会有所帮助 [Change Marker Icon][1] [1]:***.com/questions/13761864/how-to-change-marker-icon 你把viewForAnnotation方法放在什么类里了?是在 LocationsViewController 还是 MapAnnotation 中? @Anna 在阅读了您的帖子“您将 viewForAnnotation 方法放在哪个类中?是在 LocationsViewController 还是 MapAnnotation 中?-”之后,我将所有内容都移到了 LocationViewController 中,并对代码进行了轻微更改工作。非常感谢您的帮助。我相信在不久的将来我会再次需要它。我在上面发布了我在 LocationViewController.m 中使用的代码。 【参考方案1】:

类型属性(example-firstPin、secondPin、thirdPin 等)添加到您的自定义MapViewAnnotation 类中。

MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] init...
newAnnotation.type = @"firstPin";  // <-- set property in annotation
[self.mapView addAnnotation:newAnnotation];

并在您的 - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id &lt;MKAnnotation&gt;) annotation 方法中进行简单检查

MapViewAnnotation *mvAnn = (MapViewAnnotation *)annotation;
if ([mvAnn.type isEqualToString:@"firstPin"])

    annView.pinColor = MKPinAnnotationColorGreen;

else if ([mvAnn.type isEqualToString:@"secondPin"])

    annView.pinColor = MKPinAnnotationColorRed;

else if ([mvAnn.type isEqualToString:@"thirdPin"])

    annView.pinColor = MKPinAnnotationColorOrange;

【讨论】:

查看我上面添加的注释和代码。所以不会让我在评论中发布它,因为它太长了。 @pawan 请帮我解决这个问题***.com/questions/35647839/…

以上是关于更改地图图钉(颜色或图像)的主要内容,如果未能解决你的问题,请参考以下文章

谷歌地图图标(图钉) - 如何定义颜色?

选择注释引脚时更改引脚颜色

更改谷歌地图当前位置蓝点颜色 iOS

当用户点击地图图钉时动态更改图标图像文件:谷歌地图

无法更改图钉 iOS MapKit 的颜色

将地图标记设置为自定义颜色 Android