MapKit Annotations Not Appearing - ios

I'm having issues getting annotations to show up on a map in an iOS application.
I have followed the conventions in the documentation. From debugging it seems that viewForAnnotation is never being called for the annotation point.
The code for the View Controller is:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super
// Do any additional setup after loading the view, typically from a nib.
self.mapView.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager requestAlwaysAuthorization];
[self.locationManager startUpdatingLocation];
self.mapView.showsUserLocation = YES;
[self.mapView setMapType:MKMapTypeStandard];
[self.mapView setZoomEnabled:YES];
[self.mapView setScrollEnabled:YES];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
//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.005f;
region.span.longitudeDelta = 0.005f;
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotations:[self createAnnotations]];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(#"viewForAnnotation");
if([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] init];
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"foo"];
[pinView setPinColor:MKPinAnnotationColorGreen];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
return pinView;
}
- (NSMutableArray *)createAnnotations
{
NSMutableArray *annotations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D coord;
coord.latitude = 37.4;
coord.longitude = -122.2;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotations addObject:annotation];
return annotations;
}
#end

One obvious problem is here in the createAnnotations method:
CLLocationCoordinate2D coord;
coord.latitude = 37.4;
coord.longitude = -122.2;
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotations addObject:annotation];
The coordinate property of the annotation is never set.
Set it after creating the annotation:
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = coord; // <-- add this line
[annotations addObject:annotation];

Related

Pin not showing on Mapkit - obj-c

I'm a newbie to ios dev. I tried to add pin annotations on map using mapkit. However, after tried the most basic methods, it still didn't show up, either that's a simple pin, or a custom annotationview. There is no error message, it's just the pin won't show up in the map.
Here's my code:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mapView.delegate = self;
NSLog(#"Custom Annotations start.");
// Custom Annotations.
CLLocationCoordinate2D bryanParkCoordinates = CLLocationCoordinate2DMake(37.785834, -122.406417);
MyCustomAnnotation *bryanParkAnnotation = [[MyCustomAnnotation alloc]initWithTitle:#"Bryant Park" Location:bryanParkCoordinates];
[self.mapView addAnnotation:bryanParkAnnotation];
// Simple pin.
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:bryanParkCoordinates];
[annotation setTitle:#"Title"];
[self.mapView addAnnotation:annotation];
// Another try.
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(37.330713, -121.894348);
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
MKCoordinateRegion region = {coord, span};
MKPointAnnotation *newannotation = [[MKPointAnnotation alloc] init];
[newannotation setCoordinate:coord];
[self.mapView setRegion:region];
[self.mapView addAnnotation:annotation];
// On your location.
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = bryanParkCoordinates;
point.title = #"Where am I?";
point.subtitle = #"I'm here!!!";
[self.mapView addAnnotation:point];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MyCustomAnnotation class]])
{
MyCustomAnnotation *myLocation = (MyCustomAnnotation *)annotation;
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"MyCustomAnnotation"];
if(annotationView == nil)
annotationView = myLocation.annotationView;
else
annotationView.annotation = annotation;
return annotationView;
}
else{
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"DETAILPIN_ID"];
[pinView setAnimatesDrop:YES];
[pinView setCanShowCallout:NO];
return pinView;
}
}
If I just want a simple pin on map, just adding those lines
// Simple pin.
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:bryanParkCoordinates];
[annotation setTitle:#"Title"];
[self.mapView addAnnotation:annotation];
should be enough? Is there other things that I could have missed? Like adding other delegate or something else other than adding those simple codes in 'viewDidLoad'? Thanks so much!
Managing Annotation Views
You should implement this delegate method and create a view for the annotation and return it.
basically when you add an annotation this delegates method will get called and you have to create the view for that annotation within this delegate method
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mapView.delegate = self;
NSLog(#"Custom Annotations start.");
// Custom Annotations.
CLLocationCoordinate2D bryanParkCoordinates = CLLocationCoordinate2DMake(37.785834, -122.406417);
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(37.330713, -121.894348);
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
MKCoordinateRegion region = {coord, span};
[self.mapView setRegion:region];
[self.mapView addAnnotation:annotation];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = bryanParkCoordinates;
point.title = #"Where am I?";
point.subtitle = #"I'm here!!!";
[self.mapView addAnnotation:point];
}
Try the following code:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if (annotation == mapView.userLocation) return nil;
static NSString* Identifier = #"PinAnnotationIdentifier";
MKPinAnnotationView* pinView;
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:Identifier];
if (pinView == nil) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:Identifier];
pinView.canShowCallout = YES;
return pinView;
}
pinView.annotation = annotation;
return pinView;
}
Don't forget to add <MKMapViewDelegate> to your #interface section.
#interface YourViewController () <MKMapViewDelegate>

