I'm designing an app for a restaurant (and also, mostly to practice with iOS dev).
For this restaurant info page (address, phone #, etc) I added a smaller Map on the bottom page to display the place where the restaurant is but:
how can I make this small map to display the restaurant location (I know the coordinates) and
how to code that if the user click on the map, it switches to Apple Map app with the same coordinates.
Thank you
You have to use the MKMapViewDelegate in your viewcontroller.h file and use the ma delegate's methods to handle the map.
Try with the code below to add the pin of the restaurant and center the map on it.
MKAnnotationView *point = [[MKAnnotationView alloc] initWithAnnotation:_station reuseIdentifier:#"restaurant"];
[map addAnnotation:point.annotation];
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.002;
span.longitudeDelta = 0.002;
region.span = span;
region.center = location.coordinate;
[map setRegion:region animated:YES];
After that, implement the didSelectAnnotationView method to recognize the pin pressed. In it you can open the Apple Map with on the restaurant's coordinates:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
NSString *title = #"title";
float latitude = ...;
float longitude = ...;
int zoom = 13;
NSString *stringURL = [NSString stringWithFormat:#"http://maps.apple.com?q=%##%1.6f,%1.6f&z=%d", title, latitude, longitude, zoom];
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
}
EDIT:
I'm working to an application similar to your, but in my case is about station.
MDStationController is the controller of the view that contains the map on the top (like yours). MDStation is the station.
// MDStationController.h
#import <UIKit/UIKit.h>
#import "MDStation.h"
#import <MapKit/MapKit.h>
#interface MDStationController : UIViewController <MKMapViewDelegate>
#property (nonatomic, retain) MDStation* station;
#end
// MDStationController.m
#implementation MDStationController
#synthesize station=_station;
[...]
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = _station.name;
stationView.map.delegate = self;
[self addAndcenterMapOnLocation:_station.location];
}
- (void) addAndcenterMapOnLocation:(CLLocation*)location {
MKAnnotationView *point = [[MKAnnotationView alloc] initWithAnnotation:_station reuseIdentifier:#"station"];
[stationView.map addAnnotation:point.annotation];
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.002;
span.longitudeDelta = 0.002;
region.span = span;
region.center = location.coordinate;
[stationView.map setRegion:region animated:YES];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
static NSString *defaultID = #"station";
MKAnnotationView *pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultID];
pinView.image = [UIImage imageNamed:#"bike_gray"];
return pinView;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
NSString *title = _station.name;
float latitude = _station.location.latitude;
float longitude = _station.location.longitude;
int zoom = 13;
NSString *stringURL = [NSString stringWithFormat:#"http://maps.apple.com?q=%##%1.6f,%1.6f&z=%d", title, latitude, longitude, zoom];
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
}
Related
I have an app in which I use a google API to find places near a user. If they tap a cell named "ATM" on my table view, they are directed to a MapView which will display their user location on the map. They then have to press ANOTHER button called "show" on the screen to plot the points on the nearest ATM's. I want my app to run so that they don't have to press the second button, and once the map loads their user location, the app searches for the nearest "xyz's" near them. Here is the code for my querySearch:
-(void) queryGooglePlaces: (NSString *) googleType
{
NSString *url = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%#&types=%#&sensor=true&key=%#", currentCentre.latitude, currentCentre.longitude, [NSString stringWithFormat:#"%i", currenDist], googleType, kGOOGLE_API_KEY];
//Formulate the string as URL object.
NSURL *googleRequestURL=[NSURL URLWithString:url];
// Retrieve the results of the URL.
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
[self performSelectorOnMainThread:#selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
Here is the method I want to eliminate. I want this code to run once the map has focused on the user's location:
- (IBAction)showPlace:(id)sender {
NSString *lowerString = [tappedString lowercaseString];
[self queryGooglePlaces:lowerString];
}
And lastly, here are my MapView delegate method's incase they help:
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
//Zoom back to the user location after adding a new set of annotations.
//Get the center point of the visible map.
CLLocationCoordinate2D centre = [mv centerCoordinate];
MKCoordinateRegion region;
//If this is the first launch of the app then set the center point of the map to the user's location.
if (firstLaunch) {
region = MKCoordinateRegionMakeWithDistance(locationManager.location.coordinate,1000,1000);
firstLaunch=NO;
}else {
//Set the center point to the visible region of the map and change the radius to match the search radius passed to the Google query string.
region = MKCoordinateRegionMakeWithDistance(centre,currenDist,currenDist);
}
//Set the visible region of the map.
[mv setRegion:region animated:YES];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
//Define our reuse indentifier.
static NSString *identifier = #"MapPoint";
if ([annotation isKindOfClass:[MapPoint class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.animatesDrop = YES;
return annotationView;
}
return nil;
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
//Get the east and west points on the map so we calculate the distance (zoom level) of the current map view.
MKMapRect mRect = self.mapView.visibleMapRect;
MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect));
MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect));
//Set our current distance instance variable.
currenDist = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint);
//Set our current centre point on the map instance variable.
currentCentre = self.mapView.centerCoordinate;
}
call - (IBAction)showPlace:(id)sender this method using perform selector once you have fetched all the data.
Use MKMapViewDelegate to know when the user has been found:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSString *lowerString = [tappedString lowercaseString];
[self queryGooglePlaces:lowerString];
}
MKMapViewDelegate reference
You should nest the dispatch_sync block inside dispatch_async block. Try like this:
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
dispatch_sync(kBgQueue, ^{
[self fetchedData:data];
})
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm making this app where people can see what tourist attractions are near them and I'm a bit stuck!
I need it to load the user's current location when the app is loaded and zoom to it, however it's not doing so.
Sorry to be a bit blunt but I really don't know how to set it up so that it will work with both the button and load their location automatically.
https://pbs.twimg.com/media/BcRSTk0CAAEV47Q.jpg
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface ViewController : UIViewController{
MKMapView *mapview;
}
#property (weak, nonatomic) IBOutlet MKMapView *myMapView;
-(IBAction)getlocation;
#end
ViewController.m
#import "ViewController.h"
#import "Annotation.h"
#interface ViewController ()
#end
//Thorpe Park Coordinates
#define THORPE_LATITUDE 51.40395;
#define THORPE_LONGITUDE -0.51433;
//London Eye Coordinates
#define LONDONEYE_LATITUDE 51.50340;
#define LONDONEYE_LONGITUDE -0.11952;
//The New London Dungeons
#define LONDONDUGNEONS_LATITUDE 51.50231;
#define LONDONDUGNEONS_LONGITUDE -0.11965;
//Span
#define THE_SPAN 0.50f;
#implementation ViewController
#synthesize myMapView;
-(IBAction)getlocation {
mapview.showsUserLocation = YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Create Region
MKCoordinateRegion myRegion;
//Center
CLLocationCoordinate2D center;
center.latitude = LONDONEYE_LATITUDE;
center.longitude = LONDONEYE_LONGITUDE;
//Span
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
myRegion.center = center;
myRegion.span = span;
//Set our mapView
[myMapView setRegion:myRegion animated:YES];
//Annotation's
NSMutableArray * locations = [ [NSMutableArray alloc] init];
CLLocationCoordinate2D location;
Annotation *myAnn;
//London Eye
myAnn = [[Annotation alloc] init];
location.latitude = LONDONEYE_LATITUDE;
location.longitude = LONDONEYE_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = #"The London Eye";
myAnn.subtitle = #"";
[locations addObject:myAnn];
//The New London Dungeons
myAnn = [[Annotation alloc] init];
location.latitude = LONDONDUGNEONS_LATITUDE;
location.longitude = LONDONDUGNEONS_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = #"The London Dungeons";
myAnn.subtitle = #"";
[locations addObject:myAnn];
//Thorpe Park
myAnn = [[Annotation alloc] init];
location.latitude = THORPE_LATITUDE;
location.longitude = THORPE_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = #"Thorpe Park";
myAnn.subtitle = #"";
[locations addObject:myAnn];
[self.myMapView addAnnotations:locations];
/*
//1. Create a coordinate to be used with pin
CLLocationCoordinate2D ThorpeLocation;
ThorpeLocation.latitude = THORPE_LATITUDE;
ThorpeLocation.longitude = THORPE_LONGITUDE;
Annotation * myAnnotation = [Annotation alloc];
myAnnotation.coordinate = ThorpeLocation;
myAnnotation.title = #"Thorpe Park";
myAnnotation.subtitle = #"Theme Park";
[self.myMapView addAnnotation:myAnnotation];
*/
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Use this, and don't forget to put the delegate on your .h file, hope it works ;)
_mapView.userTrackingMode = YES;
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.00001;
span.longitudeDelta = 0.00001;
CLLocationCoordinate2D location = _mapView.userLocation.coordinate;
region.span = span;
region.center = location;
[_mapView setRegion:region animated:TRUE];
[_mapView regionThatFits:region];
}
I'm making an app with multiple annotations on a mapview. I succeeded in displaying the annotations on the mapview.
I want to use the app to find different stores in one country. So I have all the coordinates and I want that when an annotation is clicked, the Maps app gets launched and the user can get a route from his current location.
My problem is that when I use the calloutAccessoryControlTapped function, every annotation displays the information of the first coordinates I filled in.
This is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
[self plotAnnotations];
}
-(void)plotAnnotations
{
CLLocationCoordinate2D coordinate1;
coordinate1.latitude = 52.511917;
coordinate1.longitude = 4.994776;
MyLocation *annotation = [[MyLocation alloc] initWithCoordinate:coordinate1 title:#"Basic-Fit Purmerend"];
CLLocationCoordinate2D coordinate2;
coordinate2.latitude = 51.972618;
coordinate2.longitude = 5.310799;
MyLocation *annotation2 = [[MyLocation alloc] initWithCoordinate:coordinate2 title:#"Basic-Fit Aalsmeer"];
[self.mapView addAnnotation:annotation];
[self.mapView addAnnotation:annotation2];
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
}
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
CLLocationCoordinate2D coordinate1;
coordinate1.latitude = 52.511917;
coordinate1.longitude = 4.994776;
NSDictionary *addressDict1 = #{(NSString*)kABPersonAddressStreetKey:#"Grotenhuysweg 100, 1447 Purmerend, Nederland"};
MKPlacemark *placeMark1 = [[MKPlacemark alloc]initWithCoordinate:coordinate1 addressDictionary:addressDict1];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placeMark1];
[mapItem setName:#"Basic-Fit Purmerend"];
[mapItem setUrl:[NSURL URLWithString:#"http://www.basic-fit.nl/sportschool/Purmerend"]];
CLLocationCoordinate2D coordinate2;
coordinate2.latitude = 51.972618;
coordinate2.longitude = 5.310799;
NSDictionary *addressDict2 = #{(NSString*)kABPersonAddressStreetKey:#"Molenvliet 18A, Aalsmeer, Nederland"};
MKPlacemark *placeMark2 = [[MKPlacemark alloc]initWithCoordinate:coordinate2 addressDictionary:addressDict2];
MKMapItem *mapItem2 = [[MKMapItem alloc] initWithPlacemark:placeMark2];
[mapItem2 setName:#"Basic-Fit Aalsmeer"];
[mapItem2 setUrl:[NSURL URLWithString:#"http://www.basic-fit.nl/sportschool/aalsmeer"]];
NSArray *mapPoints = #[mapItem];
[MKMapItem openMapsWithItems:mapPoints launchOptions:nil];
NSArray *mapPoints1 =#[mapItem2];
[MKMapItem openMapsWithItems:mapPoints1 launchOptions:nil];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *identifier = #"MyLocation";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annotationView)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:#"arrest.png"];//here we use a nice image instead of the default pins
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
} else {
annotationView.annotation = annotation;
}
return annotationView;
}
I eventually want to display more than 2 annotations on the map. But when I select an annotation, the data provided by coordinate1 are displayed.
How can I solve this?
Nice work so far. The problem is in the -mapView:annotationView:calloutAccessoryControlTapped: delegate method. This method is called when an annotation on the map view is tapped. You're trying to configure and open the Maps app for both of your example annotations in this delegate method which you shouldn't be doing. You should be opening the Maps app using the annotation view that you're given access to. The reason why the first annotation's details are being shown no matter what is because
NSArray *mapPoints = #[mapItem];
[MKMapItem openMapsWithItems:mapPoints launchOptions:nil];
will get called first any time you tap on an annotation.
Here's some stuff that I suggest you do.
You should modify your MyLocation class to have a NSDictionary property that will hold the address and a NSURL property to hold your URL.
So your -plotAnnotations method would look like this now:
-(void)plotAnnotations
{
CLLocationCoordinate2D coordinate1;
coordinate1.latitude = 52.511917;
coordinate1.longitude = 4.994776;
MyLocation *annotation = [[MyLocation alloc] initWithCoordinate:coordinate1 title:#"Basic-Fit Purmerend"];
// Set the address for this annotation
annotation.address = #{(NSString*)kABPersonAddressStreetKey:#"Grotenhuysweg 100, 1447 Purmerend, Nederland"};
annotation.url = [NSURL URLWithString:#"http://www.basic-fit.nl/sportschool/Purmerend"];
CLLocationCoordinate2D coordinate2;
coordinate2.latitude = 51.972618;
coordinate2.longitude = 5.310799;
MyLocation *annotation2 = [[MyLocation alloc] initWithCoordinate:coordinate2 title:#"Basic-Fit Aalsmeer"];
// Set the address for this annotation
annotation2.address = #{(NSString*)kABPersonAddressStreetKey:#"Molenvliet 18A, Aalsmeer, Nederland"};
annotation.url = [NSURL URLWithString:#"http://www.basic-fit.nl/sportschool/aalsmeer"];
[self.mapView addAnnotation:annotation];
[self.mapView addAnnotation:annotation2];
}
Now your -mapView:annotationView:calloutAccessoryControlTapped: delegate method will look like this:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
// You can retrieve your annotation using the annotation property of MKAnnotationView
MyLocation *annotation = (MyLocation *)view.annotation;
// Then you configure everything like you were doing
MKPlacemark *placeMark = [[MKPlacemark alloc] initWithCoordinate:annotation.coordinate addressDictionary:annotation.address];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placeMark];
[mapItem setName:annotation.title];
[mapItem setUrl:annotation.url];
[MKMapItem openMapsWithItems:#[mapItem] launchOptions:nil];
}
Something better to do would be to create a class called Place that has properties that stores all of your data pieces. Then you would modify your MyLocation class to initialize with your Place class. Then you would just access your Place class every time you have access to an annotation.
I created a map view in my app. Now I want the user to be able to add several addresses to drop pins at those locations. I also want the user to be able to remove these pins.
Does anyone know where I can find a good tutorial, or where to start? I have no experience with mapviews...
try this......
-
(void)ShowPins
{
activity.hidden=YES;
[activity stopAnimating];
double lat;
double lng;
for (int ijk=0; ijk<arrayLocationList.count; ijk++)
{
/*Set your lat and long here*/
lat=[[[[arrayLocationList objectAtIndex:ijk]objectForKey:#"location"] objectForKey:#"lat"] doubleValue];
lng=[[[[arrayLocationList objectAtIndex:ijk]objectForKey:#"location"] objectForKey:#"lng"] doubleValue];
CLLocationCoordinate2D geos = CLLocationCoordinate2DMake(lat, lng);
MKPlacemark* marker = [[MKPlacemark alloc] initWithCoordinate:geos addressDictionary:nil];
[mapVieww addAnnotation:marker];
}
CLLocationCoordinate2D coord1 = {.latitude = lat, .longitude =lng};
MKCoordinateSpan span = {.latitudeDelta = .03,.longitudeDelta = .03};
MKCoordinateRegion region = {coord1, span};
[mapVieww setRegion:region];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
{
MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"annotation1"];
UILabel *lable=[[UILabel alloc]init];
[newAnnotation addSubview:lable];
newAnnotation.pinColor = MKPinAnnotationColorRed;
newAnnotation.animatesDrop = YES;
newAnnotation.canShowCallout = NO;
[newAnnotation setSelected:YES animated:YES];
return newAnnotation;
}
To Drop pins at several locations u have to try this code:
You need to put all those location latitude and longitude in an array and call this function everytime after incrementing the num value
-(void)Mapview
{ NSMutableArray* annotations=[[NSMutableArray alloc] init];
CLLocationCoordinate2D theCoordinate1;
theCoordinate1.latitude = [latit[num]doubleValue];
theCoordinate1.longitude = [longit[num]doubleValue];
Annotation* myAnnotation1=[[Annotation alloc] init];
myAnnotation1.coordinate=theCoordinate1;
[map_view addAnnotation:myAnnotation1];
map_view.showsUserLocation = NO;
[annotations addObject:myAnnotation1];
MKMapRect rect = MKMapRectNull;
for (id <MKAnnotation> annotation in annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 20);
if (MKMapRectIsNull(rect)) {
rect = pointRect;
} else {
rect = MKMapRectUnion(rect, pointRect);
}
}
map_view.visibleMapRect = rect;
}
You can edit this code by adding leftcallout acessoryview or modify the contents in callout view .
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
map_view.showsUserLocation= NO;
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
MKPinAnnotationView* pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:#"AnnotationIdentifier"];
if(pinView == nil)
{
pinView = pinView=[[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:#"currentloc"] ;
pinView.centerOffset = CGPointMake(0,60);
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
}
return pinView;
}
To Remove the pins you can use this code
[self.map_view removeAnnotations:map_view.annotations];
(or)
[self.map_view removeAnnotations: [self.map_view.annotations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"!(self isKindOfClass: %#)", [MKUserLocation class]]]];
I read the current location's latitude and longitude values and then pin that location in iPhone successfully. Now I want to read that place name using this latitude and longitude values.
I used the following code to read the find the current location:
- (void)mapView:(MKMapView *)mapView1 didUpdateUserLocation:(MKUserLocation *)userLocation{
CLLocation *whereIAm = userLocation.location;
NSLog(#"I'm at %#", whereIAm.description);
latitude = whereIAm.coordinate.latitude;
longitude = whereIAm.coordinate.longitude;
NSLog(#"LAT : %f",latitude);
NSLog(#"LONG : %f",longitude);
mapView.mapType=MKMapTypeStandard;
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude =latitude; //22.569722 ;
region.center.longitude = longitude; //88.369722;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = #" Welcome";
ann.subtitle = #"Hi";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
}
- (void)viewDidLoad {
[super viewDidLoad];
mapView.showsUserLocation = YES;
mapView.delegate = self;
}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation: (id<MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
}
[mapView.userLocation setTitle:#"I am here"];
return pinView;
}
Use the MKReverseGeocoderDelegate Protocol like this :
#interface YourViewController : UIViewController <MKReverseGeocoderDelegate>
{
}
#implementation YourViewController
- (void)mapView:(MKMapView *)mapView1 didUpdateUserLocation:(MKUserLocation *)userLocation{
CLLocation *whereIAm = userLocation.location;
MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:whereIAm.coordinate];
reverseGeocoder.delegate = self;
[reverseGeocoder start];
}
Now implement the following method delegate to get the response :
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
NSLog(#"I'm at %#", placemark.locality);
}
For more informations look at this : http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKReverseGeocoder_Class/