Позвоните в Карты для получения инструкций изнутри вашего приложения - iOS5 iOS6

Вот странная проблема: мое приложение должно вызывать встроенные Карты в iOS (как 5.1, так и 6). Оказывается, что он прекрасно работает под iOS6, но не под iOS5.1. Карты в iOS6 называются и прослеживаются направления от saddr к daddr, но когда я нахожусь в iOS5, вызывается приложение карт, но в daddr помещается только один пин-код. По неизвестной причине начальные координаты (saddr) не отображаются и направление не отслеживается.

Вот мой код:

addr = [NSString stringWithFormat: @"maps://saddr=%f,%f&daddr=%f,%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude, oldLatitude, oldLongitude];
NSURL *url = [NSURL URLWithString:addr];
[[UIApplication sharedApplication] openURL:url];

Я попытался изменить URL-адрес на "http://maps.google.com/something", но вместо встроенного приложения "Карты" он вызывает Safari. Я заметил, что переменные правильно передаются в URL.

Есть идеи?

Заранее спасибо!

2 ответа

Решение

У меня была похожая проблема, и мне пришлось создать условный код ОС, чтобы справиться с тем фактом, что приложение Google Maps было удалено. Из новой справки MKMapItem

//first create latitude longitude object
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude,longitude);

//create MKMapItem out of coordinates
MKPlacemark* placeMark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
MKMapItem* destination =  [[MKMapItem alloc] initWithPlacemark:placeMark];

if([destination respondsToSelector:@selector(openInMapsWithLaunchOptions:)])
{
    //using iOS6 native maps app
    [destination openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving}];        
} 
else
{
    //using iOS 5 which has the Google Maps application
    NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=Current+Location&daddr=%f,%f", latitude, longitude];
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
}

[placeMark release];
[destination release];

Чтобы получить прогулочные маршруты:

  1. Для карт iOS 6 - Вы можете установить MKLaunchOptionsDirectionsModeWalking вместо MKLaunchOptionsDirectionsModeDriving
  2. Для карт Google - Добавить &dirflg=w на URL.

Я думаю, что лучше использовать openInMapsWithLaunchOptions в iOS6, потому что это дает вам полный контроль над тем, как будет реагировать приложение карт.

Ты можешь использовать MKPlacemark а также MKMapItem чтобы запустить приложение "Карты" с координатой и заголовком на выводе карты:

NSString *pinTitle;
CLLocationCoordinate2D coordinate;

MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:@{(id)kABPersonAddressStreetKey: pinTitle}];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];

if ([mapItem respondsToSelector:@selector(openInMapsWithLaunchOptions:)])
{
    [mapItem openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving}];
}
else
{
    // Google Maps fallback
    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%f,%f&saddr=Current+Location", locationItem.coordinate.latitude, locationItem.coordinate.longitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}

Обратите внимание, что вам нужно будет связать с AddressBook.framework а также добавить #import <AddressBook/AddressBook.h> где-то в вашем коде, чтобы использовать kABPersonAddressStreetKey постоянная.

Другие вопросы по тегам