Google Maps iOS SDK,获取两个位置之间的路线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20905797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Maps iOS SDK, Getting Directions between 2 locations
提问by Amit Singh
While I am using Google Maps SDK, I am trying to get driving direction between two locations on iOS. I know we can do this using two methods:-
当我使用 Google Maps SDK 时,我正在尝试获取 iOS 上两个位置之间的行车路线。我知道我们可以使用两种方法来做到这一点:-
1.) Using URL Scheme, for which it is necessary that Google Maps App is installed on your device.
1.) 使用 URL Scheme,为此需要在您的设备上安装 Google Maps App。
2.) Using Directions API, via Request-Response and then parsing the JSON. Displaying markers to show the direction.
2.) 使用 Directions API,通过请求-响应,然后解析 JSON。显示标记以显示方向。
Now, my question is there any other way by which I can do this on iOS? I need to show the direction from my current location to a particular location of which i have the Lat/Long.
现在,我的问题是还有其他方法可以在 iOS 上执行此操作吗?我需要显示从我当前位置到我有纬度/经度的特定位置的方向。
I mean is it really not possible to simply pass 2 location as parameter and Google Maps SDK, will give me the directions?
我的意思是真的不可能简单地将 2 个位置作为参数和 Google Maps SDK 传递,会给我指示吗?
Thanks,
谢谢,
采纳答案by Brett
It sounds like you are looking for UI Chrome like the Google Maps app has for showing directions. Google Maps SDK for iOS will paint you a map, but you are responsible for the additional navigation chrome.
听起来您正在寻找像 Google Maps 应用程序那样显示路线的 UI Chrome。适用于 iOS 的 Google Maps SDK 将为您绘制地图,但您需要负责额外的导航镶边。
You can use the Google Directions APIto request directions, and then use the encoded path returned from the service to draw a GMSPolylineusing GMSPath's pathFromEncodedPath:method.
您可以使用Google Directions API请求路线,然后使用从服务返回的编码路径使用GMSPath 的 pathFromEncodedPath:方法绘制GMSPolyline。
回答by Muhammad Noman
NSString *urlString = [NSString stringWithFormat:
@"%@?origin=%f,%f&destination=%f,%f&sensor=true&key=%@",
@"https://maps.googleapis.com/maps/api/directions/json",
mapView.myLocation.coordinate.latitude,
mapView.myLocation.coordinate.longitude,
destLatitude,
destLongitude,
@"Your Google Api Key String"];
NSURL *directionsURL = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:directionsURL];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog(@"%@",response);
NSDictionary *json =[NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingMutableContainers error:&error];
GMSPath *path =[GMSPath pathFromEncodedPath:json[@"routes"][0][@"overview_polyline"][@"points"]];
GMSPolyline *singleLine = [GMSPolyline polylineWithPath:path];
singleLine.strokeWidth = 7;
singleLine.strokeColor = [UIColor greenColor];
singleLine.map = self.mapView;
}
else NSLog(@"%@",[request error]);
Note: make Sure Your Google Direction API Sdk Is Enable in Your google developer Console.
注意:确保您的 Google Direction API Sdk 在您的 google 开发者控制台中启用。
回答by Engnyl
These lines shows location between a given latitude / longitude and user location;
这些线显示给定纬度/经度和用户位置之间的位置;
NSString *googleMapUrlString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%f,%f&daddr=%@,%@", mapView.userLocation.coordinate.latitude, mapView.userLocation.coordinate.longitude, destinationLatitude, destinationLongtitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapUrlString]];
回答by Sourabh Sharma
Swift 3.0 & XCode 8.0Using AFNetworking & SwiftJson
Swift 3.0 & XCode 8.0使用 AFNetworking & SwiftJson
let destLatitude="26.9124"
let destLongitude="75.7873"
mapView.isMyLocationEnabled = true
var urlString = "\("https://maps.googleapis.com/maps/api/directions/json")?origin=\("28.7041"),\("77.1025")&destination=\(destLatitude),\(destLongitude)&sensor=true&key=\("Your-Api-key")"
urlString = urlString.addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)!
let manager=AFHTTPRequestOperationManager()
manager.responseSerializer = AFJSONResponseSerializer(readingOptions: JSONSerialization.ReadingOptions.allowFragments) as AFJSONResponseSerializer
manager.requestSerializer = AFJSONRequestSerializer() as AFJSONRequestSerializer
manager.responseSerializer.acceptableContentTypes = NSSet(objects:"application/json", "text/html", "text/plain", "text/json", "text/javascript", "audio/wav") as Set<NSObject>
manager.post(urlString, parameters: nil, constructingBodyWith: { (formdata:AFMultipartFormData!) -> Void in
}, success: { operation, response -> Void in
//{"responseString" : "Success","result" : {"userId" : "4"},"errorCode" : 1}
//if(response != nil){
let parsedData = JSON(response)
print_debug("parsedData : \(parsedData)")
var path = GMSPath.init(fromEncodedPath: parsedData["routes"][0]["overview_polyline"]["points"].string!)
//GMSPath.fromEncodedPath(parsedData["routes"][0]["overview_polyline"]["points"].string!)
var singleLine = GMSPolyline.init(path: path)
singleLine.strokeWidth = 7
singleLine.strokeColor = UIColor.green
singleLine.map = self.mapView
//let loginResponeObj=LoginRespone.init(fromJson: parsedData)
// }
}, failure: { operation, error -> Void in
print_debug(error)
let errorDict = NSMutableDictionary()
errorDict.setObject(ErrorCodes.errorCodeFailed.rawValue, forKey: ServiceKeys.keyErrorCode.rawValue as NSCopying)
errorDict.setObject(ErrorMessages.errorTryAgain.rawValue, forKey: ServiceKeys.keyErrorMessage.rawValue as NSCopying)
})
回答by Patel Jigar
I had done it as it also shows PINS DISTANCE AND DURATIONon map with DIRECTION ROUTE. But dont forget to set your GOOGLE DIRECTION API TO ENABLEDin your GOOGLE DEVELOPER CONSOLE
我已经这样做了,因为它还在地图上用DIRECTION ROUTE显示引脚距离和持续时间。但不要忘记在您的GOOGLE 开发者控制台中将您的GOOGLE DIRECTION API设置为启用
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
NSString *urlString =@"https://maps.googleapis.com/maps/api/directions/json";
NSDictionary *dictParameters = @{@"origin" : [NSString stringWithFormat:@"%@",_sourceAdd], @"destination" : [NSString stringWithFormat:@"%@",_destinationAdd], @"mode" : @"driving", @"key":@"AIzaSyD9cWTQkAxemELVXTNUCALOmzlDv5b9Dhg"};
[manager GET:urlString parameters:dictParameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
GMSPath *path =[GMSPath pathFromEncodedPath:responseObject[@"routes"][0][@"overview_polyline"][@"points"]];
NSDictionary *arr=responseObject[@"routes"][0][@"legs"];
NSMutableArray *loc=[[NSMutableArray alloc]init];
loc=[[arr valueForKey:@"start_location"]valueForKey:@"lat"];
_sourceloc.latitude=[loc[0] doubleValue];
loc=[[arr valueForKey:@"start_location"]valueForKey:@"lng"];
_sourceloc.longitude=[loc[0] doubleValue];
loc=[[arr valueForKey:@"end_location"]valueForKey:@"lat"];
_destinationloc.latitude=[loc[0] doubleValue];
loc=[[arr valueForKey:@"end_location"]valueForKey:@"lng"];
_destinationloc.longitude=[loc[0] doubleValue];
NSString *dis,*dur;
loc=[[arr valueForKey:@"distance"]valueForKey:@"text"];
dis=loc[0];
loc=[[arr valueForKey:@"duration"]valueForKey:@"text"];
dur=loc[0];
NSString *sa,*da;
loc=[arr valueForKey:@"start_address"];
sa=loc[0];
loc=[arr valueForKey:@"end_address"];
da=loc[0];
UIAlertView *av=[[UIAlertView alloc]initWithTitle:@"Route Info" message:[NSString stringWithFormat:@"Distance:%@ \nDuration:%@",dis,dur] delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
[av show];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:_sourceloc.latitude longitude:_sourceloc.longitude zoom:10];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
GMSMarker *marker = [GMSMarker markerWithPosition:_sourceloc];
marker.title=@"Source";
marker.snippet =sa;
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.map = mapView;
GMSMarker *marker2 = [GMSMarker markerWithPosition:_destinationloc];
marker2.title=@"Destination";
marker2.snippet =da;
marker2.appearAnimation = kGMSMarkerAnimationPop;
marker2.map = mapView;
GMSPolyline *singleLine = [GMSPolyline polylineWithPath:path];
singleLine.strokeWidth = 4;
singleLine.strokeColor = [UIColor blueColor];
singleLine.map = mapView;
self.view = mapView;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
回答by iOS
Swift 4.1, Xcode 9.4.1
斯威夫特 4.1,Xcode 9.4.1
//Here you need to set your origin and destination points and mode
let url = NSURL(string: "https://maps.googleapis.com/maps/api/directions/json?origin=Machilipatnam&destination=Vijayawada&mode=driving")
//OR if you want to use latitude and longitude for source and destination
//let url = NSURL(string: "\("https://maps.googleapis.com/maps/api/directions/json")?origin=\("17.521100"),\("78.452854")&destination=\("15.1393932"),\("76.9214428")")
let task = URLSession.shared.dataTask(with: url! as URL) { (data, response, error) -> Void in
do {
if data != nil {
let dic = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableLeaves) as! [String:AnyObject]
// print(dic)
let status = dic["status"] as! String
var routesArray:String!
if status == "OK" {
routesArray = (((dic["routes"]!as! [Any])[0] as! [String:Any])["overview_polyline"] as! [String:Any])["points"] as! String
// print("routesArray: \(String(describing: routesArray))")
}
DispatchQueue.main.async {
let path = GMSPath.init(fromEncodedPath: routesArray!)
let singleLine = GMSPolyline.init(path: path)
singleLine.strokeWidth = 6.0
singleLine.strokeColor = .blue
singleLine.map = mapView
}
}
} catch {
print("Error")
}
}
task.resume()
Here, you need to add your key (google api key)to the above API.
在这里,您需要将您的密钥(google api 密钥)添加到上述 API 中。
回答by Luca Davanzo
Using SwiftI definitely solved in this way.
My purpose was finding distance between two coordinates:
使用Swift我肯定是这样解决的。
我的目的是找到两个坐标之间的距离:
import AFNetworking
/**
Calculate distance between two valid coordinates
- parameter origin: origin coordinates
- parameter destination: destination coordinates
- parameter completion: completion callback
*/
func calculateDistance(origin origin: CLLocation, destination: CLLocation, completion: (distance: Double?) -> Void) {
let service = "https://maps.googleapis.com/maps/api/directions/json"
let originLat = origin.coordinate.latitude
let originLong = origin.coordinate.longitude
let destLat = destination.coordinate.latitude
let destLong = destination.coordinate.longitude
let urlString = "\(service)?origin=\(originLat),\(originLong)&destination=\(destLat),\(destLong)&mode=driving&units=metric&sensor=true&key=<YOUR_KEY>"
let directionsURL = NSURL(string: urlString)
let request = NSMutableURLRequest(URL: directionsURL!)
request.HTTPMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let operation = AFHTTPRequestOperation(request: request)
operation.responseSerializer = AFJSONResponseSerializer()
operation.setCompletionBlockWithSuccess({ (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in
if let result = responseObject as? NSDictionary {
if let routes = result["routes"] as? [NSDictionary] {
if let lines = routes[0]["overview_polyline"] as? NSDictionary {
if let points = lines["points"] as? String {
let path = GMSPath(fromEncodedPath: points)
let distance = GMSGeometryLength(path)
print("wow \(distance / 1000) KM")
}
}
}
}
}) { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
print("\(error)")
}
operation.start()
}
回答by Navneet Singh Rana
(void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:30.692408
longitude:76.767556
zoom:14];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView.myLocationEnabled = YES;
// Creates markers in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(30.6936659, 76.77201819999999);
marker.title = @"Chandigarh 47c";
marker.snippet = @"Hello World";
marker.map = mapView;
GMSMarker *marker1 = [[GMSMarker alloc] init];
marker1.position = CLLocationCoordinate2DMake(30.742138, 76.818756);
marker1.title = @"Sukhna Lake";
marker1.map = mapView;
//creating a path
GMSMutablePath *path = [GMSMutablePath path];
[path addCoordinate:CLLocationCoordinate2DMake(@(30.6936659).doubleValue,@(76.77201819999999).doubleValue)];
[path addCoordinate:CLLocationCoordinate2DMake(@(30.742138).doubleValue,@(76.818756).doubleValue)];
GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path];
rectangle.strokeWidth = 2.f;
rectangle.map = mapView;
self.view=mapView;
}
回答by Manoj Reddy
Create a key in google developer console make sure your project is created with App bundleID after that add the following code
在谷歌开发者控制台中创建一个密钥,确保您的项目是使用 App bundleID 创建的,然后添加以下代码
NSString *KEY=@"";
NSString *Origin=@"";
NSString *Destination=@"";
NSString *str_maps=[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&key=%@",Origin,Destination,KEY];
NSURL *url=[NSURL URLWithString:str_maps];
NSData *dta=[NSData dataWithContentsOfURL:url];
NSDictionary *dict=(NSDictionary *)[NSJSONSerialization JSONObjectWithData:dta options:kNilOptions error:nil];
NSLog(@"%@",dict);
回答by Jabbar
If someone is looking to parse the distance from routes array following is the way to get the distance in swift 4/5
如果有人想解析与路由数组的距离,那么以下是在 swift 4/5 中获取距离的方法
let distance = responseJSON["routes"][0]["legs"][0]["distance"]["text"]