Multiple map views in same app - iphone

I wrote this code to add a map view in a single page for my app.
I want to add one more page with a different map view.
How can I do this?
controller.h
# import < UIKit/UIKit.h >
# import < MapKit/MapKit.h >
#interface __3_ProductionsViewController : UIViewController
{
MKMapView *mapview;
}
#property (nonatomic, retain) IBOutlet MKMapView *mapview;
-(IBAction)setMap:(id)sender;
-(IBAction)getlocation;
#end
controller.m
# import "__3_ProductionsViewController.h"
# import " NewClass.h "
# import " Class2.h "
#implementation __3_ProductionsViewController
#synthesize mapview;
-(IBAction)getlocation
{
mapview.showsUserLocation = YES;
}
-(IBAction)setMap:(id)sender
{
switch (((UISegmentedControl *) sender).selectedSegmentIndex)
{
case 0:
mapview.mapType = MKMapTypeSatellite;
break;
case 1:
mapview.mapType = MKMapTypeStandard;
break;
case 2:
mapview.mapType = MKMapTypeHybrid;
break;
default:
break;
}
}
-(void)viewDidLoad
{
[super viewDidLoad];
[mapview setMapType:MKMapTypeSatellite];
[mapview setZoomEnabled:YES];
[mapview setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } };
region.center.latitude = 45.043615;
region.center.longitude = 12.149377;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
NewClass *ann = [[NewClass alloc] init];
ann.title = #"ADRIA";
ann.subtitle = #"Adria International Raceway";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
}
newclass.h
# import < Foundation/Foundation.h >
# import < MapKit/MKAnnotation.h >
#interface NewClass : NSObject
{
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#end
newclass.m
# import "NewClass.h"
#implementation NewClass
#synthesize coordinate,title,subtitle;
#end

Related

Add image to the left of my annotations

I have created annotations that show up perfectly on my map but I am not sure how to implement the leftCalloutAccessoryView to show images.
MapViewAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MapViewAnnotation : NSObject <MKAnnotation>
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#end
MapViewAnnotation.m
#import "MapViewAnnotation.h"
#implementation MapViewAnnotation
#synthesize coordinate, title, subtitle;
#end
GroundsViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface GroundsViewController : UIViewController <MKMapViewDelegate>
{
MKMapView *mapView;
}
#property (strong, nonatomic) IBOutlet UISegmentedControl *segment;
- (IBAction)changeSeg:(id)sender;
#end
GroundsViewController.m
#import "GroundsViewController.h"
#import "MapViewAnnotation.h"
#interface GroundsViewController ()
#end
// Centre in on Northern Ireland
#define Northern_Ireland_Latitude 54.629338;
#define Northern_Ireland_Longitude -6.668701;
//Span
#define The_Span 2.00f;
// Premiership
#define Ballymena_Latitude 54.870105;
#define Ballymena_Longitude -6.265076;
// Championship 1
#define Ards_Latitude 54.651629;
#define Ards_Longitude -5.684478;
// Championship 2
#define Annagh_Latitude 54.411372;
#define Annagh_Longitude -6.440355;
#implementation GroundsViewController
#synthesize segment;
- (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;
}
}
// When the view loads
- (void)viewDidLoad
{
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
[self.view insertSubview:mapView atIndex:0];
// Create the region
MKCoordinateRegion myRegion;
// Center
CLLocationCoordinate2D center;
center.latitude = Northern_Ireland_Latitude;
center.longitude = Northern_Ireland_Longitude;
// Create the Span
MKCoordinateSpan span;
span.latitudeDelta = The_Span;
span.longitudeDelta = The_Span;
myRegion.center = center;
myRegion.span = span;
// Set our map view
[mapView setRegion:myRegion animated:YES];
NSMutableArray *locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
MapViewAnnotation *myAnn;
//Premiership
// Pin to show Ballymena United F.C.
myAnn = [[MapViewAnnotation alloc] init];
location.latitude = Ballymena_Latitude;
location.longitude = Ballymena_Longitude;
myAnn.coordinate = location;
myAnn.title = #"Ballymena United F.C.";
myAnn.subtitle = #"The Showgrounds";
[locations addObject:myAnn];
// Championship 1
// Pin to show Ards F.C.
myAnn = [[MapViewAnnotation alloc] init];
location.latitude = Ards_Latitude;
location.longitude = Ards_Longitude;
myAnn.coordinate = location;
myAnn.title = #"Ards F.C.";
myAnn.subtitle = #"Clandeboye Park";
[locations addObject:myAnn];
// Championship 2
// Pin to show Annagh United F.C.
myAnn = [[MapViewAnnotation alloc] init];
location.latitude = Annagh_Latitude;
location.longitude = Annagh_Longitude;
myAnn.coordinate = location;
myAnn.title = #"Annagh United F.C.";
myAnn.subtitle = #"Tandragee Road";
[locations addObject:myAnn];
[self->mapView addAnnotations:locations];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = #"MyLocation";
if ([annotation isKindOfClass:[PlaceMark class]]) {
MKPinAnnotationView *annotationView =
(MKPinAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
UIImageView *imvLeft = [[UIImageView alloc] init];
//Set Image
[annotationView setLeftCalloutAccessoryView:imvLeft];
return annotationView;
}
return nil;
}

Importing NSMutable Array into Map Annotations

Hi all i have a mysql database that consists of 7 columns and i have a script that reads it and extracts the data as JSON which i have pulled into an NSMutableArray, I want to be able to use this array to setup annotations on my Map but i'm not sure what to do here, as you can see i have defined one annotation here which shows up no problem but i'm honestly not sure how to show the NSMutableArray items? The NSMutable array will have more information than needed for the Annotations, I only need, coordinate, title and subtitle so how can i go about doing this? Here is my code so far:
hazards.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface Hazards : NSObject <MKAnnotation>
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#property (nonatomic, strong) NSString * ID;
#property (nonatomic, strong) NSString * ROUTE;
#property (nonatomic, strong) NSString * ADDRESS;
#property (nonatomic, strong) NSString * LATITUDE;
#property (nonatomic, strong) NSString * LONGITUDE;
#property (nonatomic, strong) NSString * HAZARD;
#property (nonatomic, strong) NSString * RISK;
// Methods
- (id) initWithID: (NSString *) hazardsID andROUTE: (NSString *) hazardsROUTE andADDRESS: (NSString *) hazardsADDRESS andLATITUDE: (NSString *) hazardsLATITUDE andLONGITUDE: (NSString *) hazardsLONGITUDE andHAZARD: (NSString *) hazardsHAZARD andRISK: (NSString *) hazardsRISK;
#end
hazards.m
#import "Hazards.h"
#implementation Hazards
#synthesize coordinate, title, subtitle, ID, ROUTE, ADDRESS, LATITUDE, LONGITUDE, HAZARD, RISK;
- (id) initWithID: (NSString *) hazardsID andROUTE: (NSString *) hazardsROUTE andADDRESS: (NSString *) hazardsADDRESS andLATITUDE: (NSString *) hazardsLATITUDE andLONGITUDE: (NSString *) hazardsLONGITUDE andHAZARD: (NSString *) hazardsHAZARD andRISK: (NSString *) hazardsRISK {
self = [super init];
if (self)
{
ID = hazardsID;
ROUTE = hazardsROUTE;
ADDRESS = hazardsADDRESS;
LATITUDE = hazardsLATITUDE;
LONGITUDE = hazardsLONGITUDE;
HAZARD = hazardsHAZARD;
RISK = hazardsRISK;
}
return self;
}
#end
viewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "Hazards.h"
#interface ViewController : UIViewController
#property (nonatomic, strong) NSMutableArray *json;
#property (nonatomic, strong) NSMutableArray *hazardsArray;
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#pragma mark - Methods
-(void) retrieveData;
#end
viewController.m
#import "ViewController.h"
#import "Hazards.h"
#interface ViewController ()
#end
// Railway Street Ballymena Coordinates
#define BALLYMENA_LATITUDE 54.857719;
#define BALLYMENA_LONGITUDE -6.280654;
// Span
#define THE_SPAN 0.01f;
#define getDataURL #"localhost:8888/rmb/json.php"
#implementation ViewController
#synthesize json, hazardsArray, mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Create the region
MKCoordinateRegion myRegion;
// Center
CLLocationCoordinate2D center;
center.latitude = BALLYMENA_LATITUDE;
center.longitude = BALLYMENA_LONGITUDE;
//Span
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
myRegion.center = center;
myRegion.span = span;
// Set our mapview
[mapView setRegion:myRegion animated: YES];
// Annotation
NSMutableArray *locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
Hazards *myAnn;
// Pin to show Royal Mail Ballymena delivery office
myAnn = [[Hazards alloc] init];
location.latitude = BALLYMENA_LATITUDE;
location.longitude = BALLYMENA_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = #"Royal Mail Ballymena";
myAnn.subtitle = #"111, Railway Street";
[locations addObject:myAnn];
[self.mapView addAnnotations:locations];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Methods
-(void) retrieveData {
NSURL *url = [NSURL URLWithString:getDataURL];
NSData *data = [NSData dataWithContentsOfURL:url];
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
// Setup our hazards array
hazardsArray = [[NSMutableArray alloc] init];
for (int i =0; i < json.count; i++) {
// Create hazard object
NSString *hazardsID = [[json objectAtIndex:i] objectForKey:#"ID"];
NSString *hazardsROUTE = [[json objectAtIndex:i] objectForKey:#"ROUTE"];
NSString *hazardsADDRESS = [[json objectAtIndex:i] objectForKey:#"ADDRESS"];
NSString *hazardsLATITUDE = [[json objectAtIndex:i] objectForKey:#"LATITUDE"];
NSString *hazardsLONGITUDE = [[json objectAtIndex:i] objectForKey:#"LONGITUDE"];
NSString *hazardsHAZARD = [[json objectAtIndex:i] objectForKey:#"HAZARD"];
NSString *hazardsRISK = [[json objectAtIndex:i] objectForKey:#"RISK"];
Hazards *myHazards = [[Hazards alloc] initWithID:hazardsID andROUTE:hazardsROUTE andADDRESS:hazardsADDRESS andLATITUDE:hazardsLATITUDE andLONGITUDE:hazardsLONGITUDE andHAZARD:hazardsHAZARD andRISK:hazardsRISK];
// Add our hazards object to our hazards array
[hazardsArray addObject:myHazards];
}
// [self.mapView addAnnotation:hazardsArray];
}
#end
Many Thanks in Advance
Assuming your retrieveData correctly builds Hazard objects and is called at the right time you could either add them one at a time with this line inside the loop
[self.mapView addAnnotation:myHazards];
or all at once after the loop is finished
[self.mapView addAnnotations:hazardsArray];

Add type to annotation so I can filter Annotation array in the 3 pin colors

I'm new to xcode - I made a map and added some Annotations using a MutableArray. I would like to divide them in to 3 groups and show them on the map using the 3 different pin colors.
Hope you can help or give me an url to a guide that can help me. I tried making a 'if statement' and a 'annotationType' but could get it to work right. Hope you can help or give me a hint.
Thank you for your time.
ViewController.m
{
[super viewDidLoad];
[self.myMapView setDelegate:self];
MKCoordinateRegion myRegion;
CLLocationCoordinate2D center;
center.latitude = AMAR_LATITUDE;
center.longitude = AMAR_LONGITUDE;
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
myRegion.center = center;
myRegion.span = span;
[myMapView setRegion: myRegion animated: YES];
NSMutableArray * locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
Annotation *myAnn;
NSMutableArray * locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
Annotation *myAnn;
myAnn = [[Annotation alloc] init];
location.latitude = TATU_LATITUDE;
location.longitude = TATU_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = #"Tante Tuli";
myAnn.subtitle =#"Amagerbrogade 161";
[locations addObject:myAnn];
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKPinAnnotationView *view =[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"Pin"];
view.pinColor = MKPinAnnotationColorRed;
view.enabled = YES;
view.animatesDrop = YES;
view.canShowCallout = YES;
view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return view;
}
Annotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface Annotation : NSObject <MKAnnotation>
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
- initWithPosition:(CLLocationCoordinate2D)coords;
#end
Annotation.m
#import "Annotation.h"
#implementation Annotation;
#synthesize coordinate, title, subtitle;
- initWithPosition:(CLLocationCoordinate2D)coords; {
if (self = [super init]) {
self.coordinate = coords;
}
return self;
}
#end
I have tried to add an fourth line:
Annotation.h
#property (nonatomic, copy) NSString *colortag;
Annotation.m
#synthesize coordinate, title, subtitle, colortag;
ViewController.m
myAnn = [[Annotation alloc] init];
location.latitude = TATU.LATITUDE;
location.longitude = TATU.LONGITUDE;
myAnn.coordinate = location;
myAnn.title = #"name";
myAnn.subtitle =#"adr";
myAnn.colortag =#"purple";
[locations addObject:myAnn];
if ([[annotation colortag] isEqualToString:#"purple"])
view.pinColor = MKPinAnnotationColorPurple;
I get error: No known instance method for selector 'colertag' :S
Your Annotation class has title, subtitle and coordinate properties. Just add a fourth and set it when you're setting the others, then read it and set the colour in viewForAnnotation. You will need to cast the annotation parameter into your class (after checking it is an instance of your class and not the user's location annotation) before reading the new property.

'MyAnnotation' may not respond to 'initWithDictionary:'

I keep getting this semantic issue with this code ('MyAnnotation' may not respond to 'initWithDictionary:'), im adding annotations to a map using a plist.
Even though it displays the pin and everything i want it to, i get an semantic issue and cant seem to solve the problem
if anyone could help that would be great thanks
heres the code, the problem is in the //BrewMapViewController.m the error is on this line
MyAnnotation *annotation = [[MyAnnotation alloc] initWithDictionary:breweryDict];
/*MyAnnotation.h*/
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MyAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
NSString *test;
}
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#property (nonatomic, copy) NSString *test;
#end
/*MyAnnotation.m*/
#import "MyAnnotation.h"
#implementation MyAnnotation
#synthesize coordinate, title, subtitle, test;
- (id) initWithDictionary:(NSDictionary *) dict
{
self = [super init];
if (self != nil) {
coordinate.latitude = [[dict objectForKey:#"latitude"] doubleValue];
coordinate.longitude = [[dict objectForKey:#"longitude"] doubleValue];
self.title = [dict objectForKey:#"name"];
self.subtitle = [dict objectForKey:#"address"];
self.test = [dict objectForKey:#"test"];
}
return self;
}
#end
/*BrewMapViewController.h*/
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface BrewMapViewController : UIViewController <MKMapViewDelegate> {
IBOutlet MKMapView *map;
NSArray *breweries;
}
#end
/*BrewMapViewController.m*/
#import "BrewMapViewController.h"
#import "MyAnnotation.h"
#implementation BrewMapViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
breweries = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:#"test"
ofType:#"xml"]];
double minLat = [[breweries valueForKeyPath:#"#min.latitude"] doubleValue];
double maxLat = [[breweries valueForKeyPath:#"#max.latitude"] doubleValue];
double minLon = [[breweries valueForKeyPath:#"#min.longitude"] doubleValue];
double maxLon = [[breweries valueForKeyPath:#"#max.longitude"] doubleValue];
MKCoordinateRegion region;
region.center.latitude = (maxLat + minLat) / 2.0;
region.center.longitude = (maxLon + minLon) / 2.0;
region.span.latitudeDelta = (maxLat - minLat) * 1.05;
region.span.longitudeDelta = (maxLon - minLon) * 1.05;
map.region = region;
for (NSDictionary *breweryDict in breweries){
MyAnnotation *annotation = [[MyAnnotation alloc] initWithDictionary:breweryDict];
[map addAnnotation:annotation];
[annotation release];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
if (map.userLocation == annotation){
return nil;
}
NSString *identifier = #"MY_IDENTIFIER";
MKAnnotationView *annotationView = [map dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil){
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:identifier]
autorelease];
annotationView.image = [UIImage imageNamed:#"beer.png"];
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.leftCalloutAccessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pretzel.png"]] autorelease];
}
return annotationView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"I've been tapped");
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)dealloc {
[breweries release];
[map release];
[super dealloc];
}
#end
You have to put the method signature for - (id) initWithDictionary:(NSDictionary *) dict into your header file in order to tell BrewMapViewController that the method exists:
/*MyAnnotation.h*/
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MyAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
NSString *test;
}
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#property (nonatomic, copy) NSString *test;
- (id) initWithDictionary:(NSDictionary *) dict;
#end

Have more than one Pin Annotation? MapKit

At the moment I have 1 pin, when you tap the pin it displays information. However I want more than 1 pin at different locations. How would I do this?
This is my code.
Map.m
- (void)viewDidLoad
{
[super viewDidLoad];
[mapview setMapType:MKMapTypeStandard];
[mapview setZoomEnabled:YES];
[mapview setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } };
region.center.latitude = 52.509078;
region.center.longitude = -1.884799;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapview setRegion:region animated:YES];
NewClass *ann = [[NewClass alloc] init];
ann.title = #"AVFC";
ann.subtitle = #"Aston Villa Football Club";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
// Do any additional setup after loading the view, typically from a nib.
}
NewClass.h
#interface NewClass : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
NSString *titleOne;
NSString *subtitleTwo;
}
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#property (nonatomic, copy) NSString *titleOne;
#property (nonatomic, copy) NSString *subtitleTwo;
#end
Create your annotations (however many you want) then add them to an NSArray. You can add them to your MKMapView using [mapview addAnnotations:annotationArray];
MKMapView Ref