iOS:如何获取两个坐标之间的路线路径
Posted
技术标签:
【中文标题】iOS:如何获取两个坐标之间的路线路径【英文标题】:iOS: How to get route path between two coordinates 【发布时间】:2016-06-24 07:26:00 【问题描述】:在我的项目中,我必须借助纬度和经度找出两个位置之间的路线路径。
我正在使用以下代码
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[mapvw setDelegate:self];
[self mapp];
-(void)mapp
CLLocationCoordinate2D coordinateArray[2];
coordinateArray[0] = CLLocationCoordinate2DMake(28.6129, 77.2295);
coordinateArray[1] = CLLocationCoordinate2DMake(28.6562, 77.2410);
self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2];
[mapvw setVisibleMapRect:[self.routeLine boundingMapRect]]; //If you want the route to be visible
[mapvw addOverlay:self.routeLine];
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
if(overlay == self.routeLine)
if(nil == self.routeLineView)
self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
self.routeLineView.fillColor = [UIColor redColor];
self.routeLineView.strokeColor = [UIColor redColor];
self.routeLineView.lineWidth = 3;
return self.routeLineView;
return nil;
通过这段代码,我在模拟器上得到这样的输出
但我想要显示道路方向而不是直线方向的路线图。 请帮我解决这个问题,我在过去 2 天遇到了麻烦 ....
【问题讨论】:
您使用的是谷歌地图还是苹果地图(MapKit - 默认)? 我正在使用苹果地图 但如果您是 Google Map 用户,那么在两个位置之间创建路线非常容易。我还没有实现苹果地图。 对于苹果地图,请查看以下答案。 【参考方案1】:要在两个纬度之间绘制路径,您可以使用以下代码。
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface ViewController : UIViewController<MKMapViewDelegate>
@property (strong, nonatomic) IBOutlet MKMapView *myMapView;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) MKPlacemark *destination;
@property (strong,nonatomic) MKPlacemark *source;
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view.
[self getDirections];
-(void)getDirections
CLLocationCoordinate2D sourceCoords = CLLocationCoordinate2DMake(37.773972, -122.431297);
MKCoordinateRegion region;
//Set Zoom level using Span
MKCoordinateSpan span;
region.center = sourceCoords;
span.latitudeDelta = 1;
span.longitudeDelta = 1;
region.span=span;
[_myMapView setRegion:region animated:TRUE];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:sourceCoords addressDictionary:nil];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = sourceCoords;
annotation.title = @"San Francisco";
[self.myMapView addAnnotation:annotation];
//[self.myMapView addAnnotation:placemark];
_destination = placemark;
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:_destination];
CLLocationCoordinate2D destCoords = CLLocationCoordinate2DMake(37.615223, -122.389977);
MKPlacemark *placemark1 = [[MKPlacemark alloc] initWithCoordinate:destCoords addressDictionary:nil];
MKPointAnnotation *annotation1 = [[MKPointAnnotation alloc] init];
annotation1.coordinate = destCoords;
annotation1.title = @"San Francisco University";
[self.myMapView addAnnotation:annotation1];
//[self.myMapView addAnnotation:placemark1];
_source = placemark1;
MKMapItem *mapItem1 = [[MKMapItem alloc] initWithPlacemark:_source];
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
request.source = mapItem1;
request.destination = mapItem;
request.requestsAlternateRoutes = NO;
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
[directions calculateDirectionsWithCompletionHandler:
^(MKDirectionsResponse *response, NSError *error)
if (error)
NSLog(@"ERROR");
NSLog(@"%@",[error localizedDescription]);
else
[self showRoute:response];
];
-(void)showRoute:(MKDirectionsResponse *)response
for (MKRoute *route in response.routes)
[_myMapView
addOverlay:route.polyline level:MKOverlayLevelAboveRoads];
for (MKRouteStep *step in route.steps)
NSLog(@"%@", step.instructions);
#pragma mark - MKMapViewDelegate methods
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
renderer.strokeColor = [UIColor colorWithRed:0.0/255.0 green:171.0/255.0 blue:253.0/255.0 alpha:1.0];
renderer.lineWidth = 10.0;
return renderer;
更新:Swift 4
import MapKit
import UIKit
class ViewController: UIViewController, MKMapViewDelegate
var destination: MKPlacemark?
var source: MKPlacemark?
@IBOutlet var myMapView: MKMapView!
func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
self.myMapView.delegate = self
getDirections()
func getDirections()
let sourceCoords: CLLocationCoordinate2D = CLLocationCoordinate2DMake(37.773972, -122.431297)
let region: MKCoordinateRegion
//Set Zoom level using Span
let span: MKCoordinateSpan
region.center = sourceCoords
span.latitudeDelta = CLLocationDegrees(1)
span.longitudeDelta = CLLocationDegrees(1)
region.span = span
myMapView.setRegion(region, animated: true)
let placemark = MKPlacemark(coordinate: sourceCoords, addressDictionary: nil)
let annotation = MKPointAnnotation()
annotation.coordinate = sourceCoords
annotation.title = "San Francisco"
myMapView.addAnnotation(annotation)
//[self.myMapView addAnnotation:placemark];
destination = placemark
var mapItem: MKMapItem? = nil
if let aDestination = destination
mapItem = MKMapItem(placemark: aDestination)
let destCoords: CLLocationCoordinate2D = CLLocationCoordinate2DMake(37.615223, -122.389977)
let placemark1 = MKPlacemark(coordinate: destCoords, addressDictionary: nil)
let annotation1 = MKPointAnnotation()
annotation1.coordinate = destCoords
annotation1.title = "San Francisco University"
myMapView.addAnnotation(annotation1)
//[self.myMapView addAnnotation:placemark1];
source = placemark1
var mapItem1: MKMapItem? = nil
if let aSource = source
mapItem1 = MKMapItem(placemark: aSource)
let request = MKDirectionsRequest()
request.source = mapItem1
request.destination = mapItem
request.requestsAlternateRoutes = false
let directions = MKDirections(request: request)
directions.calculate(completionHandler: (_ response: MKDirectionsResponse, _ error: Error?) -> Void in
if error != nil
print("ERROR")
print("\(error?.localizedDescription ?? "")")
else
self.showRoute(response)
)
func showRoute(_ response: MKDirectionsResponse?)
for route: MKRoute in response?.routes
myMapView.add(route.polyline, level: .aboveRoads)
for step: MKRouteStep in route.steps
print("\(step.instructions)")
// MARK: - MKMapViewDelegate methods
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer
var renderer: MKPolylineRenderer? = nil
if let anOverlay = overlay as? MKPolyline
renderer = MKPolylineRenderer(polyline: anOverlay)
renderer?.strokeColor = UIColor(red: 0.0 / 255.0, green: 171.0 / 255.0, blue: 253.0 / 255.0, alpha: 1.0)
renderer?.lineWidth = 10.0
if let aRenderer = renderer
return aRenderer
return MKOverlayRenderer()
希望对您有所帮助!根据您的要求更改上述代码中的 lat long。
【讨论】:
显示错误:No known class method for selector 'addOverlay:level:' 当我使用我的纬度和经度时,它没有显示路线。它唯一显示您的纬度和经度的路线 Apple 地图在某些国家/地区受限。所以您必须从这些国家/地区输入 lat long。 好的,谢谢您的信息。如果我还想在地图中显示导航怎么办 导航,不知道。但我想从上面的代码中你会得到那条路线的所有点,通过使用它你可以显示导航。以上是关于iOS:如何获取两个坐标之间的路线路径的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Mapbox Kotlin 计算两个坐标之间的距离
如何在从 ListView 项获取的两个地理点之间绘制标记和路线?