首页 文章

从ios应用程序打开带有路线的苹果 Map 应用程序

提问于
浏览
17

正如 Headers 所述,我想通过按下按钮在我自己的应用程序中打开ios设备中的本机 Map 应用程序 . 目前我使用 MKmapview 显示一个简单的引脚,其中lat / long取自 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];

}`

当您触摸图钉时,会出现一个带有 Headers 和信息按钮的信息框 .

这是代码:

#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;
}

当我点击上面信息框中的按钮时,我想制作一个方法,打开 Map 应用程序,其中包含从当前用户位置到上面mapPoint的路线 . 那可能吗?我也可以自定义这个按钮的外观吗? (我的意思是喜欢在按钮上放置一个不同的图像,使其看起来像“按我指示方向”) .

很抱歉,如果这是一个愚蠢的问题,但这是我的第一个ios应用程序,Obj-c对我来说是一种全新的语言 .

感谢所有的回复提前 .

3 回答

  • 14

    您初始化一个按钮并向按钮添加操作:

    Objective-C Code

    NSString* directionsURL = [NSString stringWithFormat:@"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",self.mapView.userLocation.coordinate.latitude, self.mapView.userLocation.coordinate.longitude, mapPoint.coordinate.latitude, mapPoint.coordinate.longitude];
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:options:completionHandler:)]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString: directionsURL] options:@{} completionHandler:^(BOOL success) {}];
    } else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString: directionsURL]];
    }
    

    使用mapPoint是您要指向的位置 .

    Swift3 or later

    let directionsURL = "http://maps.apple.com/?saddr=35.6813023,139.7640529&daddr=35.4657901,139.6201192"
    guard let url = URL(string: directionsURL) else {
        return
    }
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    } else {
        UIApplication.shared.openURL(url)
    }
    

    请注意, saddrdaddr 可以是位置名称或位置坐标 . 所以 directionsURL 可以是:

    // directions with location coordinate
    "http://maps.apple.com/?saddr=35.6813023,139.7640529&daddr=35.4657901,139.6201192"
    // or directions with location name
    "http://maps.apple.com/?saddr=Tokyo&daddr=Yokohama"
    // or directions from current location to destination location
    "http://maps.apple.com/?saddr=Current%%20Location&daddr=Yokohama"
    

    更多选项参数(如传输类型, Map 类型......)请查看here

  • 37

    您可以使用以下代码打开带有方向的 Map :(假设您的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"]];
    
  • 7

    以下是在Apple Map 上显示路线的工作代码 . 它适用于您目的地的当前位置,您只需要通过目的地的纬度和长度 .

    double destinationLatitude, destinationLongitude;
    destinationLatitude=// Latitude of destination place.
    destinationLongitude=// Longitude of destination place.
    
    Class mapItemClass = [MKMapItem class];
    
    if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
    {
        // Create an MKMapItem to pass to the Maps app
        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(destinationLatitude,destinationLongitude);
        MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
    
        MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
        [mapItem setName:@"Name/text on destination annotation pin"];
    
        // Set the directions mode to "Driving"
        // Can use MKLaunchOptionsDirectionsModeDriving instead
        NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};
    
        // Get the "Current User Location" MKMapItem
        MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];
    
        // Pass the current location and destination map items to the Maps app
        // Set the direction mode in the launchOptions dictionary
        [MKMapItem openMapsWithItems:@[currentLocationMapItem, mapItem] launchOptions:launchOptions];
    }
    

    另外,请在这里与我分享,如果发现任何问题,或者我们需要找到另一种方法来完成它 .

相关问题