我正在 Xcode 4.3.3、iOS 5 上开发并使用 Mapkit 库。该应用程序应该在谷歌地图上显示当前位置,获取目的地地址,最后绘制这两点之间的最短路径。
我使用本教程来实现应用程序,我现在有当前位置:http: //blog.objectgraph.com/index.php/2009/04/02/iphone-sdk-30-playing-with-map-kit/
我寻找路由,但我没有找到任何资源。请指导我如何绘制当前位置和目的地之间的最短路径。
谢谢!
我正在 Xcode 4.3.3、iOS 5 上开发并使用 Mapkit 库。该应用程序应该在谷歌地图上显示当前位置,获取目的地地址,最后绘制这两点之间的最短路径。
我使用本教程来实现应用程序,我现在有当前位置:http: //blog.objectgraph.com/index.php/2009/04/02/iphone-sdk-30-playing-with-map-kit/
我寻找路由,但我没有找到任何资源。请指导我如何绘制当前位置和目的地之间的最短路径。
谢谢!
你可以试试这个只适用于ios 6。
Class itemClass = [MKMapItem class];
if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *newLocation = [[CLLocation alloc]initWithLatitude:getLatitude
longitude:getLongitude];
[geocoder reverseGeocodeLocation:newLocation
completionHandler:^(NSArray *placemarks, NSError *error) {
MKPlacemark *placeMark = [[MKPlacemark alloc] initWithPlacemark:[placemarks objectAtIndex:0]];
MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:placeMark];
MKMapItem *mapItem2 = [MKMapItem mapItemForCurrentLocation];
NSArray *mapItems = @[mapItem, mapItem2];
NSDictionary *options = @{
MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsMapTypeKey:
[NSNumber numberWithInteger:MKMapTypeStandard],
MKLaunchOptionsShowsTrafficKey:@YES
};
[MKMapItem openMapsWithItems:mapItems launchOptions:options];
} ];
}
else
{
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to Get Your Location"delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
}
你谷歌地图套件和路线?
你会发现这个: http: //inlight.com.au/posts/mapkit,它解释了如何使用谷歌地图并解码响应以提取点以获取路线。
还有一个商业工具包: http: //mtdirectionsk.it/
Below ans work for all ios:
NSString *deviceVersion = [[UIDevice currentDevice] systemVersion];
NSLog(@"My Device version is :%@ ",deviceVersion);
//********* For ios 6 supporting devices *********
if ([deviceVersion isEqualToString:@"6.0"])
{
Class itemClass = [MKMapItem class];
if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *newLocation = [[CLLocation alloc]initWithLatitude:getLatitude
longitude:getLongitude];
[geocoder reverseGeocodeLocation:newLocation
completionHandler:^(NSArray *placemarks, NSError *error) {
MKPlacemark *placeMark = [[MKPlacemark alloc] initWithPlacemark:[placemarks objectAtIndex:0]];
MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:placeMark];
MKMapItem *mapItem2 = [MKMapItem mapItemForCurrentLocation];
NSArray *mapItems = @[mapItem, mapItem2];
NSDictionary *options = @{
MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsMapTypeKey:
[NSNumber numberWithInteger:MKMapTypeStandard],
MKLaunchOptionsShowsTrafficKey:@YES
};
[MKMapItem openMapsWithItems:mapItems launchOptions:options];
} ];
}
else
{
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to Get Your Location"delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
}
//********* For other ios supporting devices *********
else {
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = getLatitude;
region.center.longitude = getLongitude;
MKCoordinateRegion currentRegion = { {0.0, 0.0 }, { 0.0, 0.0 } };
currentRegion.center.latitude = currentLatitude;
currentRegion.center.longitude = currentLongitude;
region.span.longitudeDelta = 4.0f;
region.span.latitudeDelta = 4.0f;
currentRegion.span.longitudeDelta = 4.0f;
currentRegion.span.latitudeDelta = 4.0f;
CLLocationCoordinate2D start = { currentRegion.center.latitude, currentRegion.center.longitude };
CLLocationCoordinate2D destination = { region.center.latitude, region.center.longitude };
NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",start.latitude, start.longitude, destination.latitude, destination.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];
}
}
iOS MapKit 不提供应用程序即服务的路由信息。相反,您现在的选择是从第三方机制获取路线信息,或者自己构建路线(如果您没有基础街道数据,这基本上是不可能的)。
如果您需要在自己的应用程序中提供路线信息,您可能会将CloudMade视为替代地图系统,或者如果您想利用 Apple 的 Map 进行导航,则可以考虑仅调用 Maps 以路由到某个位置。
如果要调用 Map App 进行导航,则基本上需要打开一个带有源和目标位置的 URL。这是一个例子:
CLLocationManager *manager = [[[CLLocationManager alloc] init] autorelease];
CLLocationCoordinate2D currentLocation = [manager location].coordinate;
NSString *from = [NSString stringWithFormat: @"%f,%f",
currentLocation.latitude, currentLocation.longitude];
// used to be able to (3.2 iPad) "Current+Location", now we have to send: lat,long
NSString *encodedAddress = [address stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding];
NSString *url = [NSString stringWithFormat:
@"http://maps.google.com/maps?saddr=%@&daddr=%@", from, encodedAddress];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];