Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何创建MKMapView?_Iphone - Fatal编程技术网

Iphone 如何创建MKMapView?

Iphone 如何创建MKMapView?,iphone,Iphone,文档中没有太多关于它的内容,而且似乎没有用于此的init方法?如何创建并设置要在地图视图中显示的经度和纬度或区域?界面生成器包括MKMapView(地图视图)。将元素拖到XIB中,在控制器中添加引用插座,将它们链接起来。然后,设置区域。有很多很好的例子: 您可以通过代码或界面生成器包含MKMapView 对于Interface builder,只需将其拖放到xib即可。(工具->库->地图视图) 按代码 在您的.h文件中 MKMapView * mapView; 在你的.m文件中 -(void

文档中没有太多关于它的内容,而且似乎没有用于此的init方法?如何创建并设置要在地图视图中显示的经度和纬度或区域?

界面生成器包括MKMapView(地图视图)。将元素拖到XIB中,在控制器中添加引用插座,将它们链接起来。然后,设置区域。有很多很好的例子:


您可以通过代码或界面生成器包含MKMapView

对于Interface builder,只需将其拖放到xib即可。(工具->库->地图视图)

按代码

在您的.h文件中

MKMapView * mapView;
在你的.m文件中

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

首先,添加MapKit.framework。
然后,在.h文件中

#import <MapKit/MapKit.h>

mapview示例编码以查找位置

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

示例代码:您应该能够使用“initWithFrame”(在UIView文档中)分配和初始化视图,然后将视图作为子视图添加到父视图中。
(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];
 }
@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.
}