【问题标题】:How do I add custom pins to the iPhone MapKit?如何将自定义引脚添加到 iPhone MapKit?
【发布时间】:2010-04-23 14:06:30
【问题描述】:

我正在 iPhone 上测试 MapKit 框架,并且非常想将显示位置的标准图钉切换为名为“location.png”的图像。

如何修改我的代码以允许这样做?

主控制器

- (void)viewDidLoad
{
    [super viewDidLoad];

    //
    // Set the map center
    //
    CLLocationCoordinate2D coordinate;
    coordinate.latitude = 49.2802;
    coordinate.longitude = -123.1182;
    mapView.region = MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000);

    //
    // Set 10 random locations on the map for testing purposes
    //
    for(int i = 0; i < 10; i++)
    {
        CGFloat latDelta = rand()*.035/RAND_MAX -.02;
        CGFloat longDelta = rand()*.03/RAND_MAX -.015;

        CLLocationCoordinate2D newCoord = { coordinate.latitude + latDelta, coordinate.longitude + longDelta };
        MapAnnotation* annotation = [[MapAnnotation alloc] initWithCoordinate:newCoord];
        [mapView addAnnotation:annotation];
        [annotation release];
    }

    [mapView setDelegate:self];
}

MapAnnotation.h

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

@interface MapAnnotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D _coordinate;
}

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;

@end

MapAnnotation.m

#import "MapAnnotation.h"

@implementation MapAnnotation
@synthesize coordinate = _coordinate;

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate
{
    self = [super init];

    if (self != nil)
    {
        _coordinate = coordinate;
    }

    return self;
}

@end

谢谢!

