gpt4 book ai didi

iphone - 对于 MapKit,我应该使用 Directions 还是制作我自己的路线叠加层?

转载 作者:行者123 更新时间:2023-11-28 08:51:36 25 4
gpt4 key购买 nike

我绝对是任何编程的初学者,但我正在尝试通过使用 MapKit 制作一个在 map 上显示(我的!)路线的 iOS 应用程序来学习。沿途有带有特定信息的注释。

我有两个问题。

  1. 更好/更容易的是,尝试在注释之间使用 MapKit 方向,以便 MapKit 显示路线(我想要)沿着道路(我必须在每个注释之间获取方向,否则路线不会总是我想展示的那个!)或者以某种方式制作我的路线的叠加 png 图像并使用它?

  2. 如果事实证明使用叠加层更好,我该如何制作叠加层并确保它位于正确的位置并放大和缩小?

我找到了很多关于如何添加叠加层的教程,但似乎找不到太多关于如何制作可在应用程序中使用的叠加层的教程。

最佳答案

在 map 上设置图像不是实现此功能的好方法。对于路径绘制,Apple 提供了 MKPolyline。这将解决您在缩放因子方面保持其位置的所有顾虑。

请查看下面的折线图

iPhone: How to draw line between two points on MapKit?

http://pinkstone.co.uk/how-to-draw-an-mkpolyline-on-a-map-view/

最终您的目标是,通过内置方向 API 或第三方 API(如 Google Direction API)以某种方式获得中间位置的经纬度。

枚举这些位置并在 map 上绘制多段线。如果您将来尝试使用 Google map ,将会出现相同的逻辑和情况。只是方法和类将被改变。

- (void)drawLine {

// remove polyline if one exists
[self.mapView removeOverlay:self.polyline];

// create an array of coordinates from allPins
CLLocationCoordinate2D coordinates[self.allPins.count];
int i = 0;
for (Pin *currentPin in self.allPins) {
coordinates[i] = currentPin.coordinate;
i++;
}

// create a polyline with all cooridnates
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coordinates count:self.allPins.count];
[self.mapView addOverlay:polyline];
self.polyline = polyline;

// create an MKPolylineView and add it to the map view
self.lineView = [[MKPolylineView alloc]initWithPolyline:self.polyline];
self.lineView.strokeColor = [UIColor redColor];
self.lineView.lineWidth = 5;

}

编辑

如果您想使用 GOOGLE API 和 GOOGLE MAP 绘图

-(void)drawPathFrom:(CLLocation*)source toDestination:(CLLocation*)destination{    

NSString *baseUrl = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=true", source.coordinate.latitude, source.coordinate.longitude, destination.coordinate.latitude, destination.coordinate.longitude];

NSURL *url = [NSURL URLWithString:[baseUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"Url: %@", url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if(!connectionError){
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray *routes = [result objectForKey:@"routes"];
if([routes isKindOfClass:[NSArray class]] && routes.count>0){
NSDictionary *firstRoute = [routes objectAtIndex:0];
NSString *encodedPath = [firstRoute[@"overview_polyline"] objectForKey:@"points"];

GMSPolyline *polyPath = [GMSPolyline polylineWithPath:[GMSPath pathFromEncodedPath:encodedPath]];
polyPath.strokeColor = [UIColor blueColor];
polyPath.strokeWidth = 3.5f;
polyPath.map = _mapView;

}
}
}];

}

关于iphone - 对于 MapKit,我应该使用 Directions 还是制作我自己的路线叠加层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34063964/

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