0

自分の場所を示し、マップ上に他の場所のピンが表示されるマップを作成したいと考えています。

地図が表示され、ピンも表示されます。これも私の位置を特定しますが、最初に私の位置にズームインしません。最初に自分の場所を表示するにはどうすればよいですか? マップビューを開くと、コードに挿入した最後のピンが表示されます。これが私のコードです。

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
4

1 に答える 1

2

showsUserLocation を設定しましたが、Apple ドキュメントを読むと、その関数は青い点をオンにするだけで、ビューをズームまたはパンしないことがわかります。"http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/occ/instp/MKMapView/showsUserLocation

あなたがしたいことは、ユーザー追跡モードを userfollows に設定することです。

その他のコードのヒント:

  • それらすべての場所を配列に入れ、それらをループします。そのように一度に1つずつ作成しないでください
  • 作成するすべての地域に対して、地図上で地域を設定しないでください。マップをユーザーにズームインさせようとしていますが、各 setRegion はマップが見ている場所を変更します。とにかく最後のものだけが効果があります。
  • 注釈の座標を取得するためだけにリージョンを作成する必要はありません
于 2012-09-28T04:30:11.787 に答える