این راهنما را دنبال کنید تا برنامه خود را برای بازیابی زمانها، مسافتها و مسیرهای مسیر فعلی تنظیم کنید.
نمای کلی
برای دریافت اطلاعات در مورد مسیر فعلی، ویژگی مناسب را از نمونه navigator
دریافت کنید:
-
GMSNavigator.timeToNextDestination
برای دریافت زمان پیش بینی شده به مقصد بعدی در مسیر فعلی، در چند ثانیه. -
GMSNavigator.distanceToNextDestination
برای دریافت مسافت تا مقصد بعدی در مسیر فعلی، بر حسب متر. -
GMSNavigationDelayCategory
برای دریافت دسته تاخیر برای جریان ترافیک. -
GMSNavigator.currentRouteLeg
برای دریافت اطلاعات در مورد مسیر فعلی. -
GMSNavigator.traveledPath
برای دریافت آخرین مسیر طی شده.
کد را ببینید
نمایش/پنهان کردن کد سوئیفت برای دریافت اطلاعات مسیر.
/* * Copyright 2017 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import GoogleNavigation import UIKit class ViewController: UIViewController { var mapView: GMSMapView! var locationManager: CLLocationManager! override func loadView() { locationManager = CLLocationManager() let camera = GMSCameraPosition.camera(withLatitude: 47.67, longitude: -122.20, zoom: 14) mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera) // Show the terms and conditions. let companyName = "Ride Sharing Co." GMSNavigationServices.showTermsAndConditionsDialogIfNeeded( withCompanyName: companyName ) { termsAccepted in guard termsAccepted else { // Handle the case when the user rejects the terms and conditions. return } // Enable navigation after the user accepts the terms. self.mapView.isNavigationEnabled = true // Request authorization to use location services. self.locationManager.requestAlwaysAuthorization() } self.view = mapView makeButtons() } // Create a route and start guidance. func startNav() { var destinations = [GMSNavigationWaypoint]() destinations.append( GMSNavigationWaypoint.init( placeID: "ChIJnUYTpNASkFQR_gSty5kyoUk", title: "PCC Natural Market")!) destinations.append( GMSNavigationWaypoint.init( placeID: "ChIJJ326ROcSkFQRBfUzOL2DSbo", title: "Marina Park")!) mapView.navigator?.setDestinations(destinations) { routeStatus in guard routeStatus == .OK else { print("Handle route statuses that are not OK.") return } self.mapView.navigator?.isGuidanceActive = true self.mapView.cameraMode = .following self.mapView.locationSimulator?.simulateLocationsAlongExistingRoute() } mapView.roadSnappedLocationProvider?.startUpdatingLocation() } func getTimeToNextDestination() { if let navigator = mapView.navigator { let time = navigator.timeToNextDestination let minutes = floor(time / 60) let seconds = round(time - minutes * 60) print("Time to next destination: \(minutes):\(seconds)") } } func getDistanceToNextDestination() { if let navigator = mapView.navigator { let distance = navigator.distanceToNextDestination let miles = distance * 0.00062137 print("Distance to next destination: \(miles) miles.") } } func getCurrentRouteLeg() { if let navigator = mapView.navigator { let currentLeg = navigator.currentRouteLeg let nextDestination = currentLeg?.destinationWaypoint?.title ?? "Not Available" let lat = currentLeg?.destinationCoordinate.latitude.description ?? "Not Available" let lng = currentLeg?.destinationCoordinate.longitude.description ?? "Not Available" print("Destination: \(nextDestination) at (\(lat),\(lng)") } } func getTraveledPath() { if let navigator = mapView.navigator { let latestPath = navigator.traveledPath if latestPath.count() > 0 { let begin: CLLocationCoordinate2D = latestPath.coordinate(at: 0) let current: CLLocationCoordinate2D = latestPath.coordinate(at: latestPath.count() - 1) print( "Path from (\(begin.latitude),\(begin.longitude)) to (\(current.latitude),\(current.longitude))" ) } } } // Add some buttons to the view. func makeButtons() { // Start navigation. let navButton = UIButton(frame: CGRect(x: 5, y: 150, width: 200, height: 35)) navButton.backgroundColor = .blue navButton.alpha = 0.5 navButton.setTitle("Start navigation", for: .normal) navButton.addTarget(self, action: #selector(startNav), for: .touchUpInside) self.mapView.addSubview(navButton) // Get the time to the next destination. let timeButton = UIButton(frame: CGRect(x: 5, y: 190, width: 200, height: 35)) timeButton.backgroundColor = .blue timeButton.alpha = 0.5 timeButton.setTitle("Time to next destination", for: .normal) timeButton.addTarget(self, action: #selector(getTimeToNextDestination), for: .touchUpInside) self.mapView.addSubview(timeButton) // Get the distance to the next destination. let distanceButton = UIButton(frame: CGRect(x: 5, y: 230, width: 200, height: 35)) distanceButton.backgroundColor = .blue distanceButton.alpha = 0.5 distanceButton.setTitle("Distance to next destination", for: .normal) distanceButton.addTarget( self, action: #selector(getDistanceToNextDestination), for: .touchUpInside) self.mapView.addSubview(distanceButton) // Get the current route leg. let routeButton = UIButton(frame: CGRect(x: 5, y: 270, width: 200, height: 35)) routeButton.backgroundColor = .blue routeButton.alpha = 0.5 routeButton.setTitle("Get current route leg", for: .normal) routeButton.addTarget(self, action: #selector(getCurrentRouteLeg), for: .touchUpInside) self.mapView.addSubview(routeButton) // Get the most recently traveled path. let pathButton = UIButton(frame: CGRect(x: 5, y: 310, width: 200, height: 35)) pathButton.backgroundColor = .blue pathButton.alpha = 0.5 pathButton.setTitle("Get traveled path", for: .normal) pathButton.addTarget(self, action: #selector(getTraveledPath), for: .touchUpInside) self.mapView.addSubview(pathButton) } }
نمایش/پنهان کردن کد Objective-C برای دریافت اطلاعات مسیر.
/* * Copyright 2017 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #import "ViewController.h" @import GoogleNavigation; @interface ViewController () @end @implementation ViewController { GMSMapView *_mapView; CLLocationManager *_locationManager; } - (void)loadView { _locationManager = [[CLLocationManager alloc] init]; GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:47.67 longitude:-122.20 zoom:14]; _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; // Show the terms and conditions. NSString *companyName = @"Ride Sharing Co."; [GMSNavigationServices showTermsAndConditionsDialogIfNeededWithCompanyName:companyName callback:^(BOOL termsAccepted) { if (termsAccepted) { // Enable navigation if the user accepts the terms. _mapView.navigationEnabled = YES; // Request authorization to use location services. [_locationManager requestAlwaysAuthorization]; } else { // Handle the case when the user rejects the terms and conditions. } }]; self.view = _mapView; [self makeButtons]; } // Create a route and start guidance. - (void)startNav { NSArray<GMSNavigationWaypoint *> *destinations = @[[[GMSNavigationWaypoint alloc] initWithPlaceID:@"ChIJnUYTpNASkFQR_gSty5kyoUk" title:@"PCC Natural Market"], [[GMSNavigationWaypoint alloc] initWithPlaceID:@"ChIJJ326ROcSkFQRBfUzOL2DSbo" title:@"Marina Park"]]; [_mapView.navigator setDestinations:destinations callback:^(GMSRouteStatus routeStatus){ _mapView.navigator.guidanceActive = YES; _mapView.cameraMode = GMSNavigationCameraModeFollowing; [_mapView.locationSimulator simulateLocationsAlongExistingRoute]; }]; [_mapView.roadSnappedLocationProvider startUpdatingLocation]; } - (void)getTimeToNextDestination { NSTimeInterval time = _mapView.navigator.timeToNextDestination; int minutes = floor(time/60); int seconds = round(time - minutes * 60); NSLog(@"%@", [NSString stringWithFormat:@"Time to next destination: %i:%i.", minutes, seconds]); } - (void)getDistanceToNextDestination { CLLocationDistance distance = _mapView.navigator.distanceToNextDestination; double miles = distance * 0.00062137; NSLog(@"%@", [NSString stringWithFormat:@"Distance to next destination: %.2f.", miles]); } - (void)getCurrentRouteLeg { GMSRouteLeg *currentSegment = _mapView.navigator.currentRouteLeg; NSString *nextDestination = currentSegment.destinationWaypoint.title; CLLocationDegrees lat = currentSegment.destinationCoordinate.latitude; CLLocationDegrees lng = currentSegment.destinationCoordinate.longitude; NSLog(@"%@", [NSString stringWithFormat:@"%@, %f/%f", nextDestination, lat, lng]); } - (void)getTraveledPath { GMSPath *latestPath = _mapView.navigator.traveledPath; if (latestPath.count > 0) { CLLocationCoordinate2D begin = [latestPath coordinateAtIndex:0]; CLLocationCoordinate2D current = [latestPath coordinateAtIndex:latestPath.count - 1]; NSLog(@"Path from %f/%f to %f/%f", begin.latitude, begin.longitude, current.latitude, current.longitude); } } #pragma mark - Buttons and things // Add some buttons to the view. - (void)makeButtons { // Start navigation. UIButton *navButton = [UIButton buttonWithType:UIButtonTypeCustom]; [navButton addTarget:self action:@selector(startNav) forControlEvents:UIControlEventTouchUpInside]; [navButton setTitle:@"Start navigation" forState:UIControlStateNormal]; [navButton setBackgroundColor:[UIColor blueColor]]; [navButton setAlpha:0.5]; navButton.frame = CGRectMake(5.0, 150.0, 200.0, 35.0); [_mapView addSubview:navButton]; // Get the time to the next destination. UIButton *timeButton = [UIButton buttonWithType:UIButtonTypeCustom]; [timeButton addTarget:self action:@selector(getTimeToNextDestination) forControlEvents:UIControlEventTouchUpInside]; [timeButton setTitle:@"Time to next destination" forState:UIControlStateNormal]; [timeButton setBackgroundColor:[UIColor blueColor]]; [timeButton setAlpha:0.5]; timeButton.frame = CGRectMake(5.0, 190.0, 200.0, 35.0); [_mapView addSubview:timeButton]; // Get the distance to the next destination. UIButton *distanceButton = [UIButton buttonWithType:UIButtonTypeCustom]; [distanceButton addTarget:self action:@selector(getDistanceToNextDestination) forControlEvents:UIControlEventTouchUpInside]; [distanceButton setTitle:@"Distance to next destination" forState:UIControlStateNormal]; [distanceButton setBackgroundColor:[UIColor blueColor]]; [distanceButton setAlpha:0.5]; distanceButton.frame = CGRectMake(5.0, 230.0, 200.0, 35.0); [_mapView addSubview:distanceButton]; // Get the current route leg. UIButton *routeButton = [UIButton buttonWithType:UIButtonTypeCustom]; [routeButton addTarget:self action:@selector(getCurrentRouteLeg) forControlEvents:UIControlEventTouchUpInside]; [routeButton setTitle:@"Get current route leg" forState:UIControlStateNormal]; [routeButton setBackgroundColor:[UIColor blueColor]]; [routeButton setAlpha:0.5]; routeButton.frame = CGRectMake(5.0, 270.0, 200.0, 35.0); [_mapView addSubview:routeButton]; // Get the most recently traveled path. UIButton *pathButton = [UIButton buttonWithType:UIButtonTypeCustom]; [pathButton addTarget:self action:@selector(getTraveledPath) forControlEvents:UIControlEventTouchUpInside]; [pathButton setTitle:@"Get current route leg" forState:UIControlStateNormal]; [pathButton setBackgroundColor:[UIColor blueColor]]; [pathButton setAlpha:0.5]; pathButton.frame = CGRectMake(5.0, 310.0, 200.0, 35.0); [_mapView addSubview:pathButton]; } @end
زمان رسیدن به مقصد بعدی
برای دریافت زمان به مقصد بعدی، timeToNextDestination()
را فراخوانی کنید. این مقدار NSTimeInterval
را برمی گرداند. مثال زیر ثبت زمان رسیدن به مقصد بعدی را نشان می دهد:
if let navigator = mapView.navigator {
let time = navigator.timeToNextDestination
let minutes = floor(time/60)
let seconds = round(time - minutes * 60)
NSLog("Time to next destination: %.0f:%.0f", minutes, seconds)
}
NSTimeInterval time = _mapView.navigator.timeToNextDestination;
int minutes = floor(time/60);
int seconds = round(time - minutes * 60);
NSLog(@"%@", [NSString stringWithFormat:@"Time to next destination: %i:%i.", minutes, seconds]);
فاصله گرفتن تا مقصد بعدی
برای دریافت فاصله تا مقصد بعدی، distanceToNextDestination()
را فراخوانی کنید. این یک مقدار CLLocationDistance
را برمی گرداند. واحدها بر حسب متر مشخص می شوند.
if let navigator = mapView.navigator {
let distance = navigator.distanceToNextDestination
let miles = distance * 0.00062137
NSLog("Distance to next destination: %.2f miles.", miles)
}
CLLocationDistance distance = _mapView.navigator.distanceToNextDestination;
double miles = distance * 0.00062137;
NSLog(@"%@", [NSString stringWithFormat:@"Distance to next destination: %.2f.", miles]);
رسیدن به شرایط ترافیکی به مقصد بعدی
برای دریافت مقداری که جریان ترافیک به مقصد بعدی را نشان میدهد، delayCategoryToNextDestination
را فراخوانی کنید. این پارامتر GMSNavigationDelayCategory
را برمی گرداند. مثال زیر ارزیابی نتیجه و ثبت یک پیام ترافیک را نشان می دهد:
if let navigator = mapView.navigator {
// insert sample for evaluating traffic value
let delay = navigator.delayCategoryToNextDestination
let traffic = "unavailable"
switch delay {
case .noData:
traffic = "unavailable"
case .heavy:
traffic = "heavy"
case .medium:
traffic = "moderate"
case .light:
traffic = "light"
default:
traffic = "unavailable"
}
print("Traffic is \(traffic).")
}
GMSNavigationDelayCategory delay = mapView.navigator.delayCategoryToNextDestination;
NSString *traffic = @"";
switch (delayCategory) {
case GMSNavigationDelayCategoryNoData:
traffic = @"No Data";
break;
case GMSNavigationDelayCategoryHeavy:
traffic = @"Heavy";
break;
case GMSNavigationDelayCategoryMedium:
traffic = @"Medium";
break;
case GMSNavigationDelayCategoryLight:
traffic = @"Light";
break;
default:
NSLog(@"Invalid delay category: %zd", delayCategory);
}
NSLog(@"%@", [NSString stringWithFormat:@"Traffic is %@.", traffic]);
دریافت اطلاعات در مورد پای فعلی
برای دریافت اطلاعات در مورد مسیر فعلی، با currentRouteLeg
تماس بگیرید. این یک نمونه GMSRouteLeg
را برمی گرداند که از آن می توانید دریافت کنید:
- مقصد برای پا.
- مختصات نهایی در ساق پا.
- مسیری که شامل مختصاتی است که پای مسیر را تشکیل می دهد.
مثال زیر ثبت مختصات عنوان و lat/lng را برای قسمت بعدی مسیر نشان می دهد:
if let navigator = mapView.navigator {
let currentLeg = navigator.currentRouteLeg
let nextDestination = currentLeg?.destinationWaypoint?.title
let lat = currentLeg?.destinationCoordinate.latitude.description
let lng = currentLeg?.destinationCoordinate.longitude.description
NSLog(nextDestination! + ", " + lat! + "/" + lng!)
}
GMSRouteLeg *currentSegment = _mapView.navigator.currentRouteLeg;
NSString *nextDestination = currentSegment.destinationWaypoint.title;
CLLocationDegrees lat = currentSegment.destinationCoordinate.latitude;
CLLocationDegrees lng = currentSegment.destinationCoordinate.longitude;
NSLog(@"%@", [NSString stringWithFormat:@"%@, %f/%f", nextDestination, lat, lng]);
دریافت آخرین مسیر طی شده
برای دریافت آخرین مسیر طی شده، با traveledPath
تماس بگیرید. این یک نمونه GMSPath
را برمی گرداند که برای حذف نقاط اضافی ساده شده است (به عنوان مثال تبدیل نقاط خطی متوالی به یک پاره خط منفرد). این مسیر تا شروع راهنمایی خالی است. مثال زیر دریافت آخرین مسیر طی شده را نشان می دهد:
if let navigator = mapView.navigator {
let latestPath = navigator.traveledPath
if latestPath.count() > 0 {
let begin: CLLocationCoordinate2D = latestPath.coordinate(at: 0)
let current: CLLocationCoordinate2D = latestPath.coordinate(at: latestPath.count() - 1)
print("Path from (\(begin.latitude),\(begin.longitude)) to (\(current.latitude),\(current.longitude))")
}
}
GMSPath *latestPath = mapView.navigator.traveledPath;
if (latestPath.count > 0) {
CLLocationCoordinate2D begin = [latestPath coordinateAtIndex:0];
CLLocationCoordinate2D current = [latestPath coordinateAtIndex:latestPath.count - 1];
NSLog(@"Path from %f/%f to %f/%f",
begin.latitude,
begin.longitude,
current.latitude,
current.longitude);
}
این راهنما را دنبال کنید تا برنامه خود را برای بازیابی زمانها، مسافتها و مسیرهای مسیر فعلی تنظیم کنید.
نمای کلی
برای دریافت اطلاعات در مورد مسیر فعلی، ویژگی مناسب را از نمونه navigator
دریافت کنید:
-
GMSNavigator.timeToNextDestination
برای دریافت زمان پیش بینی شده به مقصد بعدی در مسیر فعلی، در چند ثانیه. -
GMSNavigator.distanceToNextDestination
برای دریافت مسافت تا مقصد بعدی در مسیر فعلی، بر حسب متر. -
GMSNavigationDelayCategory
برای دریافت دسته تاخیر برای جریان ترافیک. -
GMSNavigator.currentRouteLeg
برای دریافت اطلاعات در مورد مسیر فعلی. -
GMSNavigator.traveledPath
برای دریافت آخرین مسیر طی شده.
کد را ببینید
نمایش/پنهان کردن کد سوئیفت برای دریافت اطلاعات مسیر.
/* * Copyright 2017 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import GoogleNavigation import UIKit class ViewController: UIViewController { var mapView: GMSMapView! var locationManager: CLLocationManager! override func loadView() { locationManager = CLLocationManager() let camera = GMSCameraPosition.camera(withLatitude: 47.67, longitude: -122.20, zoom: 14) mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera) // Show the terms and conditions. let companyName = "Ride Sharing Co." GMSNavigationServices.showTermsAndConditionsDialogIfNeeded( withCompanyName: companyName ) { termsAccepted in guard termsAccepted else { // Handle the case when the user rejects the terms and conditions. return } // Enable navigation after the user accepts the terms. self.mapView.isNavigationEnabled = true // Request authorization to use location services. self.locationManager.requestAlwaysAuthorization() } self.view = mapView makeButtons() } // Create a route and start guidance. func startNav() { var destinations = [GMSNavigationWaypoint]() destinations.append( GMSNavigationWaypoint.init( placeID: "ChIJnUYTpNASkFQR_gSty5kyoUk", title: "PCC Natural Market")!) destinations.append( GMSNavigationWaypoint.init( placeID: "ChIJJ326ROcSkFQRBfUzOL2DSbo", title: "Marina Park")!) mapView.navigator?.setDestinations(destinations) { routeStatus in guard routeStatus == .OK else { print("Handle route statuses that are not OK.") return } self.mapView.navigator?.isGuidanceActive = true self.mapView.cameraMode = .following self.mapView.locationSimulator?.simulateLocationsAlongExistingRoute() } mapView.roadSnappedLocationProvider?.startUpdatingLocation() } func getTimeToNextDestination() { if let navigator = mapView.navigator { let time = navigator.timeToNextDestination let minutes = floor(time / 60) let seconds = round(time - minutes * 60) print("Time to next destination: \(minutes):\(seconds)") } } func getDistanceToNextDestination() { if let navigator = mapView.navigator { let distance = navigator.distanceToNextDestination let miles = distance * 0.00062137 print("Distance to next destination: \(miles) miles.") } } func getCurrentRouteLeg() { if let navigator = mapView.navigator { let currentLeg = navigator.currentRouteLeg let nextDestination = currentLeg?.destinationWaypoint?.title ?? "Not Available" let lat = currentLeg?.destinationCoordinate.latitude.description ?? "Not Available" let lng = currentLeg?.destinationCoordinate.longitude.description ?? "Not Available" print("Destination: \(nextDestination) at (\(lat),\(lng)") } } func getTraveledPath() { if let navigator = mapView.navigator { let latestPath = navigator.traveledPath if latestPath.count() > 0 { let begin: CLLocationCoordinate2D = latestPath.coordinate(at: 0) let current: CLLocationCoordinate2D = latestPath.coordinate(at: latestPath.count() - 1) print( "Path from (\(begin.latitude),\(begin.longitude)) to (\(current.latitude),\(current.longitude))" ) } } } // Add some buttons to the view. func makeButtons() { // Start navigation. let navButton = UIButton(frame: CGRect(x: 5, y: 150, width: 200, height: 35)) navButton.backgroundColor = .blue navButton.alpha = 0.5 navButton.setTitle("Start navigation", for: .normal) navButton.addTarget(self, action: #selector(startNav), for: .touchUpInside) self.mapView.addSubview(navButton) // Get the time to the next destination. let timeButton = UIButton(frame: CGRect(x: 5, y: 190, width: 200, height: 35)) timeButton.backgroundColor = .blue timeButton.alpha = 0.5 timeButton.setTitle("Time to next destination", for: .normal) timeButton.addTarget(self, action: #selector(getTimeToNextDestination), for: .touchUpInside) self.mapView.addSubview(timeButton) // Get the distance to the next destination. let distanceButton = UIButton(frame: CGRect(x: 5, y: 230, width: 200, height: 35)) distanceButton.backgroundColor = .blue distanceButton.alpha = 0.5 distanceButton.setTitle("Distance to next destination", for: .normal) distanceButton.addTarget( self, action: #selector(getDistanceToNextDestination), for: .touchUpInside) self.mapView.addSubview(distanceButton) // Get the current route leg. let routeButton = UIButton(frame: CGRect(x: 5, y: 270, width: 200, height: 35)) routeButton.backgroundColor = .blue routeButton.alpha = 0.5 routeButton.setTitle("Get current route leg", for: .normal) routeButton.addTarget(self, action: #selector(getCurrentRouteLeg), for: .touchUpInside) self.mapView.addSubview(routeButton) // Get the most recently traveled path. let pathButton = UIButton(frame: CGRect(x: 5, y: 310, width: 200, height: 35)) pathButton.backgroundColor = .blue pathButton.alpha = 0.5 pathButton.setTitle("Get traveled path", for: .normal) pathButton.addTarget(self, action: #selector(getTraveledPath), for: .touchUpInside) self.mapView.addSubview(pathButton) } }
نمایش/پنهان کردن کد Objective-C برای دریافت اطلاعات مسیر.
/* * Copyright 2017 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #import "ViewController.h" @import GoogleNavigation; @interface ViewController () @end @implementation ViewController { GMSMapView *_mapView; CLLocationManager *_locationManager; } - (void)loadView { _locationManager = [[CLLocationManager alloc] init]; GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:47.67 longitude:-122.20 zoom:14]; _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; // Show the terms and conditions. NSString *companyName = @"Ride Sharing Co."; [GMSNavigationServices showTermsAndConditionsDialogIfNeededWithCompanyName:companyName callback:^(BOOL termsAccepted) { if (termsAccepted) { // Enable navigation if the user accepts the terms. _mapView.navigationEnabled = YES; // Request authorization to use location services. [_locationManager requestAlwaysAuthorization]; } else { // Handle the case when the user rejects the terms and conditions. } }]; self.view = _mapView; [self makeButtons]; } // Create a route and start guidance. - (void)startNav { NSArray<GMSNavigationWaypoint *> *destinations = @[[[GMSNavigationWaypoint alloc] initWithPlaceID:@"ChIJnUYTpNASkFQR_gSty5kyoUk" title:@"PCC Natural Market"], [[GMSNavigationWaypoint alloc] initWithPlaceID:@"ChIJJ326ROcSkFQRBfUzOL2DSbo" title:@"Marina Park"]]; [_mapView.navigator setDestinations:destinations callback:^(GMSRouteStatus routeStatus){ _mapView.navigator.guidanceActive = YES; _mapView.cameraMode = GMSNavigationCameraModeFollowing; [_mapView.locationSimulator simulateLocationsAlongExistingRoute]; }]; [_mapView.roadSnappedLocationProvider startUpdatingLocation]; } - (void)getTimeToNextDestination { NSTimeInterval time = _mapView.navigator.timeToNextDestination; int minutes = floor(time/60); int seconds = round(time - minutes * 60); NSLog(@"%@", [NSString stringWithFormat:@"Time to next destination: %i:%i.", minutes, seconds]); } - (void)getDistanceToNextDestination { CLLocationDistance distance = _mapView.navigator.distanceToNextDestination; double miles = distance * 0.00062137; NSLog(@"%@", [NSString stringWithFormat:@"Distance to next destination: %.2f.", miles]); } - (void)getCurrentRouteLeg { GMSRouteLeg *currentSegment = _mapView.navigator.currentRouteLeg; NSString *nextDestination = currentSegment.destinationWaypoint.title; CLLocationDegrees lat = currentSegment.destinationCoordinate.latitude; CLLocationDegrees lng = currentSegment.destinationCoordinate.longitude; NSLog(@"%@", [NSString stringWithFormat:@"%@, %f/%f", nextDestination, lat, lng]); } - (void)getTraveledPath { GMSPath *latestPath = _mapView.navigator.traveledPath; if (latestPath.count > 0) { CLLocationCoordinate2D begin = [latestPath coordinateAtIndex:0]; CLLocationCoordinate2D current = [latestPath coordinateAtIndex:latestPath.count - 1]; NSLog(@"Path from %f/%f to %f/%f", begin.latitude, begin.longitude, current.latitude, current.longitude); } } #pragma mark - Buttons and things // Add some buttons to the view. - (void)makeButtons { // Start navigation. UIButton *navButton = [UIButton buttonWithType:UIButtonTypeCustom]; [navButton addTarget:self action:@selector(startNav) forControlEvents:UIControlEventTouchUpInside]; [navButton setTitle:@"Start navigation" forState:UIControlStateNormal]; [navButton setBackgroundColor:[UIColor blueColor]]; [navButton setAlpha:0.5]; navButton.frame = CGRectMake(5.0, 150.0, 200.0, 35.0); [_mapView addSubview:navButton]; // Get the time to the next destination. UIButton *timeButton = [UIButton buttonWithType:UIButtonTypeCustom]; [timeButton addTarget:self action:@selector(getTimeToNextDestination) forControlEvents:UIControlEventTouchUpInside]; [timeButton setTitle:@"Time to next destination" forState:UIControlStateNormal]; [timeButton setBackgroundColor:[UIColor blueColor]]; [timeButton setAlpha:0.5]; timeButton.frame = CGRectMake(5.0, 190.0, 200.0, 35.0); [_mapView addSubview:timeButton]; // Get the distance to the next destination. UIButton *distanceButton = [UIButton buttonWithType:UIButtonTypeCustom]; [distanceButton addTarget:self action:@selector(getDistanceToNextDestination) forControlEvents:UIControlEventTouchUpInside]; [distanceButton setTitle:@"Distance to next destination" forState:UIControlStateNormal]; [distanceButton setBackgroundColor:[UIColor blueColor]]; [distanceButton setAlpha:0.5]; distanceButton.frame = CGRectMake(5.0, 230.0, 200.0, 35.0); [_mapView addSubview:distanceButton]; // Get the current route leg. UIButton *routeButton = [UIButton buttonWithType:UIButtonTypeCustom]; [routeButton addTarget:self action:@selector(getCurrentRouteLeg) forControlEvents:UIControlEventTouchUpInside]; [routeButton setTitle:@"Get current route leg" forState:UIControlStateNormal]; [routeButton setBackgroundColor:[UIColor blueColor]]; [routeButton setAlpha:0.5]; routeButton.frame = CGRectMake(5.0, 270.0, 200.0, 35.0); [_mapView addSubview:routeButton]; // Get the most recently traveled path. UIButton *pathButton = [UIButton buttonWithType:UIButtonTypeCustom]; [pathButton addTarget:self action:@selector(getTraveledPath) forControlEvents:UIControlEventTouchUpInside]; [pathButton setTitle:@"Get current route leg" forState:UIControlStateNormal]; [pathButton setBackgroundColor:[UIColor blueColor]]; [pathButton setAlpha:0.5]; pathButton.frame = CGRectMake(5.0, 310.0, 200.0, 35.0); [_mapView addSubview:pathButton]; } @end
زمان رسیدن به مقصد بعدی
برای دریافت زمان به مقصد بعدی، timeToNextDestination()
را فراخوانی کنید. این مقدار NSTimeInterval
را برمی گرداند. مثال زیر ثبت زمان رسیدن به مقصد بعدی را نشان می دهد:
if let navigator = mapView.navigator {
let time = navigator.timeToNextDestination
let minutes = floor(time/60)
let seconds = round(time - minutes * 60)
NSLog("Time to next destination: %.0f:%.0f", minutes, seconds)
}
NSTimeInterval time = _mapView.navigator.timeToNextDestination;
int minutes = floor(time/60);
int seconds = round(time - minutes * 60);
NSLog(@"%@", [NSString stringWithFormat:@"Time to next destination: %i:%i.", minutes, seconds]);
فاصله گرفتن تا مقصد بعدی
برای دریافت فاصله تا مقصد بعدی، distanceToNextDestination()
را فراخوانی کنید. این یک مقدار CLLocationDistance
را برمی گرداند. واحدها بر حسب متر مشخص می شوند.
if let navigator = mapView.navigator {
let distance = navigator.distanceToNextDestination
let miles = distance * 0.00062137
NSLog("Distance to next destination: %.2f miles.", miles)
}
CLLocationDistance distance = _mapView.navigator.distanceToNextDestination;
double miles = distance * 0.00062137;
NSLog(@"%@", [NSString stringWithFormat:@"Distance to next destination: %.2f.", miles]);
رسیدن به شرایط ترافیکی به مقصد بعدی
برای دریافت مقداری که جریان ترافیک به مقصد بعدی را نشان میدهد، delayCategoryToNextDestination
را فراخوانی کنید. این پارامتر GMSNavigationDelayCategory
را برمی گرداند. مثال زیر ارزیابی نتیجه و ثبت یک پیام ترافیک را نشان می دهد:
if let navigator = mapView.navigator {
// insert sample for evaluating traffic value
let delay = navigator.delayCategoryToNextDestination
let traffic = "unavailable"
switch delay {
case .noData:
traffic = "unavailable"
case .heavy:
traffic = "heavy"
case .medium:
traffic = "moderate"
case .light:
traffic = "light"
default:
traffic = "unavailable"
}
print("Traffic is \(traffic).")
}
GMSNavigationDelayCategory delay = mapView.navigator.delayCategoryToNextDestination;
NSString *traffic = @"";
switch (delayCategory) {
case GMSNavigationDelayCategoryNoData:
traffic = @"No Data";
break;
case GMSNavigationDelayCategoryHeavy:
traffic = @"Heavy";
break;
case GMSNavigationDelayCategoryMedium:
traffic = @"Medium";
break;
case GMSNavigationDelayCategoryLight:
traffic = @"Light";
break;
default:
NSLog(@"Invalid delay category: %zd", delayCategory);
}
NSLog(@"%@", [NSString stringWithFormat:@"Traffic is %@.", traffic]);
دریافت اطلاعات در مورد پای فعلی
برای دریافت اطلاعات در مورد مسیر فعلی، با currentRouteLeg
تماس بگیرید. این یک نمونه GMSRouteLeg
را برمی گرداند که از آن می توانید دریافت کنید:
- مقصد برای پا.
- مختصات نهایی در ساق پا.
- مسیری که شامل مختصاتی است که پای مسیر را تشکیل می دهد.
مثال زیر ثبت مختصات عنوان و lat/lng را برای قسمت بعدی مسیر نشان می دهد:
if let navigator = mapView.navigator {
let currentLeg = navigator.currentRouteLeg
let nextDestination = currentLeg?.destinationWaypoint?.title
let lat = currentLeg?.destinationCoordinate.latitude.description
let lng = currentLeg?.destinationCoordinate.longitude.description
NSLog(nextDestination! + ", " + lat! + "/" + lng!)
}
GMSRouteLeg *currentSegment = _mapView.navigator.currentRouteLeg;
NSString *nextDestination = currentSegment.destinationWaypoint.title;
CLLocationDegrees lat = currentSegment.destinationCoordinate.latitude;
CLLocationDegrees lng = currentSegment.destinationCoordinate.longitude;
NSLog(@"%@", [NSString stringWithFormat:@"%@, %f/%f", nextDestination, lat, lng]);
دریافت آخرین مسیر طی شده
برای دریافت آخرین مسیر طی شده، با traveledPath
تماس بگیرید. این یک نمونه GMSPath
را برمی گرداند که برای حذف نقاط اضافی ساده شده است (به عنوان مثال تبدیل نقاط خطی متوالی به یک پاره خط منفرد). این مسیر تا شروع راهنمایی خالی است. مثال زیر دریافت آخرین مسیر طی شده را نشان می دهد:
if let navigator = mapView.navigator {
let latestPath = navigator.traveledPath
if latestPath.count() > 0 {
let begin: CLLocationCoordinate2D = latestPath.coordinate(at: 0)
let current: CLLocationCoordinate2D = latestPath.coordinate(at: latestPath.count() - 1)
print("Path from (\(begin.latitude),\(begin.longitude)) to (\(current.latitude),\(current.longitude))")
}
}
GMSPath *latestPath = mapView.navigator.traveledPath;
if (latestPath.count > 0) {
CLLocationCoordinate2D begin = [latestPath coordinateAtIndex:0];
CLLocationCoordinate2D current = [latestPath coordinateAtIndex:latestPath.count - 1];
NSLog(@"Path from %f/%f to %f/%f",
begin.latitude,
begin.longitude,
current.latitude,
current.longitude);
}