Skip to main content

Route between two places using Google Maps iOS SDK.

Its very interesting to have the Google maps 
Firstly. We have to install the pods of Google maps.
So, follow the steps for installing the pods of Google maps on developers.google.com
1. Create the pod file as on the site.
2. In Podfile add the pod of Alamofire and SwiftyJSON.
3. Save the pod file.
    pod 'GoogleMaps'
    pod 'GooglePlaces'
    pod 'Alamofire', '~> 4.2.0'
    pod 'SwiftyJSON'
4. Install the pods.



Here is the code for route.

import UIKit
import GoogleMaps
import GooglePlaces
import Alamofire
import SwiftyJSON

class GoogleViewController: UIViewController,GMSMapViewDelegate {

    @IBOutlet weak var googleView: GMSMapView!
    
    // AIzaSyC907BQBnrZK0UjA2zARtE6Mq7L__yqw5Q
    override func viewDidLoad() {
        super.viewDidLoad()
        GMSServices.provideAPIKey("AIzaSyC907BQBnrZK0UjA2zARtE6Mq7L__yqw5Q")
        GMSPlacesClient.provideAPIKey("AIzaSyC907BQBnrZK0UjA2zARtE6Mq7L__yqw5Q")
        
        
        // Create a GMSCameraPosition that tells the map to display the
       
        let camera = GMSCameraPosition.camera(withLatitude: 28.524555,
                                              longitude: 77.275111,
                                              zoom: 10.0,
                                              bearing: 30,
                                              viewingAngle: 40)
        //Setting the googleView
        self.googleView.camera = camera
        self.googleView.delegate = self
        self.googleView?.isMyLocationEnabled = true
        self.googleView.settings.myLocationButton = true
        self.googleView.settings.compassButton = true
        self.googleView.settings.zoomGestures = true
        self.googleView.animate(to: camera)
        self.view.addSubview(self.googleView)
        
        //Setting the start and end location
        let origin = "\(28.524555),\(77.275111)"
        let destination = "\(28.643091),\(77.218280)"
        
        
        let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving"
        
        //Rrequesting Alamofire and SwiftyJSON
        Alamofire.request(url).responseJSON { response in
            print(response.request as Any)  // original URL request
            print(response.response as Any) // HTTP URL response
            print(response.data as Any)     // server data
            print(response.result)   // result of response serialization
            
            let json = JSON(data: response.data!)
            let routes = json["routes"].arrayValue
            
            for route in routes
            {
                let routeOverviewPolyline = route["overview_polyline"].dictionary
                let points = routeOverviewPolyline?["points"]?.stringValue
                let path = GMSPath.init(fromEncodedPath: points!)
                let polyline = GMSPolyline.init(path: path)
                polyline.strokeColor = UIColor.blue
                polyline.strokeWidth = 2
                polyline.map = self.googleView
            }
        }

        
         // Creates a marker in the center of the map.
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: 28.524555, longitude: 77.275111)
        marker.title = "Mobiloitte"
        marker.snippet = "India"
       marker.map = googleView
        
        //28.643091, 77.218280
        let marker1 = GMSMarker()
        marker1.position = CLLocationCoordinate2D(latitude: 28.643091, longitude: 77.218280)
        marker1.title = "NewDelhi"
        marker1.snippet = "India"
        marker1.map = googleView
       
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //Drawing straight line between two points.
    
//    func draw() {
//        let path = GMSMutablePath()
//        path.addLatitude(28.524555, longitude:77.275111) //Mobiloitte
//        path.addLatitude(28.643091, longitude:77.218280) // New Delhi
//        //        path.addLatitude(21.291, longitude:-157.821) // Hawaii
//        //        path.addLatitude(37.423, longitude:-122.091) // Mountain View
//        
//        let polyline = GMSPolyline(path: path)
//        polyline.strokeColor = .blue
//        polyline.strokeWidth = 3.0
//        polyline.map = self.googleView
//        
//
//    }

   

}

Comments

Popular posts from this blog

Draggable Marker and Drawing the routes between them using Google Maps | IOS | Swift

This blog is about how we can make the marker draggable and get the current location from it and to draw the routes between them. For that we have to install the pods of Google maps from developer.google.com. Then we have to add the following    pod 'GoogleMaps'   pod 'GooglePlaces'   pod 'GooglePlacesAPI'   pod 'SwiftyJSON' And we have to save the pods into the pod file. Then, we have to install all the pods through terminal. Again we have to make the outlet for textfield and button as discussed below. 2 TextField and 3 Buttons. Hope you will enjoy the coding. Happy coding.! import UIKit import GoogleMaps import GooglePlaces import SwiftyJSON import Alamofire enum Location { case startLocation case destinationLocation } class ViewController: UIViewController , GMSMapViewDelegate ,  CLLocationManagerDelegate { @IBOutlet weak var googleMaps: GMSMapView ! @IBOutlet weak ...

UIDatePicker with UserNotifications | iOS | Swift

Local Notifications The big problem with APNS is that it requires you to run a server somewhere that interacts with APNS to push a notification to a device. For applications that already have a client-server architecture such as an IM client this is no big deal. There are also a number of third parties who can provide a cost efficient service for small scale users who do not want the trouble of running their own server. However for many applications this is still way too complicated. This blog helps us in triggering the notification at specific time which is being selected by user in date picker. For that you have to take textField and two buttons. And don,t forget to edit AppDelegate method. import  UserNotifications   func  application( _  application:  UIApplication , didFinishLaunchingWithOptions launchOptions: [ UIApplicationLaunchOptionsKey :  Any ]?) ->  Bool  {          le...