2010-06-25 25 views
9

La documentación no habla mucho al respecto, y parece que no hay un método init para esto? ¿Cómo crearía uno y establecería la longitud y la latitud o región para mostrar en la vista del mapa?¿Cómo crear un MKMapView?

+0

código de ejemplo: http://developer.apple. com/iphone/library/samplecode/MapCallouts/Introduction/Intro.html – lukya

Respuesta

2

El generador de interfaces incluye MKMapView (Vista de mapa). Arrastre el elemento a su XIB, agregue una toma de referencia en su controlador, conéctelos. Luego, establece la región. Un montón de buenos ejemplos:

http://developer.apple.com/iphone/library/samplecode/WorldCities/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009466

+0

Debería poder asignar e inicializar la vista usando 'initWithFrame' (en la documentación de UIView), luego agregar la vista a la vista principal como una subvista. –

5

Puede incluir MKMapView tanto por código o por el constructor de interfaz.

Para constructor de interfaz sólo tienes que arrastrar & caer a su xib. (Herramientas-> Biblioteca-> MapView)

Por código

En su archivo .h

MKMapView * mapView; 

En su .m archivo

-(void)viewWillAppear:(BOOL)animated 
{ 
    self.mapView = [[[MKMapView alloc] initWithFrame:self.view.frame] autorelease]; 
    [self.view addSubview:self.mapView];    
} 
20

Primero, agregue MapKit.framework.
A continuación, en el archivo .h

#import <MapKit/MapKit.h> 

y añadir delegado <MKMapViewDelegate>.

A continuación, en archivos .m, agregue el siguiente código:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.frame]; 
    [self.view addSubview:mapView]; 
} 
0
(void)viewDidLoad { 
    [super viewDidLoad]; 
    MKMapView *myMapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    [self.view addSubview:myMapView]; 
} 
1

codificación muestra MapView para encontrar una ubicación

@interface mapViewController() 

@end 

@implementation mapViewController 

- (void)viewDidLoad { 
[super viewDidLoad]; 


self.title=self.name; 

CLLocationCoordinate2D myCoordinate = 
_mapView.userLocation.coordinate; 
    myCoordinate.latitude =[self.lat doubleValue]; 
myCoordinate.longitude =[self.lng doubleValue]; 


// NSLog(@"--->%@",self.lat); 
//  NSLog(@"--->%@",self.lng); 
//set location and zoom level 
MKCoordinateRegion viewRegion = 
    MKCoordinateRegionMakeWithDistance(myCoordinate, 1000, 1000); 
    MKCoordinateRegion adjustedRegion = [self.mapView 
    regionThatFits:viewRegion]; 
[self.mapView setRegion:adjustedRegion animated:YES]; 

MKPointAnnotation *point = [[MKPointAnnotation alloc] init]; 
// Set your annotation to point at your coordinate 
point.coordinate = myCoordinate; 
point.title = self.address; 

//Drop pin on map 
[self.mapView addAnnotation:point]; 

self.mapView.delegate = self; 
// Do any additional setup after loading the view. 
}