MapKit帮助。无法让我的位置首先出现

时间:2012-09-27 15:02:05

标签: ios geolocation mapkit cllocationmanager

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

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

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

1 个答案:

答案 0 :(得分:2)

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

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

其他代码提示:

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