2

Iam making an iphone app, which should take in latitude and longitudes from an array and should locate and display the map with a customized annotation/pins. I have used the mapkit here's how:

//MapViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MapViewController : UIViewController  <MKMapViewDelegate> {
 IBOutlet MKMapView *mapView;
 MKPlacemark *mPlacemark;
}

@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@end


//MapViewController.m

#import "MapViewController.h"
#import <MapKit/MapKit.h>

@implementation MapViewController
@synthesize mapView;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    CLLocationCoordinate2D location = mapView.userLocation.coordinate;
    MKCoordinateRegion region;
    MKCoordinateSpan span;

    location.latitude  = 37.250556;
    location.longitude = -96.358333;

    span.latitudeDelta = 0.05;
    span.longitudeDelta = 0.05;

    region.span = span;
    region.center = location;

    [mapView setRegion:region animated:YES];
    [mapView regionThatFits:region];
}

 - (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [mapView setNeedsDisplay];    
}


@end

This does not locate the place for the latitude and longitude. Please help.

Vladimir
  • 170,431
  • 36
  • 387
  • 313
binbash
  • 149
  • 3
  • 8
  • 2
    Please next time format your code before posting (select it and click '010101' button on toolbar. And do not include code parts that are obviously not necessary - it will be much easier for others to help you. – Vladimir Jul 21 '10 at 08:49
  • sure will do next time onwards... – binbash Jul 21 '10 at 09:13

2 Answers2

1

I have made a sample project with your code and it works fine - map gets positioned to the expected (it seems) region. Check if your mapView ivar is initialized (are connections set properly in IB?)

Edit: In your code you just set map's visible region but do not add any annotations to it (apart from automatically showing current user position which is always in Cupertino if you test on simulator). To put pin to you map you need to create an object confirming to MKAnnotation protocol and add it to mapView:

// Sample example just to show annotation
// in your program you will likely need to use custom annotation objects 
CLLocation* myLocation = [[CLLocation alloc] initWithLatitude:37.250556 longitude:-96.358333];
[mapView addAnnotation:myLocation];

Some (not so relevant) comments on your code:

  • You don't need to initialize location variable with current location as you overwrite its coordinate values immediately after that
  • Why do you call [mapView setNeedsDisplay]; in viewDidUnload method? I'm not sure if this may cause serious problems but you must use this method for cleaning memory up (e.g. releasing retained outlets), not for redrawing your UI
Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • Well it does display the map with the pin on california, cupertino, which is the default location, but the coordinates I have given in the above code should pin it on Kansas.I have removed the initialization and setNeedDisplay, but it pins the placemark on cupertino rather than kansas. – binbash Jul 21 '10 at 09:16
  • Interestingly enough, I'm not sure why your code snippet works. `CLLocation` does not actually conform to `MKAnnotation` (but it does work when I run that code). – Nate May 23 '12 at 03:05
  • @Nate, MKAnnotation protocol has only 1 required method - 'coordinate' and CLLocation has that one so there won't be problems in run-time although most likely compiler will issue a warning when you add CLLocation as an annotation to mapview. Anyway that was just an example and in real life you should use custom objects that conform to MKAnnotation protocol – Vladimir May 23 '12 at 06:24
  • Yep. The compiler warns about it, but of course, we all know that in Objective-C, that doesn't mean it won't work :). It sounds like Apple probably didn't make CLLocation formally implement MKAnnotation because they didn't want CoreLocation having a dependency on MapKit ... but, in reality, CLLocation effectively does conform to that protocol, as it currently stands – Nate May 23 '12 at 08:10
0

Have you set your map to follow user location somewhere else? maybe that default is overruling your manual movement of the map to Kansas.

edit Do'h, didn't see this was July last year. Well maybe you could share your solution with us if this wasn't the problem

Craig
  • 8,093
  • 8
  • 42
  • 74