4

これに関するいくつかのチュートリアルと質問がありますが、特定のアプリにそれらを実装する方法を理解するにはまだ十分な知識がありません. URL から JSON 注釈データを取得して解析し、各注釈を for ループに追加します。各注釈にリンクを追加して、マップを開いてルートを表示したいと考えています。

これが私のViewController.Hです

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

//MAP Setup
@interface ViewController : UIViewController <MKMapViewDelegate>

//map setup
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic, strong) NSMutableData *downloadData;
//- (IBAction)refreshTapped:(id)sender;


@end

そして私のViewController.m

- (void)viewDidLoad
{
    ////////////////////////
    //Connection to download JSON map info
    ////////////////////////
    self.downloadData = [NSMutableData new];

    NSURL *requestURL2 = [NSURL URLWithString:@"http:OMITTED"];
    NSURLRequest *request = [NSURLRequest requestWithURL:requestURL2];
    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];

    //scroller
    [scroller setScrollEnabled:YES];
    [scroller setContentSize:CGSizeMake(320,900)];

    [super viewDidLoad];    

//Map
    [self.mapView.userLocation addObserver:self
                                forKeyPath:@"location"
                                   options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
                                   context:nil];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.downloadData appendData:data];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    id parsed = [NSJSONSerialization JSONObjectWithData:_downloadData options:kNilOptions error:nil];

    ////////////////////////
    //Iterating and adding annotations
    ////////////////////////
    for (NSDictionary *pointInfo in parsed)
    {
        NSLog([pointInfo objectForKey:@"name"]);
        double xCoord = [(NSNumber*)[pointInfo objectForKey:@"lat"] doubleValue];
        double yCoord = [(NSNumber*)[pointInfo objectForKey:@"lon"] doubleValue];
        CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(xCoord, yCoord);


        MKPointAnnotation *point = [MKPointAnnotation new];
        point.coordinate = coords;
        point.title = [pointInfo objectForKey:@"name"];

        [self.mapView addAnnotation:point];// or whatever your map view's variable name is
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//centers map on user loc and then allows for movement of map without re-centering on userlocation check.
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([self.mapView showsUserLocation])
    {
        MKCoordinateRegion region;
        region.center = self.mapView.userLocation.coordinate;

        MKCoordinateSpan span;
        span.latitudeDelta  = .50; // Change these values to change the zoom
        span.longitudeDelta = .50;
        region.span = span;

        [self.mapView setRegion:region animated:YES];

        self.mapView.showsUserLocation = NO;}
}

- (void)dealloc
{
    [self.mapView.userLocation removeObserver:self forKeyPath:@"location"];
    [self.mapView removeFromSuperview]; // release crashes app
    self.mapView = nil;
}

@end
4

1 に答える 1

7

Location Awarenessプログラミングガイドのマップアプリを起動すると、次のようになります。

独自のアプリではなくマップアプリにマップ情報を表示したい場合は、次の2つの手法のいずれかを使用してプログラムでマップを起動できます。

iOS 6以降では、MKMapItemオブジェクトを使用してマップを開きます。iOS 5以前では、 Apple URLスキームリファレンス
の説明に従って、特別にフォーマットされたマップURLを作成して開きます。 マップアプリを開くための推奨される方法は、クラスを使用することです。このクラスは、アプリを開いて場所や道順を表示するためのクラスメソッドとインスタンスメソッドの両方を提供します。
MKMapItemopenMapsWithItems:launchOptions:openInMapsWithLaunchOptions:

マップアプリを開く方法の例については、「マップアプリに道順を表示するように依頼する」をご覧ください。</a>

したがって、次のことを行う必要があります。

  1. delegateビューコントローラをマップビュー用に定義してください。

  2. コールアウトアクセサリビューをオンにしてオンにするaviewForAnnotationを記述します。canShowCallout

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
        if ([annotation isKindOfClass:[MKUserLocation class]])
            return nil;
    
        MKAnnotationView* annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                                        reuseIdentifier:@"MyCustomAnnotation"];
    
        annotationView.canShowCallout = YES;
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    
        return annotationView;
    }
    
  3. 次に、calloutAccessoryControlTappedサポートしているiOSのバージョンに基づいて、上記のようにマップを開くメソッドを記述します。たとえば、iOS6の場合です。

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    {
        id <MKAnnotation> annotation = view.annotation;
        CLLocationCoordinate2D coordinate = [annotation coordinate];
        MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
        MKMapItem *mapitem = [[MKMapItem alloc] initWithPlacemark:placemark];
        mapitem.name = annotation.title;
        [mapitem openInMapsWithLaunchOptions:nil];
    }
    

    KMLにどのような追加の地理情報があるかはわかりませんが、適切と思わaddressDictionaryれる場合は、おそらく入力できます。


イニシャライザメソッドのaddressDictionaryパラメータの使用方法に関するフォローアップの質問に答えると、ストリート、、、、、などの変数がある場合は、次のようになります。MKPlacemarkinitWithCoordinateNSStringaddresscitystatezip

NSDictionary *addressDictionary = @{(NSString *)kABPersonAddressStreetKey : street,
                                    (NSString *)kABPersonAddressCityKey   : city,
                                    (NSString *)kABPersonAddressStateKey  : state,
                                    (NSString *)kABPersonAddressZIPKey    : zip};

これを機能させるには、プロジェクトに適切なフレームワークを追加AddressBook.framework、.mファイルにヘッダーをインポートする必要があります。

#import <AddressBook/AddressBook.h>

ただし、本当の問題は、マップアプリに「不明な場所」として表示されないようにを設定する方法nameでした。MKMapItemこれは、プロパティを設定するのと同じくらい簡単ですname。おそらく、 :titleからを取得するだけです。annotation

mapitem.name = annotation.title;
于 2013-02-15T19:56:43.723 に答える