MapKit 帮助。无法先显示我的位置

Posted

技术标签:

【中文标题】MapKit 帮助。无法先显示我的位置【英文标题】:MapKit help. Can't get my location to show up first 【发布时间】:2012-09-27 15:02:05 【问题描述】:

我想开发一张地图,显示我的位置并在地图上显示其他位置的图钉。

我已经显示了地图和图钉,它也可以找到我,但它不会先放大我的位置。我怎样才能让它首先显示我的位置?当我打开地图视图时,它只显示我输入代码的最后一个引脚。 这是我的代码。

mapview.h

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




@interface mapview : UIViewController 

    MKMapView *mapview1;





@property (nonatomic, retain) IBOutletMKMapView *mapview1;

-(IBAction)setMap:(id)sender;

-(IBAction)getlocation;

@end

mapview.m

#import "mapview.h"
#import "NewClass.h"

@implementation mapview

@synthesize mapview1;

-(IBAction)getlocation 

    mapview1.showsUserLocation = YES;





-(IBAction)setMap:(id)sender 
    switch (((UISegmentedControl *) sender).selectedSegmentIndex) 
        case 0:
            mapview1.mapType = MKMapTypeStandard;
            break;
        case 1:
            mapview1.mapType = MKMapTypeSatellite;
            break;
        case 2:
            mapview1.mapType = MKMapTypeHybrid;
            break;
        default:
            break;
    



-(void)viewDidLoad 


    [super viewDidLoad];

        [mapview1 setMapType:MKMapTypeStandard];
        [mapview1 setZoomEnabled:YES];
        [mapview1 setScrollEnabled:YES];

        MKCoordinateRegion region =  0.0, 0.0 , 0.0, 0.0  ;
        region.center.latitude = 39.956907;
        region.center.longitude = -75.610229;
        region.span.longitudeDelta = 0.01f;
        region.span.latitudeDelta = 0.01f;
        [mapview1 setRegion:region animated:YES];


        NewClass *ann = [[NewClass alloc] init];
        ann.title = @"Vigil Location";
        ann.subtitle = @"Planned Parenthood of Chester County";
        ann.coordinate = region.center;
        [mapview1 addAnnotation:ann];


    MKCoordinateRegion region1 =  0.0, 0.0 , 0.0, 0.0  ;
    region1.center.latitude = 40.042819;
    region1.center.longitude = -75.373776;
    region1.span.longitudeDelta = 0.01f;
    region1.span.latitudeDelta = 0.01f;
    [mapview1 setRegion:region1 animated:YES];

    NewClass *ann1 = [[NewClass alloc] init];
    ann1.title = @"Vigil Location";
    ann1.subtitle = @"Planned Parenthood of Bryn Mawr";
    ann1.coordinate = region1.center;
    [mapview1 addAnnotation:ann1];

    MKCoordinateRegion region2 =  0.0, 0.0 , 0.0, 0.0  ;
    region2.center.latitude = 40.18295;
    region2.center.longitude = -75.450163;
    region2.span.longitudeDelta = 0.01f;
    region2.span.latitudeDelta = 0.01f;
    [mapview1 setRegion:region2 animated:YES];

    NewClass *ann2 = [[NewClass alloc] init];
    ann2.title = @"Vigil Location";
    ann2.subtitle = @"Planned Parenthood Collegeville";
    ann2.coordinate = region2.center;
    [mapview1 addAnnotation:ann2];

    MKCoordinateRegion region3 =  0.0, 0.0 , 0.0, 0.0  ;
    region3.center.latitude = 40.120772;
    region3.center.longitude = -75.118181;
    region3.span.longitudeDelta = 0.01f;
    region3.span.latitudeDelta = 0.01f;
    [mapview1 setRegion:region3 animated:YES];

    NewClass *ann3 = [[NewClass alloc] init];
    ann3.title = @"Vigil Location";
    ann3.subtitle = @"Abington Memorial Hospital";
    ann3.coordinate = region3.center;
    [mapview1 addAnnotation:ann3];

    MKCoordinateRegion region4 =  0.0, 0.0 , 0.0, 0.0  ;
    region4.center.latitude = 39.95361;
    region4.center.longitude = -75.15267;
    region4.span.longitudeDelta = 0.01f;
    region4.span.latitudeDelta = 0.01f;
    [mapview1 setRegion:region4 animated:YES];

    NewClass *ann4 = [[NewClass alloc] init];
    ann4.title = @"Vigil Location";
    ann4.subtitle = @"Philadelphia Women's Center";
    ann4.coordinate = region4.center;
    [mapview1 addAnnotation:ann4];

    MKCoordinateRegion region5 =  0.0, 0.0 , 0.0, 0.0  ;
    region5.center.latitude = 39.910566;
    region5.center.longitude = -75.014111;
    region5.span.longitudeDelta = 0.01f;
    region5.span.latitudeDelta = 0.01f;
    [mapview1 setRegion:region5 animated:YES];

    NewClass *ann5 = [[NewClass alloc] init];
    ann5.title = @"Vigil Location";
    ann5.subtitle = @"Cherry Hill Women's Center";
    ann5.coordinate = region5.center;
    [mapview1 addAnnotation:ann5];

    MKCoordinateRegion region6 =  0.0, 0.0 , 0.0, 0.0  ;
    region6.center.latitude = 39.74285;
    region6.center.longitude = -75.550838;
    region6.span.longitudeDelta = 0.01f;
    region6.span.latitudeDelta = 0.01f;
    [mapview1 setRegion:region6 animated:YES];

    NewClass *ann6 = [[NewClass alloc] init];
    ann6.title = @"Vigil Location";
    ann6.subtitle = @"Planned Parenthood of Wilmington";
    ann6.coordinate = region6.center;
    [mapview1 addAnnotation:ann6];

    MKCoordinateRegion region7 =  0.0, 0.0 , 0.0, 0.0  ;
    region7.center.latitude = 40.03754;
    region7.center.longitude = -76.300828;
    region7.span.longitudeDelta = 0.01f;
    region7.span.latitudeDelta = 0.01f;
    [mapview1 setRegion:region7 animated:YES];

    NewClass *ann7 = [[NewClass alloc] init];
    ann7.title = @"Vigil Location";
    ann7.subtitle = @"Planned Parenthood of Lancaster";
    ann7.coordinate = region7.center;
    [mapview1 addAnnotation:ann7];

    MKCoordinateRegion region8 =  0.0, 0.0 , 0.0, 0.0  ;
    region8.center.latitude = 40.333819;
    region8.center.longitude = -75.93019;
    region8.span.longitudeDelta = 0.01f;
    region8.span.latitudeDelta = 0.01f;
    [mapview1 setRegion:region8 animated:YES];

    NewClass *ann8 = [[NewClass alloc] init];
    ann8.title = @"Vigil Location";
    ann8.subtitle = @"Planned Parenthood of Reading";
    ann8.coordinate = region8.center;
    [mapview1 addAnnotation:ann8];

    MKCoordinateRegion region9 =  0.0, 0.0 , 0.0, 0.0  ;
    region9.center.latitude = 40.67246;
    region9.center.longitude = -75.375397;
    region9.span.longitudeDelta = 0.01f;
    region9.span.latitudeDelta = 0.01f;
    [mapview1 setRegion:region9 animated:YES];

    NewClass *ann9 = [[NewClass alloc] init];
    ann9.title = @"Vigil Location";
    ann9.subtitle = @"Planned Parenthood of Allentown";
    ann9.coordinate = region9.center;
    [mapview1 addAnnotation:ann9];

    MKCoordinateRegion region10 =  0.0, 0.0 , 0.0, 0.0  ;
    region10.center.latitude = 39.952384;
    region10.center.longitude = -76.7253;
    region10.span.longitudeDelta = 0.01f;
    region10.span.latitudeDelta = 0.01f;
    [mapview1 setRegion:region10 animated:YES];

    NewClass *ann10 = [[NewClass alloc] init];
    ann10.title = @"Vigil Location";
    ann10.subtitle = @"Planned Parenthood of York";
    ann10.coordinate = region10.center;
    [mapview1 addAnnotation:ann10];



