views:

312

answers:

1

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.

A: 

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
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.