Daniel Fernandez Daniel Fernandez - 1 month ago 6
iOS Question

Use an image in map pin Xcode

I am developing an application in Xcode using objective-c. My problem is that I trying to change the original red map pin for a custom image. I have the code but the image pin only appears when you press the locate button. I want than the custom pin image appears when you load the map view. I was looking for help on internet but I don't know how I can do it in my case.

Can someone help me? Thank you!

My RestMapPin.h:

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

@interface RestMapPin : NSObject <MKAnnotation> {

CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}

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

@end


My RestMapPin.m

#import "RestMapPin.h"

@implementation RestMapPin

@synthesize coordinate, title, subtitle;

@end


My MapViewController.h

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

@interface MapViewController : UIViewController {

MKMapView *mapView;
}

@property (weak, nonatomic) IBOutlet UIBarButtonItem *barButton;

@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property(nonatomic, retain) CLLocationManager *locationManager;

-(IBAction)setMap:(id)sender;

-(IBAction)GetLocation:(id)sender;

@end


My MapViewController.m

#import "MapViewController.h"
#import "SWRevealViewController.h"
#import "RestMapPin.h"

@interface MapViewController ()

@end

@implementation MapViewController

@synthesize mapView;

- (void)viewDidLoad {
[super viewDidLoad];

_barButton.target = self.revealViewController;
_barButton.action = @selector(revealToggle:);

[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

[self.navigationItem setTitle:@"Mapa"]; /*Cambia el titulo del navigation controller*/

[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; /*Cambia el color de las letras del navigation controller bar del menu principal*/

[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:27/255.0f green:101/255.0f blue:163/255.0f alpha:1.0f]];

self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; /*Cambia el color del boton de la izquierda*/

/*80 Grados*/
MKCoordinateRegion region_80_grados = { {0.0, 0.0}, {0.0, 0.0}};
region_80_grados.center.latitude = 40.42871179999999;
region_80_grados.center.longitude = -3.703639100000032;
region_80_grados.span.longitudeDelta = 0.1f;
region_80_grados.span.latitudeDelta = 0.1f;
[mapView setRegion:region_80_grados animated:YES];

RestMapPin *ann_80_grados = [[RestMapPin alloc] init];
ann_80_grados.title = @"80 Grados";
ann_80_grados.subtitle = @"Malasaña";
ann_80_grados.coordinate = region_80_grados.center;
[mapView addAnnotation:ann_80_grados];

/*90 Grados*/
MKCoordinateRegion region_90_grados = { {0.0, 0.0}, {0.0, 0.0}};
region_90_grados.center.latitude = 40.4164161;
region_90_grados.center.longitude = -3.6699459999999817;
region_90_grados.span.longitudeDelta = 0.1f;
region_90_grados.span.latitudeDelta = 0.1f;
[mapView setRegion:region_90_grados animated:YES];

RestMapPin *ann_90_grados = [[RestMapPin alloc] init];
ann_90_grados.title = @"90 Grados";
ann_90_grados.subtitle = @"Retiro";
ann_90_grados.coordinate = region_90_grados.center;
[mapView addAnnotation:ann_90_grados];

/*Barra de Pintxos 3*/
MKCoordinateRegion region_barra_de_pintxos3 = { {0.0, 0.0}, {0.0, 0.0}};
region_barra_de_pintxos3.center.latitude = 40.4364001;
region_barra_de_pintxos3.center.longitude = -3.680874499999959;
region_barra_de_pintxos3.span.longitudeDelta = 0.1f;
region_barra_de_pintxos3.span.latitudeDelta = 0.1f;
[mapView setRegion:region_barra_de_pintxos3 animated:YES];

RestMapPin *ann_barra_de_pintxos3 = [[RestMapPin alloc] init];
ann_barra_de_pintxos3.title = @"BaRRa de Pintxos";
ann_barra_de_pintxos3.subtitle = @"Barrio de Salamanca";
ann_barra_de_pintxos3.coordinate = region_barra_de_pintxos3.center;
[mapView addAnnotation:ann_barra_de_pintxos3];

}

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
if(annotation != mapView.userLocation) {
static NSString *defaultPinID = @"com.invasivecode.pin";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID];


pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"pin@2x.png"];
}
else {
//[mapView.userLocation setTitle:@"I am here"];
}
return pinView;
}



-(IBAction)setMap:(id)sender {

switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
case 0:
mapView.mapType = MKMapTypeStandard;
break;
case 1:
mapView.mapType = MKMapTypeSatellite;
break;
case 2:
mapView.mapType = MKMapTypeHybrid;
break;
default:
break;
}
}


/*Obtener la localización. Código adaptado para funcionar en iOS8*/
-(IBAction)GetLocation:(id)sender {


mapView.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;

[self.locationManager requestWhenInUseAuthorization];

[self.locationManager startUpdatingLocation];

mapView.showsUserLocation = YES;
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];

/***************************************/

[super viewDidAppear:YES];

self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
NSLog(@"%@", [self deviceLocation]);

//View Area
MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = self.locationManager.location.coordinate.latitude;
region.center.longitude = self.locationManager.location.coordinate.longitude;
region.span.longitudeDelta = 0.001f;
region.span.longitudeDelta = 0.001f;
[mapView setRegion:region animated:YES];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}
- (NSString *)deviceLocation {
return [NSString stringWithFormat:@"latitude: %f longitude: %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];
}
- (NSString *)deviceLat {
return [NSString stringWithFormat:@"%f", self.locationManager.location.coordinate.latitude];
}
- (NSString *)deviceLon {
return [NSString stringWithFormat:@"%f", self.locationManager.location.coordinate.longitude];
}
- (NSString *)deviceAlt {
return [NSString stringWithFormat:@"%f", self.locationManager.location.altitude];
}

@end


Thank you very much for your help!

Answer

You need to write the below code inside viewDidLoad method because the image applied over pin when mapview's delegate method viewForAnnotation called.

Just configure the mapView first when vc loads.

mapView.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;

[self.locationManager requestWhenInUseAuthorization];

[self.locationManager startUpdatingLocation];

mapView.showsUserLocation = YES;
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];