在 Xcode 中绘制用户位置和 Google MapKit 上指定位置之间的路线
嘿家伙, 在我的 iPhone 项目中,我使用 MapKit 构建了一个 Google 地图,并在地图上指定了我公司的位置。现在我想添加一个小功能,我的谷歌地图将绘制用户位置到我公司位置之间的路线。到目前为止,这是我用来指定我公司位置的代码
#import "GoogleMap.h"
#import "MyAnnotation.h"
@implementation GoogleMap
- (void)viewDidLoad {
[super viewDidLoad];
variable1 = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:@"NewYorkAreas"
ofType:@"plist"]];
double minLat = [[variable1 valueForKeyPath:@"@min.latitude"] doubleValue];
double maxLat = [[variable1 valueForKeyPath:@"@max.latitude"] doubleValue];
double minLon = [[variable1 valueForKeyPath:@"@min.longitude"] doubleValue];
double maxLon = [[variable1 valueForKeyPath:@"@max.longitude"] doubleValue];
MKCoordinateRegion region;
region.center.latitude = (maxLat + minLat) / 2.0;
region.center.longitude = (maxLon + minLon) / 2.0;
region.span.latitudeDelta = (maxLat - minLat) * 1.05;
region.span.longitudeDelta = (maxLon - minLon) * 1.05;
map.region = region;
for (NSDictionary *newYorkAreasDict in variable1){
MyAnnotation *annotation = [[MyAnnotation alloc] initWithDictionary:newYorkAreasDict];
[map addAnnotation:annotation];
[annotation release];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
if (map.userLocation == annotation){
return nil;
}
NSString *identifier = @"MY_IDENTIFIER";
MKAnnotationView *annotationView = [map dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil){
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:identifier]
autorelease];
annotationView.image = [UIImage imageNamed:@"logo.png"];
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
return annotationView;
}
所以现在,我想实现用户位置并绘制用户位置和我公司之间的路线,但我不知道如何编码,所以我希望有人可以帮助我,谢谢!
Hey guy,
On my iPhone project, I built a Google Map using MapKit, and specified a location of my company on the map. Now I want to add a little feature where my google map will plot a route between a user's location to my company's location. So far this is the code I used to specify the location of my company
#import "GoogleMap.h"
#import "MyAnnotation.h"
@implementation GoogleMap
- (void)viewDidLoad {
[super viewDidLoad];
variable1 = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:@"NewYorkAreas"
ofType:@"plist"]];
double minLat = [[variable1 valueForKeyPath:@"@min.latitude"] doubleValue];
double maxLat = [[variable1 valueForKeyPath:@"@max.latitude"] doubleValue];
double minLon = [[variable1 valueForKeyPath:@"@min.longitude"] doubleValue];
double maxLon = [[variable1 valueForKeyPath:@"@max.longitude"] doubleValue];
MKCoordinateRegion region;
region.center.latitude = (maxLat + minLat) / 2.0;
region.center.longitude = (maxLon + minLon) / 2.0;
region.span.latitudeDelta = (maxLat - minLat) * 1.05;
region.span.longitudeDelta = (maxLon - minLon) * 1.05;
map.region = region;
for (NSDictionary *newYorkAreasDict in variable1){
MyAnnotation *annotation = [[MyAnnotation alloc] initWithDictionary:newYorkAreasDict];
[map addAnnotation:annotation];
[annotation release];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
if (map.userLocation == annotation){
return nil;
}
NSString *identifier = @"MY_IDENTIFIER";
MKAnnotationView *annotationView = [map dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil){
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:identifier]
autorelease];
annotationView.image = [UIImage imageNamed:@"logo.png"];
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
return annotationView;
}
So now, I want to implement the user's location and plot a route between the user's location and my company, but I have no clue how to code it, so I hope someone can help me out, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是您需要获取路线的 GPS 坐标,然后您可以使用 MKOverlayView。
查看此了解更多信息
One way to do it would be you would need to get the GPS coordinates for the route, then you could use an MKOverlayView.
check this out for more info