苹果地图搜索在ios objective-c中不起作用?
我想在我的应用程序中打开apple map。我写了下面的函数。地图会打开,但不会搜索地址。我错过了什么苹果地图搜索在ios objective-c中不起作用?,ios,objective-c,Ios,Objective C,我想在我的应用程序中打开apple map。我写了下面的函数。地图会打开,但不会搜索地址。我错过了什么 + (void)openMap:(NSString*)address { Class mapItemClass = [MKMapItem class]; if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOpti
+ (void)openMap:(NSString*)address
{
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
MKPlacemark* placemark = [[MKPlacemark alloc] initWithCoordinate:kCLLocationCoordinate2DInvalid
addressDictionary:nil];
MKMapItem* mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:address];
[mapItem openInMapsWithLaunchOptions:nil];
}
}
不清楚你期望什么。你的工作是给你的地标一个坐标。您没有(您使用的是
KCLLocationCoordinated2Invalid
)。所以地图应用程序根本不知道去哪里
另一方面,如果您的目的是将地址转换为坐标,则可以使用CLGeocoder。这是。您需要提供适当的纬度和经度来放置pin,您可以像下面这样操作
func openMapForPlace() {
let regionDistance: CLLocationDistance = 10000000
let coordinates = CLLocationCoordinate2DMake(40, 0)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
let options = [
MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span)
]
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = "Test"
MKMapItem.openMapsWithItems([mapItem], launchOptions: options)
}
对于目标C:
CLLocationDistance regionDistance = 100000;
CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake(40, 0);
MKCoordinateRegion regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance);
MKPlacemark *placeMark = [[MKPlacemark alloc] initWithCoordinate:coordinates addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark: placeMark];
mapItem.name = @"Test";
[MKMapItem openMapsWithItems:@[mapItem] launchOptions:nil];
您可以根据需要将选项字典添加到启动选项中
如果要从字符串中获取lat long,请使用geocoder
CLGeocoder().geocodeAddressString(txtSearchField.text!, completionHandler: { (placemarks, error) -> Void in
if error == nil {
if let placemark = placemarks!.first {
let noLocation:CLLocationCoordinate2D = placemark.location!.coordinate
}
} })
嗯。我找到了答案
我用了这个:
NSString *urlString = [NSString stringwithformat:@"http://maps.apple.com/?address=%@", address];
NSURL* mapURL = [NSURL URLWithString:[urlString
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
[[UIApplication sharedApplication] openURL:phoneURL];
}
最后,我决定不使用苹果地图。因为它没有完全定位这个位置。我将使用谷歌地图网址 我不能在不提供坐标的情况下使用地图吗?但我想搜索和定位一个地方。我的应用程序不包含地图组件。它在外部打开apple maps应用程序。objective-c中的等效项?错误位置。它不起作用。您给出的位置似乎是恒定的。是的,这是您需要更改CLLocationCoordinate2DMake(40,0)的恒定位置;对于您的动态值,我没有位置值。我刚查到地址你的地址是什么?我指的是lat lng或地名。