22

タイトルにあるように、ボタンを押して、自分のアプリ内から ios デバイスでネイティブ マップ アプリを開きたいと考えています。現在MKmapview、ファイルから取得した緯度/経度の単純なピンを表示するを使用していjsonます。

コードはこれです:

- (void)viewDidLoad {
    [super viewDidLoad]; 
}


// We are delegate for map view
self.mapView.delegate = self;

// Set title
self.title = self.location.title;

// set texts...
self.placeLabel.text = self.location.place;


self.telephoneLabel.text = self.location.telephone;
self.urlLabel.text = self.location.url;


**// Make a map annotation for a pin from the longitude/latitude points
MapAnnotation *mapPoint = [[MapAnnotation alloc] init];
mapPoint.coordinate = CLLocationCoordinate2DMake([self.location.latitude doubleValue], [self.location.longitude doubleValue]);
mapPoint.title = self.location.title;**

// Add it to the map view
[self.mapView addAnnotation:mapPoint];

// Zoom to a region around the pin
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(mapPoint.coordinate, 500, 500);
[self.mapView setRegion:region];

}`

ピンに触れると、タイトルと情報ボタンを含む情報ボックスが表示されます。

これはコードです:

    #pragma mark - MKMapViewDelegate

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKPinAnnotationView *view = nil;
    static NSString *reuseIdentifier = @"MapAnnotation";

    // Return a MKPinAnnotationView with a simple accessory button
    view = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier];
    if(!view) {
        view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
        view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        view.canShowCallout = YES;
        view.animatesDrop = YES;
    }

    return view;
}

上の情報ボックスのボタンをクリックすると、現在のユーザーの場所から上の mapPoint への道順でマップ アプリを開くメソッドを作成したいと考えています。それは可能ですか?また、このボタンの外観をカスタマイズできますか? (つまり、ボタンに別の画像を配置して、「ちょっと方向を押して」のように見せます)。

ばかげた質問で申し訳ありませんが、これは私の最初の iOS アプリであり、Obj-c は私にとってまったく新しい言語です。

事前にすべての返信をありがとう。

4

3 に答える 3

17

このコードを使用して方向のあるマップを開くことができます: (id< MKAnnotation > クラスに「coordinate」という名前の CLLocationCoordinate2D パブリック プロパティがあると仮定します)

MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:[annotation coordinate] addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:"WhereIWantToGo"]];
NSDictionary *options = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};
[mapItem openInMapsWithLaunchOptions:options];

ボタンを変更することもできます。実際には、標準スタイルのボタンを使用しています。

[UIButton buttonWithType:UIButtonTypeDetailDisclosure];

ただし、カスタム ボタンに画像またはラベルを割り当てることができます。

[[UIButton alloc] initWithImage:[UIImage imageNamed:"directionIcon.png"]];
于 2014-02-24T09:34:11.823 に答える