开发者

initializing the mapkit with a particular latitude and longitude Iphone

开发者 https://www.devze.com 2023-01-08 17:55 出处:网络
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:

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. P开发者_运维百科lease help.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消