I have a table view that load the data from an xml in a array and show it, every cell point to the same detail view repopulated.
In the same view i have a hidden map view, the map view show the same array of the table in the map, every annotation have a disclosure button that point to the same detail view of the table, but i don't understand how i can pass the data from the map view to the detail view...
for the disclosure button i use this code
-(IBAction)showDetails:(id)sender{
DettMercatiViewController *dettMercatiViewController = [[DettMercatiViewController alloc] initWithNibName:#"DettMercatiViewController" bundle:nil];
dettMercatiViewController.mieiMercati = [table objectAtIndex:????];
[self.navigationController pushViewController:dettMercatiViewController animated:YES];
[dettMercatiViewController release];
}
table is the name of the nsmutablearray created from the xml but i don't know what objectIndex use... any idea?
ok, here the viewdidload and the creation of pin, please help me and explain because i am desolate:
- (void)viewDidLoad
{
pp =0;
//Colore NavigationBar
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:33.0f/255.0f green:89.0f/255.0f blue:50.0f/255.0f alpha:1.0f];
//Font Navigation Bar
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 45)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:#"Museo700-Regular" size:20];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=[self.title uppercaseString];
self.navigationItem.titleView = label;
[label release];
//creazione nome XML
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *nomeXML = [NSString stringWithFormat: #"%#_%#_%#",[defaults stringForKey:#"interesse"],[defaults stringForKey:#"provincia"],[defaults stringForKey:#"giorno"]];
NSLog(#"%#", nomeXML);
//Caricamento XML
table = [[NSMutableArray alloc] init];
NSString *url = [[NSBundle mainBundle] pathForResource:nomeXML ofType:#"xml"];
XMLParser *myParser = [[[XMLParser alloc]autorelease] parseXMLAtURL:url toObject:#"Mercato" parseError:nil];
for(int i = 0; i < [[myParser items] count]; i++) {
Mercato *new = [[myParser items] objectAtIndex:i];
[table addObject:new];
///////////////////////////////////////////
//definizione posizione utente
AppDelegate *appDelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
CLLocation *userLoc = appDelegate.locationManager.location;
CLLocationCoordinate2D userCoordinate = userLoc.coordinate;
NSLog(#"user latitude = %f",userCoordinate.latitude);
NSLog(#"user longitude = %f",userCoordinate.longitude);
mapView.delegate=self;
//Pin Mappa
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = [new.latitudine doubleValue];
theCoordinate.longitude = [new.longitudine doubleValue];
myAnnotation=[[MyAnnotation alloc] init];
myAnnotation.coordinate=theCoordinate;
myAnnotation.title=[NSString stringWithFormat:#"%#", new.ubicazione];
myAnnotation.subtitle=[NSString stringWithFormat:#"%#, %#, %#",new.comune, new.giorno, new. orario];
[mapView addAnnotation:myAnnotation];
[annotations addObject:myAnnotation];
if([new.latitudine doubleValue] == 0) {
[mapView removeAnnotation:myAnnotation];
myAnnotation = nil;
}
// Inizializza mappa al Centro della Provincia
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"bergamo"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.6982642;
newRegion.center.longitude = 9.6772698;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"brescia"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.5411875;
newRegion.center.longitude = 10.2194437;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"como"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.8080597;
newRegion.center.longitude = 9.0851765;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"cremona"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.133249;
newRegion.center.longitude = 10.0226511;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"lecco"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.8565698;
newRegion.center.longitude = 9.3976704;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"lodi"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.3138041;
newRegion.center.longitude = 9.5018274;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"mantova"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.1564168;
newRegion.center.longitude = 10.7913751;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"milano"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.4654542;
newRegion.center.longitude = 9.186516;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.512872;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"monza e brianza"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.623599;
newRegion.center.longitude = 9.2588015;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"pavia"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.1847248;
newRegion.center.longitude = 9.1582069;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"sondrio"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 46.1698583;
newRegion.center.longitude = 9.8787674;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"varese"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.8205989;
newRegion.center.longitude = 8.8250576;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
}
////////////////////////////
[super viewDidLoad];
}
#pragma mark MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
NSLog(#"welcome into the map view annotation");
pp++;
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]]){
return nil;
}
// try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:AnnotationIdentifier]autorelease];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
pinView.pinColor=MKPinAnnotationColorGreen;
pinView.tag = pp;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:myAnnotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
return pinView;
}
////////////////
-(IBAction)showDetails:(id)sender{
NSLog(#"%i", pp);
DettMercatiViewController *dettMercatiViewController = [[DettMercatiViewController alloc] initWithNibName:#"DettMercatiViewController" bundle:nil];
dettMercatiViewController.mieiMercati = [table objectAtIndex:[sender tag]];
[self.navigationController pushViewController:dettMercatiViewController animated:YES];
[dettMercatiViewController release];
}
I suggest to give every disclosure button in your map view a tag and you can use this tag than
so
[table objectAtIndex:[sender tag]];
Related
In my browse view controller, the Map Kit annotations show up.
_mapVC = [[CollectionMapViewController alloc] init];
mapVC.collectedLeafArray = [collectionFetchedResultsController fetchedObjects];
mapVC.canShowCallout = YES;
mapVC.delegate = self;
[mapVC layoutMapView];
[self addChildViewController:mapVC];
[self.view insertSubview:mapVC.view belowSubview:_userCollectionSortMenu.view];
[mapVC didMoveToParentViewController:self];
I set up my child view controller the exact same way in my note taking view controller. However, they annotation pins don't show up.
_mapVC = [[CollectionMapViewController alloc] init];
_mapVC.collectedLeafArray = [NSArray arrayWithObject:_collectedLeaf];
_mapVC.delegate = self;
_mapVC.canShowCallout = NO;
[_mapVC layoutMapView];
[self addChildViewController:_mapVC];
[_scrollView addSubview:_mapVC.view];
[_mapVC didMoveToParentViewController:self];
I know the collected leaf is not nil, because I check the latitude in the CollectionMapViewController method that sets the annotations:
- (void)layoutMapView
{
NSMutableArray* leavesWithValidGeotag = [[NSMutableArray alloc] initWithCapacity:[collectedLeafArray count]];
for (CollectedLeaf* collectedLeaf in collectedLeafArray)
{
NSLog(#"latitude: %#", collectedLeaf.latitude);
if ( ![collectedLeaf.latitude isEqualToString:kGeoLocationNotAvailable] )
{
[leavesWithValidGeotag addObject:collectedLeaf];
LeafAnnotation* annotation = [[LeafAnnotation alloc] initWithLeaf:collectedLeaf];
[mapView addAnnotation:annotation];
[annotation release];
}
}
//// Adjust the region
if([leavesWithValidGeotag count] > 1)
{
NSArray* westEastSort = [leavesWithValidGeotag sortedArrayUsingSelector:#selector(longitudeCompare:)];
CollectedLeaf* eastMostLeaf = [westEastSort objectAtIndex:0];
CollectedLeaf* westMostLeaf = [westEastSort lastObject];
NSArray* southNorthSort = [leavesWithValidGeotag sortedArrayUsingSelector:#selector(latitudeCompare:)];
CollectedLeaf* southMostLeaf = [southNorthSort objectAtIndex:0];
CollectedLeaf* northMostLeaf = [southNorthSort lastObject];
CLLocationCoordinate2D center;
center.longitude = ([westMostLeaf.longitude doubleValue] + [eastMostLeaf.longitude doubleValue]) / 2.0f;
center.latitude = ([southMostLeaf.latitude doubleValue] + [northMostLeaf.latitude doubleValue]) / 2.0f;
MKCoordinateSpan span = MKCoordinateSpanMake(1.5 * fabs([northMostLeaf.latitude doubleValue] - [southMostLeaf.latitude doubleValue]),
1.5 * fabs([westMostLeaf.longitude doubleValue] - [eastMostLeaf.longitude doubleValue]));
if ( span.latitudeDelta < kMinimumSpan )
{
span.latitudeDelta = kMinimumSpan;
}
if ( span.longitudeDelta < kMinimumSpan )
{
span.longitudeDelta = kMinimumSpan;
}
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[mapView setRegion:region];
}
else if([leavesWithValidGeotag count] == 1)
{
CollectedLeaf* theLeaf = [leavesWithValidGeotag objectAtIndex:0];
CLLocationCoordinate2D center;
center.longitude = [theLeaf.longitude doubleValue];
center.latitude = [theLeaf.latitude doubleValue];
MKCoordinateSpan span = MKCoordinateSpanMake(kMinimumSpan, kMinimumSpan);
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[mapView setRegion:region];
}
[leavesWithValidGeotag release];
}
Maybe, you need to copy NSArray object.
NSArray *test1 = [NSArray arrayWithObject:_collectedLeaf];
_mapVC.collectedLeafArray = [test1 copy];
I have multiple annotation pin in map(place1,place2)
I want user touch pin to go to new Viewcontroller to get description of place
it have function to detect in pin to touch it to go to descriptionViewController?
This is my code
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MKCoordinateRegion place1 = { {0.0, 0.0} , {0.0, 0.0} };
place1.center.latitude = 34.7037755;
place1.center.longitude = 137.7345882;
place1.span.longitudeDelta = 0.02f;
place1.span.latitudeDelta = 0.02f;
[mapView setRegion:place1 animated:YES];
Newclass *ann1 = [[Newclass alloc] init];
ann1.title = #"Place1";
ann1.subtitle = #"subtitle";
ann1.coordinate = place1.center;
[mapView addAnnotation: ann1];
MKCoordinateRegion place2 = { {0.0, 0.0} , {0.0, 0.0} };
place2.center.latitude = 34.7024461;
place2.center.longitude = 137.7297572;
place2.span.longitudeDelta = 0.02f;
place2.span.latitudeDelta = 0.02f;
[mapView setRegion:place2 animated:YES];
Newclass *ann2 = [[Newclass alloc] init];
ann2.title = #"place2";
ann2.subtitle = #"subtitle";
ann2.coordinate = place2.center;
[mapView addAnnotation:ann2];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKPinAnnotationView *myPin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:#"current"];
myPin.pinColor = MKPinAnnotationColorGreen;
UIButton *advertButtom = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButtom addTarget:self action:#selector(button:) forControlEvents:UIControlEventTouchUpInside];
myPin.rightCalloutAccessoryView = advertButtom;
myPin.draggable =NO;
myPin.animatesDrop =true;
myPin.canShowCallout=YES;
return myPin;
}
-(void)button:(id)sender{
NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
UIStoryboard *storyboard = [self storyboard];
descriptionViewController *description = [storyboard instantiateViewControllerWithIdentifier:#"des"];
[self presentModalViewController:description animated:YES];
NSLog(#"touched");
NSLog(#"data %#");
}
You need to use mapView method
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
UIStoryboard *storyboard = [self storyboard];
descriptionViewController *description = [storyboard instantiateViewControllerWithIdentifier:#"des"];
[self presentModalViewController:description animated:YES];
NSLog(#"touched");
NSLog(#"data %#");
}
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 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];
}
Take a look at this code and see if you can help me fixing it:
- (void)viewDidLoad
{
//Initialize the array and add annotations for the location on the map
annotations = [[NSMutableArray alloc] init];
CustomAnnotation *annotation;
CLLocationCoordinate2D coordinate;
//1
coordinate.latitude = 25.220390105797264;
coordinate.longitude = 55.28095199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"123";
[annotations addObject:annotation];
//2
coordinate.latitude = 25.218880105797264;
coordinate.longitude = 55.27992199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"117";
[annotations addObject:annotation];
//3
coordinate.latitude = 25.219280105797264;
coordinate.longitude = 55.28052199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"118";
[annotations addObject:annotation];
//4
coordinate.latitude = 25.219680105797264;
coordinate.longitude = 55.28052199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"119";
[annotations addObject:annotation];
//5
coordinate.latitude = 25.219980105797264;
coordinate.longitude = 55.28095199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"120";
[annotations addObject:annotation];
//6
coordinate.latitude = 25.220380105797264;
coordinate.longitude = 55.28115199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"121";
[annotations addObject:annotation];
//7
coordinate.latitude = 25.220350105797264;
coordinate.longitude = 55.28105199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"122";
[annotations addObject:annotation];
//8
coordinate.latitude = 25.215050105797264;
coordinate.longitude = 55.28105199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"124";
[annotations addObject:annotation];
//9
coordinate.latitude = -35.44138;
coordinate.longitude = -71.66208;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"125";
[annotations addObject:annotation];
//10
coordinate.latitude = -35.44138;
coordinate.longitude = -71.66208;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"126";
[annotations addObject:annotation];
//Set the region and zoom factor of the map view
MKCoordinateRegion region;
region.span.latitudeDelta = 0.01f;
region.span.longitudeDelta = 0.01f;
region.center.latitude = 25.216050105797264;
region.center.longitude = 55.27905199798584;
[sheikhZayedRoadMapView addAnnotations:annotations];
[sheikhZayedRoadMapView setRegion:region];
//[siteMapView.layer setCornerRadius:5.0];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:
(id <MKAnnotation>)annotation
{
NSLog(#"Annotation Class: %#", [annotation class]);
static NSString * const kPinAnnotationIdentifier = #"PinIdentifier";
MKAnnotationView *annotationView;
Myriadproregular *annotationIndexLabel;
//CustomAnnotation *customAnnotation =nil
annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:kPinAnnotationIdentifier];
if (!annotationView) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:kPinAnnotationIdentifier];
annotationIndexLabel = [[Myriadproregular alloc] initWithFrame:CGRectMake(0, 3, 25 , 20)];
annotationIndexLabel.font = [UIFont boldSystemFontOfSize:12];
[annotationIndexLabel setBackgroundColor:[UIColor clearColor]];
[annotationIndexLabel setTextColor:[UIColor whiteColor]];
[annotationIndexLabel setTextAlignment:UITextAlignmentCenter];
[annotationView addSubview:annotationIndexLabel];
annotationView.canShowCallout = YES;
annotationView.calloutOffset = CGPointMake(-5, 5);
}else {
//Get the label if exists
for (UIView *labelView in annotationView.subviews) {
if ([labelView isKindOfClass:[UILabel class]]) {
annotationIndexLabel = (Myriadproregular *) labelView;
break;
}
}
}
if ([annotation isKindOfClass:[MKUserLocation class]]) {
[annotationView setImage:[UIImage imageNamed:#"map_notify_bg.png"]];
}else {
CustomAnnotation *customAnnotation = (CustomAnnotation *) annotation;
[annotationIndexLabel setText:customAnnotation.annotationIndex];
[annotationView setImage:[UIImage imageNamed:#"ico_inspector.png"]];
}
return annotationView;
}
Problem is:
I am not getting didSelectAnnotationView delegate method called, when I tap on the custom image provided to MKAnnotationView.
Any kind of help will be appreciated.
Thanks
[annotationView setCanShowCallout:NO]
This solved my problem, because I didn't want a callout on click of a pin, but I wanted to call the didSelectAnnotationView delegate to be called.
Thanks
Are you giving your annotations a title property? Are you sure that [annotationView setCanShowCallout:YES] is set properly?