- (void)didReceiveMemoryWarning 
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.


- (void)viewDidUnload 
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;



- (void)dealloc 
    [super dealloc];



@end

【问题讨论】:

请尽量不要粘贴代码墙。它将关闭许多不想阅读整个代码块的潜在回答者。请尝试将问题缩小到更小的 sn-p 代码 【参考方案1】:

您已经设置了 showsUserLocation,但如果您阅读 Apple 文档,您会发现该功能仅打开蓝点,它不会缩放或平移视图。 "http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/occ/instp/MKMapView/showsUserLocation

您要做的是将用户跟踪模式设置为用户关注。

其他代码提示:

将所有这些地方放在一个数组中并循环遍历它们,不要像那样一次创建一个 不要在地图上为您创建的每个区域设置区域。您正在尝试让地图放大用户,并且每个 setRegion 都会更改地图的查看位置。无论如何,只有最后一个会有任何影响。 您无需为了获取注释的坐标而创建区域

【讨论】:

好酷。编码将如何跟踪用户位置?对不起,我真的是 xcode 文盲 和我上面说的差不多mapview1.userTrackingMode = MKUserTrackingModeFollow ref:developer.apple.com/library/ios/documentation/MapKit/Reference/…

以上是关于MapKit 帮助。无法先显示我的位置的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 MapKit 让我的应用放大到我的位置

Swift Mapkit - 远离用户位置

为啥我的 MapKit 中的蓝点(用户位置)会移动?

计算我的位置和 Swift 上的 MapKit 引脚之间的距离

Mapkit 显示注释区域

如何在mapkit中以像素为单位获取米?