mkmapview automatically zoom to current user location when trying to zoom in to other locations

He is desperately waiting for the solution... I am displaying the current user location and some custom annotations, but, when I am trying to zoom in into the map my map automatically redirecting to the current location with default zoom that is because of the span.please check my code and correct me.(i can able to zoom in other annotations when current location not displaying both simulator and device )
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
[self loadMultipleAnnotations];
self.map_view.delegate = self;
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
CLLocationCoordinate2D coordinate;
coordinate.latitude = newLocation.coordinate.latitude;
coordinate.longitude = newLocation.coordinate.longitude;
MKCoordinateSpan span;
span.latitudeDelta = 5;
span.longitudeDelta = 5;
MKCoordinateRegion region;
region.center = coordinate;
region.span= span;
[self.map_view setRegion:region];
MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];
pointAnnotation.coordinate = coordinate;
[self.map_view setRegion:region animated:YES];
double radius = 40000.0;
MKCircle *circle1 = [MKCircle circleWithCenterCoordinate:coordinate radius:radius];
circle1.title = #"Current location Marking";
[self.map_view addOverlay:circle1];
}
- (MKOverlayView *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKCircle class]])
{
MKCircleView *circleView = [[MKCircleView alloc] initWithOverlay:(MKCircle*)overlay];
circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.0];
circleView.strokeColor = [[UIColor redColor] colorWithAlphaComponent:0.7];
circleView.lineWidth = 3;
return circleView;
}
return nil;
}
-(void)loadMultipleAnnotations
{
NSMutableArray *arrLatti = [NSMutableArray arrayWithObjects:#"27.175015",#"28.171391",#"29.169005",#"22.105999",#"17.811456",#"21.453069",#"22.593726",#"38.444985",#"35.603719",#"35.603719",#"36.844461",#"35.889050",#"33.651208",#"38.238180",#"36.862043",#"36.949892",#"37.142803",#"37.71859", nil];
NSMutableArray *arrLong = [NSMutableArray arrayWithObjects:#"78.042155",#"79.037090",#"80.043206",#"75.761719",#"79.804688",#"81.562500",#"79.277344",#"-121.871338",#"-118.641357",#"-120.536499",#"-120.591431",#"-116.334229",#"-116.971436",#"-121.827393",#"-117.784424",#"-119.564209",#"-118.289795",#"-122.299805", nil];
NSMutableArray *arrTitle = [NSMutableArray arrayWithObjects:#"relative1",#"criminal1",#"criminal2",#"criminal3",#"criminal4",#"criminal5",#"criminal6",#"criminal7",#"criminal8",#"criminal9",#"relative2",#"client",#"criminal10",#"relative3",#"criminal11",#"relative4",#"criminal13",#"Offender", nil];
NSMutableArray *arrList = [[NSMutableArray alloc]init];
for (int i= 0; i< [arrLatti count]; i++)
{
List *list = [[List alloc]init];
list.latitt = [arrLatti objectAtIndex:i];
list.log = [arrLong objectAtIndex:i];
list.title = [arrTitle objectAtIndex:i];
[arrList addObject:list];
List *obj = [arrList objectAtIndex:i];
CLLocationCoordinate2D locationCoordinate;
locationCoordinate.latitude = [obj.latitt floatValue];
locationCoordinate.longitude = [obj.log floatValue];
MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];
pointAnnotation.coordinate =locationCoordinate;
pointAnnotation.title = obj.title;
[self.map_view addAnnotation:pointAnnotation];
MKCoordinateSpan span;
span.latitudeDelta = 10;
span.longitudeDelta = 10;
MKCoordinateRegion region;
region.center = locationCoordinate;
region.span = span;
[self.map_view selectAnnotation:pointAnnotation animated:YES];
[self.map_view setRegion:region animated:YES];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *reuseId = #"reuseid";
MKAnnotationView *av = [_map_view dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (av == nil)
{
av = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] ;
}
else
{
av.annotation = annotation;
}
if ([annotation.title isEqualToString:#"criminal1"] || [annotation.title isEqualToString:#"Offender"] || [annotation.title isEqualToString:#"criminal2"]|| [annotation.title isEqualToString:#"criminal3"]|| [annotation.title isEqualToString:#"criminal4"]|| [annotation.title isEqualToString:#"criminal5"]|| [annotation.title isEqualToString:#"criminal6"]|| [annotation.title isEqualToString:#"criminal7"]|| [annotation.title isEqualToString:#"criminal8"]|| [annotation.title isEqualToString:#"criminal9"]|| [annotation.title isEqualToString:#"criminal10"]|| [annotation.title isEqualToString:#"criminal11"]|| [annotation.title isEqualToString:#"criminal12"]|| [annotation.title isEqualToString:#"criminal13"]|| [annotation.title isEqualToString:#"criminal14"]|| [annotation.title isEqualToString:#"criminal15"])
{
av.image = [UIImage imageNamed:#"marker.png"];
av.centerOffset = CGPointMake(0.0f, -16.5f);
}
else
{
av.image = [UIImage imageNamed:#"markerBlue.png"];
av.centerOffset = CGPointMake(0.0f, -24.5f);
}
av.canShowCallout = YES;
return av;
}
The trouble is this line in your -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation function. Every time iOS gets a new location it will set the map view to look at it...
[self.map_view setRegion:region animated:YES];
…and add another circle without removing the previous one. Why don't you just set self.map_view.showsuserLocation = true? It won't move the map view, but it will draw a nice blue dot and a circle where ever the device is on the map.
through below code i over come my problem .. now its looks me i have solved my issue
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if ( !location )
{
CLLocationCoordinate2D locationCoordinate;
locationCoordinate.latitude = userLocation.coordinate.latitude;
locationCoordinate.longitude = userLocation.coordinate.longitude;
location = userLocation.location;
MKCoordinateRegion region;
region.center = mapView.userLocation.coordinate;
region.span = MKCoordinateSpanMake(0.1, 0.1);
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];
pointAnnotation.coordinate =locationCoordinate;
[self.map_View setCenterCoordinate:locationCoordinate animated:YES];
}
}

map kit not working

i am working on a map kit that shows all the data extracted from an xml web service .this web service contains 3 variables longitude (lon),latitude(lat), the name of the atm (atmName).the for loop is extracting the data from the Aatm array but the pins are not showing on the map .also my pins are all from the same region thats why i used a default center from the Aatm array with a default value. here is my code
thank you for helping me in advance :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Aatm = [[NSMutableArray alloc] init];
pinArray = [[NSMutableArray alloc] init];
NSString *url=#"http://192.168.83.1:8080/jeeRestApp-1.0/rest/Atm";
NSXMLParser *parser;
parser=[[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
parser.delegate=self;
if ([parser parse]==FALSE){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Erreur" message:#"erreur de connection!" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:nil, nil];
[alert show];
}
//Setting the approximate region
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.mapview setShowsUserLocation:YES];
MKCoordinateRegion Myregion;
CLLocationCoordinate2D center1;
//replace by variable -----------------------------------
atm=[Aatm objectAtIndex:1];
center1.latitude=[atm.lat doubleValue];
center1.longitude=[atm.lon doubleValue];
NSLog(#"la latitude (defaut) est: %#",atm.lat);
MKCoordinateSpan span1;
span1.longitudeDelta = 0.01f;
span1.latitudeDelta = 0.01f;
Myregion.center=center1;
Myregion.span=span1;
[mapview setRegion:Myregion animated:YES];
for(int i = 0; i<=[Aatm count] - 1;i++)
{
//CLLocationCoordinate2D newCoord = { atm.lat, atm.lon};
atm=[Aatm objectAtIndex:i];
CLLocationCoordinate2D pinLocaton;
pinLocaton.latitude=[atm.lat doubleValue];
NSLog(#"latitude est de :%#",atm.lat);
pinLocaton.longitude=[atm.lon doubleValue];
NSLog(#"longitude est de :%#",atm.lon);
MKPointAnnotation *annotation=[MKPointAnnotation alloc];
annotation.title=atm.atmName;
NSLog(#"latitude est de :%#",atm.atmName);
annotation.coordinate=pinLocaton;
MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"annotation1"];
newAnnotation.pinColor = MKPinAnnotationColorGreen;
newAnnotation.animatesDrop = YES;
newAnnotation.canShowCallout = NO;
[newAnnotation setSelected:YES animated:YES];
[mapview addAnnotation:annotation];
NSLog(#"annotation added");
}
You need to implement mapView:viewForAnnotation: something like:
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKPointAnnotation class]])
{
MKPinAnnotationView* view = (MKPinAnnotationView*) [mapView dequeueReusableAnnotationViewWithIdentifier:#"pin"];
if(!view)
{
view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pin"];
}
else
{
view.annotation = annotation;
}
view.pinColor = MKPinAnnotationColorGreen;
view.animatesDrop = YES;
view.canShowCallout = NO;
return view;
}
else
{
return nil;
}
}

Annotated pins are not getting loaded during reloading of map

I have used apple maps in my application for implementing maps.I used the following code for that.
My problem is that,It is showing the pins only when it is loading for the first time.
I have written the code like this
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Locate Engineer";
[worldView setMapType:MKMapTypeStandard];
[worldView setShowsUserLocation:YES];
worldView.delegate = self;
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self)
{
i=0;
locationManager = [[CLLocationManager alloc]init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
}
return self;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"%#",newLocation);
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
pinView = nil;
if(annotation != mapView.userLocation)
{
NSString * defaultPinID = #"aa";
//removed autorelease from the code below by coder
pinView = (MKPinAnnotationView *) [worldView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if(pinView == nil)
{
pinView .tag =i;
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] ;
//pinView.image = [UIImage imageNamed:#"serviceengineer.png"];
NSLog(#"pnID is %d",i);
NSLog(#"value of i is %d",i);
if ([[pinView.annotation title] isEqualToString:#"Levitton"]) {
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.draggable = NO;
} else {
pinView.pinColor = MKPinAnnotationColorGreen;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(mapAction:) forControlEvents:UIControlEventTouchUpInside];
rightButton.tag =i;
pinView.rightCalloutAccessoryView = rightButton;
// pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
pinView.draggable = NO;
}
}
else {
[worldView.userLocation setTitle:#"Title here"]; }
i++;
}
return pinView;
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
ServiceEngineersList *engList = [ServiceEngineers findAll];
MKCoordinateSpan spans;
spans.latitudeDelta = 0.5;
spans.longitudeDelta = 0.5;
ServiceEngineers *obj1= [engList objectAtIndex:0];
// location1.latitude = 40.728224000000;
//location1.longitude =-73.794852000000;
location1.latitude = [obj1.LATITUDE doubleValue];
location1.longitude =[obj1.LONGITUDE doubleValue];
ad1 = [[annotationTest alloc] init];
ad1.pinColor = MKPinAnnotationColorGreen;
MKCoordinateRegion regions =
MKCoordinateRegionMakeWithDistance(location1, 250,250);
regions.span = spans;
[worldView setRegion:regions animated:YES];
[worldView setZoomEnabled:YES];
[worldView addAnnotation:ad1];
ServiceEngineers *obj2= [engList objectAtIndex:1];
ad2 = [[annotationTest2 alloc] init];
ad2.pinColor = MKPinAnnotationColorGreen;
//location2.latitude = 40.710841000000;
//location2.longitude=-73.897769000000;
location2.latitude = [obj2.LATITUDE doubleValue];
location2.longitude =[obj2.LONGITUDE doubleValue];
MKCoordinateRegion region1 =
MKCoordinateRegionMakeWithDistance(location2, 250, 250);
region1.span = spans;
[worldView setRegion:region1 animated:YES];
[worldView setZoomEnabled:YES];
[worldView addAnnotation:ad2];
ServiceEngineers *obj3= [engList objectAtIndex:2];
ad3 = [[annotationTest3 alloc] init];
ad3.pinColor = MKPinAnnotationColorGreen;
//location3.latitude = 40.726768000000;
//location3.longitude=-73.634295000000;
location3.latitude = [obj3.LATITUDE doubleValue];
location3.longitude =[obj3.LONGITUDE doubleValue];
MKCoordinateRegion region2 =
MKCoordinateRegionMakeWithDistance(location3, 250, 250);
region2.span = spans;
[worldView setRegion:region2 animated:YES];
[worldView setZoomEnabled:YES];
[worldView addAnnotation:ad3];
ServiceEngineers *obj4= [engList objectAtIndex:3];
ad4 = [[annotationTest4 alloc] init];
ad4.pinColor = MKPinAnnotationColorGreen;
//location4.latitude = 40.702677000000;
//location4.longitude=-73.788969000000;
location4.latitude = [obj4.LATITUDE doubleValue];
location4.longitude =[obj4.LONGITUDE doubleValue];
MKCoordinateRegion region3 =
MKCoordinateRegionMakeWithDistance(location4, 250, 250);
region3.span = spans;
[worldView setRegion:region3 animated:YES];
[worldView setZoomEnabled:YES];
[worldView addAnnotation:ad4];
ad5 = [[annotationTest5 alloc] init];
ad5.pinColor = MKPinAnnotationColorRed;
location5.latitude = 40.7258;
location5.longitude= -73.5147;
MKCoordinateRegion region4 =
MKCoordinateRegionMakeWithDistance(location5, 250, 250);
region4.span = spans;
[worldView setRegion:region4 animated:YES];
[worldView setZoomEnabled:YES];
[worldView addAnnotation:ad5];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"Couldnot find location: %#",error);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
return YES;
return NO;
}
Please help me to find out the issue.I put breakpoints.Second time it is coming only upto
worldView.delegate = self;
Just place in the viewWillAppear and remove in the viewDidLoad
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
locationManager = [[CLLocationManager alloc]init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
}

Read current location name using Map View in iPhone

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/

Resources