【问题讨论】:

    标签: objective-c iphone-sdk-3.0 mapkit


    【解决方案1】:

    我在查看MapCallouts的来源后解决了它

    这是我的解决方案:

    - (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;
    }
    

    【讨论】:

    【解决方案2】:

    使用此代码

    PlacePin.h

    #import <Foundation/Foundation.h>
    #import <MapKit/MapKit.h>
    
    @interface PlacePin : NSObject<MKAnnotation> {
    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle;
    int nTag;
    }
    
    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
    @property (nonatomic,copy) NSString *title; 
    @property (nonatomic,copy) NSString *subtitle;
    @property (nonatomic) int nTag;
    
    - (id)initWithLocation:(CLLocationCoordinate2D)coord;
    
    @end
    

    PlacePin.m

    #import "PlacePin.h"
    @implementation PlacePin
    
    @synthesize coordinate,title,subtitle;
    @synthesize nTag;
    
    - (id)initWithLocation:(CLLocationCoordinate2D)coord{   
    
    self = [super init];    
    if (self) {         
        coordinate = coord;     
    }
    return self;    
    }
    
    -(void)dealloc{
    [title release];
    [subtitle release];
    [super dealloc];
    }
    @end
    

    callingView.h

    #import <UIKit/UIKit.h>
    #import <MapKit/MapKit.h>
    #import "PlacePin.h"
    
    @interface ViewController : UIViewController<MKMapViewDelegate>{
    NSMutableArray *Arr_Latitude;
    CGFloat double_lat;
    CGFloat double_long;
    
    }
    @property (retain, nonatomic) IBOutlet MKMapView *mapView;
    -(void)zoomToFitMapAnnotations:(MKMapView*)mv;
    
    @end
    

    callingView.m

    - (void)viewDidLoad
     {
    [super viewDidLoad];
    //create demo latlong array
    NSMutableArray *arrLat=[[NSMutableArray alloc] initWithObjects:@"23.0333",@"24.0333",@"25.0333",@"26.0333" ,nil];
    NSMutableArray *arrLong=[[NSMutableArray alloc] initWithObjects:@"72.6167",@"71.6167",@"70.6167",@"71.6167", nil];
    NSMutableArray *arrTitle=[[NSMutableArray alloc] initWithObjects:@"Point1",@"Point2",@"Point3",@"Point4", nil];
    
    
    Arr_Latitude=[[NSMutableArray alloc] init];
    for(int i=0; i<[arrLat count];i++)
    {
        NSMutableDictionary *dictLatlong=[[NSMutableDictionary alloc] init];
        [dictLatlong setObject:[arrLat objectAtIndex:i] forKey:@"Latitude"];
        [dictLatlong setObject:[arrLong objectAtIndex:i] forKey:@"Longitude"];
        [dictLatlong setObject:[arrTitle objectAtIndex:i] forKey:@"Title"];
    
        [Arr_Latitude addObject:dictLatlong];
    
    }
    NSLog(@"--------------- %@",[Arr_Latitude description]);
    [arrLat release];
    [arrLong release];
    
    
    
    mapView.delegate = self;
    [mapView setMapType:MKMapTypeSatellite];
    
    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta=0.2;
    span.longitudeDelta=0.2;
    
    CLLocationCoordinate2D location;
    
    region.span = span;
    region.center = location;
    
    
    for (int i = 0; i<[Arr_Latitude count]; i++)
    {
        double_lat = [[[Arr_Latitude objectAtIndex:i]valueForKey:@"Latitude"] doubleValue];
        double_long = [[[Arr_Latitude objectAtIndex:i]valueForKey:@"Longitude"] doubleValue];
        NSString *Title=[[Arr_Latitude objectAtIndex:i]valueForKey:@"Title"];
    
        location.latitude = double_lat;
        location.longitude = double_long;
    
        PlacePin *mapPoint = [[PlacePin alloc] initWithLocation:location];
        mapPoint.nTag = i;
        [mapPoint setTitle:Title];
    
    
        //        mapPoint.title = [[locationArray objectAtIndex:i] Name];
    
        [mapView addAnnotation:mapPoint];
        mapPoint = nil;
    
        [mapView setRegion:region animated:YES];
        [mapView regionThatFits:region];
    }
    
    [self zoomToFitMapAnnotations:mapView];
    }
    
    
    -(void)zoomToFitMapAnnotations:(MKMapView*)mv
    {
    //NSLog(@"zoom To Fit Map Annotations");
    if([mv.annotations count] == 0)
        return;
    
    if([mv.annotations count] == 1) {
    
        MKCoordinateRegion region;
        MKCoordinateSpan span;
        span.latitudeDelta=0.2;
        span.longitudeDelta=0.2;
    
        for(PlacePin* annotation in mv.annotations){
    
            CLLocationCoordinate2D location;
            location.latitude = annotation.coordinate.latitude;
            location.longitude = annotation.coordinate.longitude;
            region.span=span;
            region.center=location;
    
            [mv setRegion:region animated:TRUE];
            [mv regionThatFits:region];
    
        }
    
    
    }else {
        CLLocationCoordinate2D topLeftCoord;
        topLeftCoord.latitude = -90;
        topLeftCoord.longitude = 180;
    
        CLLocationCoordinate2D bottomRightCoord;
        bottomRightCoord.latitude = 90;
        bottomRightCoord.longitude = -180;
    
        for(PlacePin* annotation in mv.annotations)
        {
            topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
            topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
    
            bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
            bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
        }
    
        MKCoordinateRegion region;
        region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
        region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
        region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides
        region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides
    
        region = [mv regionThatFits:region];
        [mv setRegion:region animated:YES];
    
    }
    
    }
    
    - (MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id <MKAnnotation>)annotation 
    {
    
    MKPinAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) 
    {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) 
            pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
        pinView.pinColor = MKPinAnnotationColorRed;
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
    } 
    else {
        [_mapView.userLocation setTitle:@"I am here"];
    }
    
    
    
    return pinView;
    }
    

    【讨论】:

      【解决方案3】:

      查看 MapCallouts 示例代码,其中有一个使用图像的自定义图钉。

      http://developer.apple.com/iphone/library/samplecode/MapCallouts/Introduction/Intro.html

      请注意,如果您在 OS 3.0 上进行测试,则需要注释掉一行代码(在您这样做之前它不会构建)。

      【讨论】:

        【解决方案4】:
        in.h file:
        float address_latitude;
        float address_longitude;
        
        - (void)viewDidLoad
        {
        
            CLLocationCoordinate2D coord1 =          CLLocationCoordinate2DMake(address_latitude,address_longitude);
            MKCoordinateSpan span = MKCoordinateSpanMake(0.0, 0.0);
            MKCoordinateRegion region1 = {coord1, span};
            MKPointAnnotation *annotation1 = [[MKPointAnnotation alloc] init];
            [annotation1 setTitle:@"Driver"];
            [annotation1 setCoordinate:coord1];
            [_map setRegion:region1];
            [_map addAnnotation:annotation1];
        
            }
        

        【讨论】:

        • 这甚至没有回答问题
        • 现在丢针的好例子,因为最初的问题是很多年前的。谢谢,请原谅典型的 S.O. snarks...他们是我不提交答案的原因。
        猜你喜欢
        • 1970-01-01
        • 2013-04-06
        • 2012-10-01
        • 1970-01-01
        • 2023-03-15
        • 2021-11-19
        • 1970-01-01
        • 2013-04-26
        • 1970-01-01
        相关资源
        最近更新 更多