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];
}
Related
I am using mapview and want to add custom image to show the location in map view , how to add image i am not able to add. this code i have used.
-(void)addAllPins
{
self.mapView.delegate=self;
for(int i = 0; i < name1.count; i++)
{
[self addPinWithTitle:name1[i] AndCoordinate:arrCoordinateStr[i]];
}
}
-(void)addPinWithTitle:(NSString *)title AndCoordinate:(NSString *)strCoordinate
{
MKPointAnnotation *mapPin = [[MKPointAnnotation alloc] init];
// clear out any white space
strCoordinate = [strCoordinate stringByReplacingOccurrencesOfString:#" " withString:#""];
// convert string into actual latitude and longitude values
NSArray *components = [strCoordinate componentsSeparatedByString:#","];
double latitude = [components[0] doubleValue];
double longitude = [components[1] doubleValue];
// setup the map pin with all data and add to map view
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
mapPin.title = title;
mapPin.coordinate = coordinate;
// UIImage *image = [UIImage imageNamed:#"hover.9.png"];
// [[self.mapView viewForAnnotation:mapPin] setImage:image];
[self.mapView addAnnotation:mapPin];
}
i want to set zoom in scale also . can some help me to solve this.
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
// NSLog(#"%#", [self deviceLocation]);
//View Area
MKCoordinateRegion region = self.mapView.region;
region.center.latitude = self.locationManager.location.coordinate.latitude;
region.center.longitude = self.locationManager.location.coordinate.longitude;
region.span.longitudeDelta = 0.015;
region.span.longitudeDelta = 0.015;
[mapView setRegion:region animated:YES];
[self addAllPins];
}
My code is help you.You can put custom pin annotation
- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id < MKAnnotation >)annotation
{
static NSString *reuseId = #"StandardPin";
MKAnnotationView *aView = (MKAnnotationView *)[sender
dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (aView == nil)
{
aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
aView.canShowCallout = YES;
}
aView.image = [UIImage imageNamed : #"Location_OnMap"];
aView.annotation = annotation;
aView.calloutOffset = CGPointMake(0, -5);
aView.draggable = YES;
aView.enabled = YES;
return aView;
}
You need to use mapView Delegate Method
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if (annotation == mapView.userLocation)
{
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:#"MKPinAnnotationView"];
if (pinView ==nil) {
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:#"MKPinAnnotationView"];
pinView.animatesDrop = YES;
}
pinView.canShowCallout = YES;
pinView.pinColor = MKPinAnnotationColorGreen;//if you want
return pinView;
}
else
{
static NSString *viewId = #"MKAnnotationView";
MKAnnotationView *annotationView = (MKAnnotationView*)
[mapView dequeueReusableAnnotationViewWithIdentifier:viewId];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:viewId];
}
annotationView.canShowCallout=YES;
annotationView.image = [UIImage imageNamed:#"yourImage"];//set your image here
return annotationView;
}
}
2.) to zoom
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(yourLocation.coordinate, 100, 100);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
[self.mapView setRegion:adjustedRegion animated:YES];
It will help.Thank you.
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];
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];
}
}
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;
}
}
I am trying to display multiple annotations on my mapView from an array. The annotation coordinates are parsed from an XML file and stored in the array as [currentCall longitude] and [currentCall latitude]. My question is, what is the syntax for "calling" the array? (I don't know if that's how you say it). In another part of my application, I display the parsed XML results in a table and use "JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];" to "call" the array. How do I do it for displaying my annotations? Everything else works fine except that little part.
Here is the implementation file:
#implementation SecondViewController
#synthesize mapView;
XMLParser *xmlParser;
-(IBAction)getlocation {
mapView.showsUserLocation = YES;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView commitAnimations];
}
-(IBAction)changeSeg:(id)sender {
if (segment.selectedSegmentIndex == 0) {
mapView.mapType = MKMapTypeStandard;
}
if (segment.selectedSegmentIndex == 1) {
mapView.mapType = MKMapTypeSatellite;
}
if (segment.selectedSegmentIndex == 2) {
mapView.mapType = MKMapTypeHybrid;
}
}
-(void)viewDidLoad {
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
[self.view insertSubview:mapView atIndex:0];
[super viewDidLoad];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[mapView setDelegate:self];
MKCoordinateRegion WCCCA = { {0.0, 0.0} , {0.0, 0.0} };
WCCCA.center.latitude = 45.53540820864449;
WCCCA.center.longitude = -122.86178648471832;
WCCCA.span.longitudeDelta = 0.02f;
WCCCA.span.latitudeDelta = 0.02f;
[mapView setRegion:WCCCA animated:YES];
Annotation *ann1 = [[Annotation alloc] init];
ann1.title = #"WCCCA";
ann1.subtitle = #"Washington County Consolidated Communications Agency";
ann1.coordinate = WCCCA.center;
[mapView addAnnotation: ann1];
MKCoordinateRegion CALL = { {0.0, 0.0} , {0.0, 0.0} };
CALL.center.latitude = [currentCall.latitude doubleValue];
CALL.center.longitude = [currentCall.longitude doubleValue];
CALL.span.longitudeDelta = 0.02f;
CALL.span.latitudeDelta = 0.02f;
[mapView setRegion:WCCCA animated:YES];
Annotation *ann2 = [[Annotation alloc] init];
ann2.title = [currentCall currentCallType];
ann2.subtitle = [currentCall location];
ann2.coordinate = CALL.center;
[mapView addAnnotation: ann1];
}
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"current"];
MyPin.pinColor = MKPinAnnotationColorRed;
UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButton addTarget:self action:#selector(button:) forControlEvents:UIControlEventTouchUpInside];
MyPin.rightCalloutAccessoryView = advertButton;
MyPin.draggable = NO;
MyPin.highlighted = YES;
MyPin.animatesDrop=TRUE;
MyPin.canShowCallout = YES;
return MyPin;
}
#end
If I am understanding your code properly, you should be able to just iterate through your parsed XML (it appears that the output the from your xmlParser object is an NSArray) and add the annotations to the mapView within the loop.
You can use the C function CLLocationCoordinate2DMake() to create the coordinate data structure for your annotation.
NSArray *callsArray = [xmlParser calls];
for (JointCAD *call in callsArray) {
Annotation *ann = [[Annotation alloc] init];
ann.title = [call currentCallType];
ann.subtitle = [call location];
ann.coordinate = CLLocationCoordinate2DMake([call latitude], [call longitude]);
[mapView addAnnotation:ann];
}