gpt4 book ai didi

ios - 如何在 iOS 6 中使用特定地址启动 iOS map 应用程序?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:49:53 25 4
gpt4 key购买 nike

我有一个应用程序,我允许用户在其中启动 map 应用程序(Google 或 Apple)来查看地址。

我曾经这样做过:

Address *address = [self.person.addresses objectAtIndex:0]; 

NSString *addressString = [NSString stringWithFormat:@"%@ %@ %@ %@, %@ %@",
address.line1 == nil ? @"" : address.line1,
address.line2 == nil ? @"" : address.line2,
address.line3 == nil ? @"" : address.line3,
address.city == nil ? @"" : address.city,
address.state == nil ? @"" : address.state,
address.zip == nil ? @"" : address.zip];

NSString *mapsURL = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", [addressString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mapsURL]];

但为了支持 iOS 6,我将其更改为:

Address *address = [self.person.addresses objectAtIndex:0]; 

NSString *addressString = [NSString stringWithFormat:@"%@ %@ %@ %@, %@ %@",
address.line1 == nil ? @"" : address.line1,
address.line2 == nil ? @"" : address.line2,
address.line3 == nil ? @"" : address.line3,
address.city == nil ? @"" : address.city,
address.state == nil ? @"" : address.state,
address.zip == nil ? @"" : address.zip];

// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
/*
// Create an MKMapItem to pass to the Maps app
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:nil
addressDictionary:nil];

MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem openInMapsWithLaunchOptions:nil];
*/

// I want something like this:
MKMapItem *mapItem = [[MKMapItem alloc] initWithAddressQuery:addressString];
[mapItem openInMapsWithLaunchOptions:nil];
}
else
{
NSString *mapsURL = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", [addressString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mapsURL]];

}

基本上,使用原始地址(没有 ABPersonRef 或任何东西)我需要在 Apple map 中打开位置。谷歌曾经做得很好。

我尝试了从 maps.google.commaps.apple.com 的简单切换,但在 iOS 5 中它会打开 Google map 网络应用程序——我不要!有一个非常好的 native 应用程序。

最佳答案

找到答案 here .它是通过使用 CLGeocoder 类来完成的:

// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:addressString
completionHandler:^(NSArray *placemarks, NSError *error) {

// Convert the CLPlacemark to an MKPlacemark
// Note: There's no error checking for a failed geocode
CLPlacemark *geocodedPlacemark = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc]
initWithCoordinate:geocodedPlacemark.location.coordinate
addressDictionary:geocodedPlacemark.addressDictionary];

// Create a map item for the geocoded address to pass to Maps app
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:geocodedPlacemark.name];

// Set the directions mode to "Driving"
// Can use MKLaunchOptionsDirectionsModeWalking 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];

}];
}
//iOS 4/5:
else
{
NSString *mapsURL = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", [addressString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
...

关于ios - 如何在 iOS 6 中使用特定地址启动 iOS map 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13535027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com