I'm trying to populate a detail disclosure but it won't generate. I don't have any errors or warnings when compiling and building the app except one here : "local declaration of mapView hides instance variable. I know what that means, it means that its already used elsewhere. and when i type self.webView the error goes away but the detail disclosure still doesn't populate. let me know what i'm doing wrong please:
Annotations:
MapViewAnnotion.h
#import
Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MapViewAnnotation : NSObject <MKAnnotation> {
NSString *title;
NSString *subtitle;
CLLocationCoordinate2D coordinate;
}
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d andSubtitle:(NSString *)sub;
#end
MapViewAnnotation.m
#import "MapViewAnnotation.h"
#implementation MapViewAnnotation
#synthesize title, coordinate,subtitle;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d andSubtitle:(NSString *)sub{
//[super init];
title = ttl;
coordinate = c2d;
subtitle = sub;
return self;
}
- (void)dealloc {
// [title release];
// [super dealloc];
}
#end
and then...
MapViewController.h
#import <UIKit/UIKit.h>
#import "RESideMenu.h"
#import <MapKit/MapKit.h>
#interface MapViewController : UIViewController
{
IBOutlet MKMapView *mapView;
IBOutlet UISegmentedControl *segmentedControl;
}
#property (nonatomic, retain) MKMapView *mapView;
#property (nonatomic, retain) UISegmentedControl *segmentedControl;
- (IBAction)segmentedControllChanged:(id)sender;
#end
MapViewController.m
#import "MapViewController.h"
#import "MapViewAnnotation.h"
#import "config.h"
#interface MapViewController ()
#end
#implementation MapViewController
#synthesize mapView, segmentedControl;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = [NSString stringWithFormat:#"\tCurrently In : %#",[config getSubTitle]];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(showMenu)];
[self.navigationItem.leftBarButtonItem setTintColor:[UIColor colorWithRed:(93/255.0) green:(149/255.0) blue:(197/255.0) alpha:1]];
// All map stuff
mapView.showsUserLocation = YES;
double cost2 = [config getLatitude];
double cost = [config getLongitude];
CLLocationCoordinate2D location;
location.latitude = cost2;
location.longitude = cost;
// Add the annotation to our map view
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:[config getTitle] andCoordinate:location andSubtitle:[config getSubTitle]];
[self.mapView addAnnotation:newAnnotation];
// Do any additional setup after loading the view from its nib.
MKCoordinateRegion region;
region.center.latitude = (double) cost2;
region.center.longitude = (double) cost;
region.span = MKCoordinateSpanMake(.99, .99);
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
//!!! Map Points
// UNITED STATES
// Alaska
MKPointAnnotation *anchorage = [[MKPointAnnotation alloc] init];
anchorage.coordinate = CLLocationCoordinate2DMake(61.218056, -149.900278);
anchorage.title = #"Anchorage";
anchorage.subtitle = #"Alaska";
[mapView addAnnotation:anchorage];
MKPointAnnotation *chena = [[MKPointAnnotation alloc] init];
chena.coordinate = CLLocationCoordinate2DMake(64.88349, -146.855596);
chena.title = #"Chena Hot Springs";
chena.subtitle = #"Alaska";
[mapView addAnnotation:chena];
MKPointAnnotation *healy = [[MKPointAnnotation alloc] init];
healy.coordinate = CLLocationCoordinate2DMake(63.869738, -149.021511);
healy.title = #"Healy";
healy.subtitle = #"Alaska";
[mapView addAnnotation:healy];
MKPointAnnotation *palmer = [[MKPointAnnotation alloc] init];
palmer.coordinate = CLLocationCoordinate2DMake(61.599722, -149.112778);
palmer.title = #"Palmer";
palmer.subtitle = #"Alaska";
[mapView addAnnotation:palmer];
MKPointAnnotation *seward = [[MKPointAnnotation alloc] init];
seward.coordinate = CLLocationCoordinate2DMake(60.104167, -149.442222);
seward.title = #"Seward";
seward.subtitle = #"Alaska";
[mapView addAnnotation:seward];
MKPointAnnotation *wasilla = [[MKPointAnnotation alloc] init];
wasilla.coordinate = CLLocationCoordinate2DMake(61.581389, -149.439444);
wasilla.title = #"Wasilla";
wasilla.subtitle = #"Alaska";
[mapView addAnnotation:wasilla];
// Arizona
MKPointAnnotation *tucson = [[MKPointAnnotation alloc] init];
tucson.coordinate = CLLocationCoordinate2DMake(32.221743, -110.926479);
tucson.title = #"Tuscon";
tucson.subtitle = #"Arizona";
[mapView addAnnotation:tucson];
MKPointAnnotation *phoenix = [[MKPointAnnotation alloc] init];
phoenix.coordinate = CLLocationCoordinate2DMake(33.448377, -112.074037);
phoenix.title = #"Phoenix";
phoenix.subtitle = #"Arizona";
[mapView addAnnotation:phoenix];
// California
MKPointAnnotation *anaheim = [[MKPointAnnotation alloc] init];
anaheim.coordinate = CLLocationCoordinate2DMake(33.835293, -117.914504);
anaheim.title = #"Anaheim";
anaheim.subtitle = #"California";
[mapView addAnnotation:anaheim];
// Colorado
MKPointAnnotation *denver = [[MKPointAnnotation alloc] init];
denver.coordinate = CLLocationCoordinate2DMake(39.737567, -104.984718);
denver.title = #"Denver";
denver.subtitle = #"Colorado";
[mapView addAnnotation:denver];
// Georgia
MKPointAnnotation *augusta = [[MKPointAnnotation alloc] init];
augusta.coordinate = CLLocationCoordinate2DMake(33.473498, -82.010515);
augusta.title = #"Augusta";
augusta.subtitle = #"Georgia";
[mapView addAnnotation:augusta];
// Hawaii
MKPointAnnotation *honolulu = [[MKPointAnnotation alloc] init];
honolulu.coordinate = CLLocationCoordinate2DMake(21.306944, -157.858333);
honolulu.title = #"Honolulu";
honolulu.subtitle = #"Hawaii";
[mapView addAnnotation:honolulu];
// Indiana
MKPointAnnotation *terrehaute = [[MKPointAnnotation alloc] init];
terrehaute.coordinate = CLLocationCoordinate2DMake(39.466703, -87.413909);
terrehaute.title = #"Terre Haute";
terrehaute.subtitle = #"Indiana";
[mapView addAnnotation:terrehaute];
MKPointAnnotation *indianapolis = [[MKPointAnnotation alloc] init];
indianapolis.coordinate = CLLocationCoordinate2DMake(39.768403, -86.158068);
indianapolis.title = #"Indianapolis";
indianapolis.subtitle = #"Indiana";
[mapView addAnnotation:indianapolis];
// Michigan
MKPointAnnotation *westland = [[MKPointAnnotation alloc] init];
westland.coordinate = CLLocationCoordinate2DMake(42.324204, -83.400211);
westland.title = #"Westland";
westland.subtitle = #"Michigan";
[mapView addAnnotation:westland];
MKPointAnnotation *detroit = [[MKPointAnnotation alloc] init];
detroit.coordinate = CLLocationCoordinate2DMake(42.331427, -83.045754);
detroit.title = #"Detroit";
detroit.subtitle = #"Michigan";
[mapView addAnnotation:detroit];
// Missouri
MKPointAnnotation *stlouis = [[MKPointAnnotation alloc] init];
stlouis.coordinate = CLLocationCoordinate2DMake(38.627003, -90.199404);
stlouis.title = #"St Louis";
stlouis.subtitle = #"Missouri";
[mapView addAnnotation:stlouis];
// New York
MKPointAnnotation *newyork = [[MKPointAnnotation alloc] init];
newyork.coordinate = CLLocationCoordinate2DMake(40.712784, -74.005941 );
newyork.title = #"New York";
newyork.subtitle = #"New York";
[mapView addAnnotation:newyork];
MKPointAnnotation *manhattan = [[MKPointAnnotation alloc] init];
manhattan.coordinate = CLLocationCoordinate2DMake(40.790278, -73.959722);
manhattan.title = #"Manhattan";
manhattan.subtitle = #"New York";
[mapView addAnnotation:manhattan];
MKPointAnnotation *queens = [[MKPointAnnotation alloc] init];
queens.coordinate = CLLocationCoordinate2DMake(40.75, -73.866667);
queens.title = #"Queens";
queens.subtitle = #"New York";
[mapView addAnnotation:queens];
// North Carolina
MKPointAnnotation *asheville = [[MKPointAnnotation alloc] init];
asheville.coordinate = CLLocationCoordinate2DMake(35.595058, -82.551487);
asheville.title = #"Asheville";
asheville.subtitle = #"North Carolina";
[mapView addAnnotation:asheville];
MKPointAnnotation *butner = [[MKPointAnnotation alloc] init];
butner.coordinate = CLLocationCoordinate2DMake(36.132089, -78.756671);
butner.title = #"Butner";
butner.subtitle = #"North Carolina";
[mapView addAnnotation:butner];
MKPointAnnotation *creedmoor = [[MKPointAnnotation alloc] init];
creedmoor.coordinate = CLLocationCoordinate2DMake(36.122368, -78.686115);
creedmoor.title = #"Creedmoor";
creedmoor.subtitle = #"North Carolina";
[mapView addAnnotation:creedmoor];
MKPointAnnotation *charlotte = [[MKPointAnnotation alloc] init];
charlotte.coordinate = CLLocationCoordinate2DMake(35.227087, -80.843127);
charlotte.title = #"Charolette";
charlotte.subtitle = #"North Carolina";
[mapView addAnnotation:charlotte];
MKPointAnnotation *fayetteville = [[MKPointAnnotation alloc] init];
fayetteville.coordinate = CLLocationCoordinate2DMake(35.052664, -78.878358);
fayetteville.title = #"Fayetteville";
fayetteville.subtitle = #"North Carolina";
[mapView addAnnotation:fayetteville];
MKPointAnnotation *jacksonville = [[MKPointAnnotation alloc] init];
jacksonville.coordinate = CLLocationCoordinate2DMake(34.754052, -77.430241);
jacksonville.title = #"Jacksonville";
jacksonville.subtitle = #"North Carolina";
[mapView addAnnotation:jacksonville];
MKPointAnnotation *greenville = [[MKPointAnnotation alloc] init];
greenville.coordinate = CLLocationCoordinate2DMake(35.612661, -77.366354);
greenville.title = #"Greenville";
greenville.subtitle = #"North Carolina";
[mapView addAnnotation:greenville];
MKPointAnnotation *greensboro = [[MKPointAnnotation alloc] init];
greensboro.coordinate = CLLocationCoordinate2DMake(36.072635, -79.791975);
greensboro.title = #"Greensboro";
greensboro.subtitle = #"North Carolina";
[mapView addAnnotation:greensboro];
MKPointAnnotation *winston = [[MKPointAnnotation alloc] init];
winston.coordinate = CLLocationCoordinate2DMake(36.09986, -80.244216);
winston.title = #"Winston";
winston.subtitle = #"North Carolina";
[mapView addAnnotation:winston];
MKPointAnnotation *boone = [[MKPointAnnotation alloc] init];
boone.coordinate = CLLocationCoordinate2DMake(36.216795, -81.674552);
boone.title = #"Boone";
boone.subtitle = #"North Carolina";
[mapView addAnnotation:boone];
MKPointAnnotation *raleigh = [[MKPointAnnotation alloc] init];
raleigh.coordinate = CLLocationCoordinate2DMake(35.77959, -78.638179);
raleigh.title = #"Raleigh";
raleigh.subtitle = #"North Carolina";
[mapView addAnnotation:raleigh];
MKPointAnnotation *durham = [[MKPointAnnotation alloc] init];
durham.coordinate = CLLocationCoordinate2DMake(35.994033, -78.898619);
durham.title = #"Durham";
durham.subtitle = #"North Carolina";
[mapView addAnnotation:durham];
MKPointAnnotation *goldsboro = [[MKPointAnnotation alloc] init];
goldsboro.coordinate = CLLocationCoordinate2DMake(35.384884, -77.992765);
goldsboro.title = #"Goldsboro";
goldsboro.subtitle = #"North Carolina";
[mapView addAnnotation:goldsboro];
MKPointAnnotation *kinston = [[MKPointAnnotation alloc] init];
kinston.coordinate = CLLocationCoordinate2DMake(35.262664, -77.581635);
kinston.title = #"Kinston";
kinston.subtitle = #"North Carolina";
[mapView addAnnotation:kinston];
MKPointAnnotation *lagrange = [[MKPointAnnotation alloc] init];
lagrange.coordinate = CLLocationCoordinate2DMake(35.306829, -77.788034);
lagrange.title = #"La Grange";
lagrange.subtitle = #"North Carolina";
[mapView addAnnotation:lagrange];
MKPointAnnotation *oxford = [[MKPointAnnotation alloc] init];
oxford.coordinate = CLLocationCoordinate2DMake(36.3107, -78.590835);
oxford.title = #"Oxford";
oxford.subtitle = #"North Carolina";
[mapView addAnnotation:oxford];
MKPointAnnotation *morrisville = [[MKPointAnnotation alloc] init];
morrisville.coordinate = CLLocationCoordinate2DMake(35.823483, -78.825562);
morrisville.title = #"Morrisville";
morrisville.subtitle = #"North Carolina";
[mapView addAnnotation:morrisville];
MKPointAnnotation *roxboro = [[MKPointAnnotation alloc] init];
roxboro.coordinate = CLLocationCoordinate2DMake(36.393752, -78.982788);
roxboro.title = #"Roxboro";
roxboro.subtitle = #"North Carolina";
[mapView addAnnotation:roxboro];
MKPointAnnotation *henderson = [[MKPointAnnotation alloc] init];
henderson.coordinate = CLLocationCoordinate2DMake(36.329591, -78.399164);
henderson.title = #"Henderson";
henderson.subtitle = #"North Carolina";
[mapView addAnnotation:henderson];
MKPointAnnotation *chapelhill = [[MKPointAnnotation alloc] init];
chapelhill.coordinate = CLLocationCoordinate2DMake(35.9132, -79.055845);
chapelhill.title = #"Chapel Hill";
chapelhill.subtitle = #"North Carolina";
[mapView addAnnotation:chapelhill];
MKPointAnnotation *apex = [[MKPointAnnotation alloc] init];
apex.coordinate = CLLocationCoordinate2DMake(35.732652, -78.850286);
apex.title = #"Apez";
apex.subtitle = #"North Carolina";
[mapView addAnnotation:apex];
// Oklahoma
MKPointAnnotation *lawton = [[MKPointAnnotation alloc] init];
lawton.coordinate = CLLocationCoordinate2DMake(34.603567, -98.395929);
lawton.title = #"Lawton";
lawton.subtitle = #"Oklahoma";
[mapView addAnnotation:lawton];
// Pennsylvania
MKPointAnnotation *mercersburg = [[MKPointAnnotation alloc] init];
mercersburg.coordinate = CLLocationCoordinate2DMake(39.82787, -77.903331);
mercersburg.title = #"Mercersburg";
mercersburg.subtitle = #"Pennsylvania";
[mapView addAnnotation:mercersburg];
// South Carolina
MKPointAnnotation *columbia = [[MKPointAnnotation alloc] init];
columbia.coordinate = CLLocationCoordinate2DMake(34.00071, -81.034814);
columbia.title = #"Columbia";
columbia.subtitle = #"South Carolina";
[mapView addAnnotation:columbia];
MKPointAnnotation *greenville2 = [[MKPointAnnotation alloc] init];
greenville2.coordinate = CLLocationCoordinate2DMake(34.852618, -82.39401);
greenville2.title = #"Greenville";
greenville2.subtitle = #"South Carolina";
[mapView addAnnotation:greenville2];
MKPointAnnotation *mbeach = [[MKPointAnnotation alloc] init];
mbeach.coordinate = CLLocationCoordinate2DMake(33.68906, -78.886694);
mbeach.title = #"Mrytle Beach";
mbeach.subtitle = #"South Carolina";
[mapView addAnnotation:mbeach];
// Tennessee
MKPointAnnotation *chattanooga = [[MKPointAnnotation alloc] init];
chattanooga.coordinate = CLLocationCoordinate2DMake(35.04563, -85.30968);
chattanooga.title = #"Chattanooga";
chattanooga.subtitle = #"Tennessee";
[mapView addAnnotation:chattanooga];
MKPointAnnotation *manchester = [[MKPointAnnotation alloc] init];
manchester.coordinate = CLLocationCoordinate2DMake(35.481743, -86.088599);
manchester.title = #"Manchester";
manchester.subtitle = #"Tennessee";
[mapView addAnnotation:manchester];
MKPointAnnotation *nashville = [[MKPointAnnotation alloc] init];
nashville.coordinate = CLLocationCoordinate2DMake(36.166667, -86.783333);
nashville.title = #"Nashville";
nashville.subtitle = #"Tennessee";
[mapView addAnnotation:nashville];
MKPointAnnotation *knoxville = [[MKPointAnnotation alloc] init];
knoxville.coordinate = CLLocationCoordinate2DMake(35.960638, -83.920739);
knoxville.title = #"Knoxville";
knoxville.subtitle = #"Tennessee";
[mapView addAnnotation:knoxville];
// Washington D.C.
MKPointAnnotation *wdc = [[MKPointAnnotation alloc] init];
wdc.coordinate = CLLocationCoordinate2DMake(38.907192, -77.036871);
wdc.title = #"Washington D.C.";
[mapView addAnnotation:wdc];
// Virgina
MKPointAnnotation *alexandria = [[MKPointAnnotation alloc] init];
alexandria.coordinate = CLLocationCoordinate2DMake(38.804836, -77.046921);
alexandria.title = #"Alexandria";
alexandria.subtitle = #"Virgina";
[mapView addAnnotation:alexandria];
MKPointAnnotation *arlington = [[MKPointAnnotation alloc] init];
arlington.coordinate = CLLocationCoordinate2DMake(38.87997, -77.10677);
arlington.title = #"Arlington";
arlington.subtitle = #"Virgina";
[mapView addAnnotation:arlington];
MKPointAnnotation *fairfax = [[MKPointAnnotation alloc] init];
fairfax.coordinate = CLLocationCoordinate2DMake(38.846224, -77.306373);
fairfax.title = #"Fairfax";
fairfax.subtitle = #"Virgina";
[mapView addAnnotation:fairfax];
MKPointAnnotation *williamsburg = [[MKPointAnnotation alloc] init];
williamsburg.coordinate = CLLocationCoordinate2DMake(37.270702, -76.707457);
williamsburg.title = #"Williamsburg";
williamsburg.subtitle = #"Virgina";
[mapView addAnnotation:williamsburg];
// OCONUS
// ASIA
// SOUTH KOREA
MKPointAnnotation *seoul = [[MKPointAnnotation alloc] init];
seoul.coordinate = CLLocationCoordinate2DMake(37.566535, 126.977969);
seoul.title = #"Seoul";
seoul.subtitle = #"South Korea";
[mapView addAnnotation:seoul];
MKPointAnnotation *suwon = [[MKPointAnnotation alloc] init];
suwon.coordinate = CLLocationCoordinate2DMake(37.263573, 127.028601);
suwon.title = #"Suwon";
suwon.subtitle = #"South Korea";
[mapView addAnnotation:suwon];
MKPointAnnotation *incheon = [[MKPointAnnotation alloc] init];
incheon.coordinate = CLLocationCoordinate2DMake(37.456256, 126.705206);
incheon.title = #"Incheon";
incheon.subtitle = #"South Korea";
[mapView addAnnotation:incheon];
MKPointAnnotation *sokcho = [[MKPointAnnotation alloc] init];
sokcho.coordinate = CLLocationCoordinate2DMake(38.207015, 128.591849);
sokcho.title = #"Sokcho-si";
sokcho.subtitle = #"South Korea";
[mapView addAnnotation:sokcho];
MKPointAnnotation *osan = [[MKPointAnnotation alloc] init];
osan.coordinate = CLLocationCoordinate2DMake(37.14981, 127.077221);
osan.title = #"Osan";
osan.subtitle = #"South Korea";
[mapView addAnnotation:osan];
MKPointAnnotation *pyeongtaek = [[MKPointAnnotation alloc] init];
pyeongtaek.coordinate = CLLocationCoordinate2DMake(36.992108, 127.112945);
pyeongtaek.title = #"Pyeongtaek-si";
pyeongtaek.subtitle = #"South Korea";
[mapView addAnnotation:pyeongtaek];
// THAILAND
MKPointAnnotation *bangkok = [[MKPointAnnotation alloc] init];
bangkok.coordinate = CLLocationCoordinate2DMake(13.727896, 100.524123);
bangkok.title = #"Bangkok";
bangkok.subtitle = #"Thailand";
[mapView addAnnotation:bangkok];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:#"pinView"];
if (!pinView) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pinView"];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = rightButton;
} else {
pinView.annotation = annotation;
}
return pinView;
}
- (void)showMenu
{
[self.sideMenuViewController presentMenuViewController];
}
- (IBAction)segmentedControllChanged:(id)sender {
if (segmentedControl.selectedSegmentIndex == 0) {
mapView.mapType = MKMapTypeStandard;
}else if (segmentedControl.selectedSegmentIndex == 1) {
mapView.mapType = MKMapTypeHybrid;
double cost2 = [config getLatitude];
double cost = [config getLongitude];
CLLocationCoordinate2D location;
location.latitude = cost2;
location.longitude = cost;
MKCoordinateRegion region;
region.center.latitude = (double) cost2;
region.center.longitude = (double) cost;
region.span = MKCoordinateSpanMake(.199, .199);
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
} else if (segmentedControl.selectedSegmentIndex == 2) {
mapView.mapType = MKMapTypeSatellite;
double cost2 = [config getLatitude];
double cost = [config getLongitude];
CLLocationCoordinate2D location;
location.latitude = cost2;
location.longitude = cost;
MKCoordinateRegion region;
region.center.latitude = (double) cost2;
region.center.longitude = (double) cost;
region.span = MKCoordinateSpanMake(.299, .299);
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
any help is welcome :) Thank you
Related
I am showing route between start location and end location on map for iPhone app, i want to show start location at top in map and end location at bottom, also make sure that all route should be visible on map. Below is code i tried.
-(void) viewDidLoad {
MKPointAnnotation *myAnnotation = [[MKPointAnnotation alloc] init];
myAnnotation.coordinate = CLLocationCoordinate2DMake(42.110532,
-87.912126);
myAnnotation.title = #"Start point";
[self.mapView addAnnotation:myAnnotation];
MKPointAnnotation *destinatioAnnotation = [[MKPointAnnotation alloc]
init];
destinatioAnnotation.coordinate = CLLocationCoordinate2DMake(42.105948,
-87.870664);
destinatioAnnotation.title = #"End Point";
[self.mapView addAnnotation:destinatioAnnotation];
CLLocationCoordinate2D startCoord1 =
CLLocationCoordinate2DMake(42.110532, -87.912126);
MKCoordinateRegion adjustedRegion1 = [self.mapView
regionThatFits:MKCoordinateRegionMakeWithDistance(startCoord1, 300000,
300000)];
[self.mapView setRegion:adjustedRegion1 animated:YES];
self.mapView.showsCompass = NO;
MKDirectionsRequest *directionsRequest = [MKDirectionsRequest new];
directionsRequest.requestsAlternateRoutes = NO;
// Make the destination
CLLocationCoordinate2D sourceCoords =
CLLocationCoordinate2DMake(42.110532, -87.912126);
MKPlacemark *sourcePlacemark = [[MKPlacemark alloc]
initWithCoordinate:sourceCoords addressDictionary:nil];
MKMapItem *sourceLoc = [[MKMapItem alloc]
initWithPlacemark:sourcePlacemark];
// Set the source and destination on the request
// Make the destination
CLLocationCoordinate2D destinationCoords = CLLocationCoordinate2DMake(42.105948, -87.870664);
MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoords addressDictionary:nil];
MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];
// Set the source and destination on the request
[directionsRequest setSource:sourceLoc];
[directionsRequest setDestination:destination];
MKDirections *directions = [[MKDirections alloc] initWithRequest:directionsRequest];
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *
_Nullable response, NSError * _Nullable error) {
// Now handle the result
if (error) {
NSLog(#"There was an error getting your directions");
return;
}
// So there wasn't an error - let's plot those routes
_currentRoute = [response.routes firstObject];
[self plotRouteOnMap:_currentRoute];
}];
}
- (void)plotRouteOnMap:(MKRoute *)route {
if(_routeOverlay) {
[self.mapView removeOverlay:_routeOverlay];
}
// Update the ivar
_routeOverlay = route.polyline;
self.routePolyline = route.polyline;
self.mapView.tag = 1;
// Add it to the map
[self.mapView setVisibleMapRect:[route.polyline boundingMapRect] edgePadding:UIEdgeInsetsMake(50, 50, 50, 40) animated:YES];
/*[self.mapView setRegion:MKCoordinateRegionForMapRect([route.polyline
boundingMapRect])
animated:YES];*/
[self.mapView addOverlay:_routeOverlay level:MKOverlayLevelAboveRoads];
[self performSelector:#selector(changeHeading) withObject:nil
afterDelay:1.2];
}
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
NSLog(#"called");
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
if (mapView.tag == 1) {
renderer.strokeColor = [UIColor redColor];
}if (mapView.tag == 2) {
renderer.strokeColor = [UIColor blueColor];
}else{
renderer.strokeColor = [UIColor blueColor];
}
renderer.lineWidth = 4.0;
NSLog(#"x: %f, y:%f, widht: %f, height: %f",[overlay boundingMapRect].origin.x,[overlay
boundingMapRect].origin.y,[overlay
boundingMapRect].size.width,[overlay boundingMapRect].size.height);
return renderer;
}
-(void) changeHeading {
MKMapCamera *camera = self.mapView.camera;
CLLocationDistance altitude = camera.altitude;
NSLog(#"altitude: %f",altitude);
CLLocationDirection mapAngle = camera.heading;
NSLog(#"mapAngle: %f",mapAngle);
if (altitude < 3000 && altitude > 1000) {
// do something
}
if (self.mapView.camera.heading != 257) {
MKMapCamera *newCamera = [MKMapCamera camera];
newCamera.centerCoordinate = self.mapView.camera.centerCoordinate;
newCamera.heading = 257;
newCamera.altitude = self.mapView.camera.altitude;
[self.mapView setCamera:newCamera animated:YES];
}
/*MKMapCamera *turnCam = [MKMapCamera cameraLookingAtCenterCoordinate:CLLocationCoordinate2DMake(42.110532,
-87.912126)
fromEyeCoordinate:CLLocationCoordinate2DMake(42.105948, -87.870664)
eyeAltitude:self.mapView.camera.altitude];
self.mapView.camera = turnCam;*/
}
I have 21 different images that I am using for custom annotations on a map. When I load the map initially, everything is perfect. It is when I navigate off of the area and back in that it seems that the images change. The annotation pins no longer correspond with the correct image (however the title and subtitle are still correct when I press on the annotation). It is like the images suddenly get mixed up. By the way I am just entering test information into NSUserDefaults and using that to test the functionality of displaying the custom annotations on the map. I am assuming it has something to do with the reusability of annotation views but I cannot get my code right.
MapViewController.h
#import "ViewController.h"
#import <MapKit/MapKit.h>
#interface MapViewController : ViewController<MKMapViewDelegate>
{
NSUserDefaults *defaults;
NSMutableArray *reportedSightings;
}
#property (strong, nonatomic) IBOutlet MKMapView *mapView;
#end
MapViewController.m
#import "MapViewController.h"
#import CoreLocation;
#interface MapViewController ()<CLLocationManagerDelegate>
#property (strong, nonatomic) CLLocationManager *locationManager;
#end
#implementation MapViewController
- (void)viewDidLoad
{
[super viewDidLoad];
defaults = [NSUserDefaults standardUserDefaults];
reportedSightings = [[NSMutableArray alloc] init];
[reportedSightings addObject:#"turtle1,44.008897,-77.743073"];
[reportedSightings addObject:#"turtle2,43.997620,-77.675193"];
[reportedSightings addObject:#"turtle3,44.001554,-77.689685"];
[reportedSightings addObject:#"turtle4,43.992655,-77.712643"];
[reportedSightings addObject:#"turtle5,44.005708,-77.725666"];
[reportedSightings addObject:#"snake1,43.993950,-77.720637"];
[reportedSightings addObject:#"snake2,43.994176,-77.717224"];
[reportedSightings addObject:#"snake3,43.998308,-77.677515"];
[reportedSightings addObject:#"snake4,44.007523,-77.719716"];
[reportedSightings addObject:#"snake5,43.997320,-77.731911"];
[reportedSightings addObject:#"snake6,43.995390,-77.716450"];
[reportedSightings addObject:#"amphibian1,43.996503,-77.694154"];
[reportedSightings addObject:#"amphibian2,43.989638,-77.704582"];
[reportedSightings addObject:#"amphibian3,44.009406,-77.738130"];
[reportedSightings addObject:#"amphibian4,44.001059,-77.733429"];
[reportedSightings addObject:#"amphibian5,44.005405,-77.713410"];
[reportedSightings addObject:#"amphibian6,44.002750,-77.699245"];
[reportedSightings addObject:#"amphibian7,43.999160,-77.692886"];
[reportedSightings addObject:#"amphibian8,43.993869,-77.722742"];
[reportedSightings addObject:#"amphibian9,43.995589,-77.714681"];
[reportedSightings addObject:#"amphibian10,43.998462,-77.675608"];
[defaults setObject:reportedSightings forKey:#"reportedSightings"];
[defaults synchronize];
reportedSightings = [NSMutableArray arrayWithArray:[defaults objectForKey:#"reportedSightings"]];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)])
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startUpdatingLocation];
self.mapView.showsUserLocation = YES;
MKCoordinateRegion region = {{0.0, 0.0}, {0.0, 0.0}};
region.center.latitude = 43.998564;
region.center.longitude = -77.709888;
[self.mapView setRegion:region];
for(int i=0; i<reportedSightings.count; i++)
{
NSString *reportedSighting = reportedSightings[i];
NSString *speciesName = [reportedSighting substringToIndex:[reportedSighting rangeOfString:#","].location];
reportedSighting = [reportedSighting substringFromIndex:[reportedSighting rangeOfString:#","].location+1];
NSString *sightingLatitude = [reportedSighting substringToIndex:[reportedSighting rangeOfString:#","].location];
reportedSighting = [reportedSighting substringFromIndex:[reportedSighting rangeOfString:#","].location+1];
NSString *sightingLongitude = reportedSighting;
float latitude = [sightingLatitude floatValue];
float longitude = [sightingLongitude floatValue];
CLLocationCoordinate2D reportedSightingCoordinates = CLLocationCoordinate2DMake(latitude, longitude);
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = reportedSightingCoordinates;
point.title = speciesName;
point.subtitle = [NSString stringWithFormat:#"%f, %f", latitude, longitude];
[self.mapView addAnnotation:point];
}
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ([self.mapView respondsToSelector:#selector(camera)])
{
MKMapCamera *newCamera = [[self.mapView camera] copy];
[newCamera setPitch:0.0];
[newCamera setHeading:307.197710];
[newCamera setAltitude:14515.983058];
[self.mapView setCamera:newCamera animated:YES];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if([annotation isKindOfClass:[MKPointAnnotation class]])
{
MKAnnotationView *pinView = (MKAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:#"CustomViewAnnotation"];
if(!pinView)
{
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"CustomViewAnnotation"];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#Annotation", [annotation title]]];
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
#end
For this section:
if(!pinView)
{
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"CustomViewAnnotation"];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#Annotation", [annotation title]]];
}
else
{
pinView.annotation = annotation;
}
Add the line:
pinView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#Annotation", [annotation title]]];
Also for the else, so it will look like that:
if(!pinView)
{
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"CustomViewAnnotation"];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#Annotation", [annotation title]]];
}
else
{
pinView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#Annotation", [annotation title]]];
pinView.annotation = annotation;
}
I have just started to use MapKit framework, and my first practice did not go well :)
Everything seems to be works fine except MapView showing somewhere in ocean and it did not find any place (ex: Land, island). I think latitude and longitude getting out of range somehow.
here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIBarButtonItem *zoomButton =
[[UIBarButtonItem alloc]
initWithTitle: #"Zoom-In"
style:UIBarButtonItemStyleBordered
target: self
action:#selector(zoomIn:)];
UIBarButtonItem *typeButton =
[[UIBarButtonItem alloc]
initWithTitle: #"Zoom-Out"
style:UIBarButtonItemStyleBordered
target: self
action:#selector(zoomOut:)];
NSArray *buttons = [[NSArray alloc]
initWithObjects:zoomButton, typeButton, nil];
barButtons.items = buttons;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
zoomLocation.latitude = 48.431638;
zoomLocation.longitude= 27.169436;
annotationPoint.coordinate = zoomLocation;
annotationPoint.title = #"My Company";
annotationPoint.subtitle = #"my Company Title";
//[self __mapView];
MKCoordinateRegion adjustedRegion = [__mapView regionThatFits:viewRegion];
[__mapView setRegion:adjustedRegion animated:YES];
[__mapView addAnnotation:annotationPoint];
}
You are setting zoomLocation coordinates after creating region. Try first to set zoomLocation
...
zoomLocation.latitude = 48.431638;
zoomLocation.longitude= 27.169436;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
...
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?
I have many of this annotations(about 2400), but here is my problem.
I get the following errors
potential leak of an object allocated on line 81 and stored into
'annot1'
potential leak of an object allocated on line 81 and stored into 'annot2'
potential leak of an object allocated on line 81 and stored into
'annot3'
And so on. Here is my code:
MKPointAnnotation *annot1 = [[MKPointAnnotation alloc] init];
annot1.title = #"A";
annot1.subtitle=#"A1";
annot1.coordinate = CLLocationCoordinate2DMake(21.978954, 120.752663);
[mapView addAnnotation:annot1];
MKPointAnnotation *annot2 = [[MKPointAnnotation alloc] init];
annot2.title = #"B";
annot2.subtitle=#"B2";
annot2.coordinate = CLLocationCoordinate2DMake(21.988607, 120.748703);
[mapView addAnnotation:annot2];
MKPointAnnotation *annot4 = [[MKPointAnnotation alloc] init];
annot4.title = #"C";
annot4.subtitle=#"C1";
annot4.coordinate = CLLocationCoordinate2DMake(22.008867, 120.743637);
[mapView addAnnotation:annot4];
MKPointAnnotation ***strong text**annot5 = [[MKPointAnnotation alloc] init];
annot5.title = #"D";
annot5.subtitle=#"D1";
annot5.coordinate = CLLocationCoordinate2DMake(22.016190, 120.837601);
[mapView addAnnotation:annot5];
MKPointAnnotation *annot6 = [[MKPointAnnotation alloc] init];
annot6.title = #"E";
annot6.subtitle=#"E1";
annot6.coordinate = CLLocationCoordinate2DMake(22.024183, 120.743401);
[mapView addAnnotation:annot6];
MKPointAnnotation *annot7 = [[MKPointAnnotation alloc] init];
annot7.title = #"F";
annot7.subtitle=#"F1";
annot7.coordinate = CLLocationCoordinate2DMake(22.055653, 121.509689);
[mapView addAnnotation:annot7];
MKPointAnnotation *annot8 = [[MKPointAnnotation alloc] init];
annot8.title = #"G";
annot8.subtitle=#"G2";
annot8.coordinate = CLLocationCoordinate2DMake(22.070082, 120.713684);
[mapView addAnnotation:annot8];
etc
{
If you're not using ARC then you should release the object after you add it to your mapview.
For example:
MKPointAnnotation *annot1 = [[MKPointAnnotation alloc] init];
annot1.title = #"A";
annot1.subtitle=#"A1";
annot1.coordinate = CLLocationCoordinate2DMake(21.978954, 120.752663);
[mapView addAnnotation:annot1];
Should be updated to:
MKPointAnnotation *annot1 = [[MKPointAnnotation alloc] init];
annot1.title = #"A";
annot1.subtitle=#"A1";
annot1.coordinate = CLLocationCoordinate2DMake(21.978954, 120.752663);
[mapView addAnnotation:annot1];
[annot1 release]
The reason is that your object reference count never hit zero and the object is never released.
MKPointAnnotation *annot1 = [[MKPointAnnotation alloc] init];
When you allocate an object it has a reference count of 1. If you add an object to an array or dictionary the reference count is incremented. So after the following block of code you have a reference count of two.
MKPointAnnotation *annot1 = [[MKPointAnnotation alloc] init];
annot1.title = #"A";
annot1.subtitle=#"A1";
annot1.coordinate = CLLocationCoordinate2DMake(21.978954, 120.752663);
[mapView addAnnotation:annot1]
Now, if you call release on annot1 after you add it to your mapview the object isn't really released yet. This is because the data structure in your mapview is holding reference to it.
[mapView addAnnotation:annot1]
Once you're done with your mapview and it is released then annot1 is finally destroyed.