polyline in subview (googlemaps) (swift)

func callWebService(){

        let url = NSURL(string: "https://maps.googleapis.com/maps/api/directions/json?origin=\(17.421542),\(78.452169)&destination=\(18.7603),\(73.8630)&waypoints=\(21.146052),\(79.088196)&key=AIzaSyDxSgGQX6jrn4iq6dyIWAKEOTneZ3Z8PtU")
        print(url)
        let request = NSURLRequest(URL: url!)
        
        let config = NSURLSessionConfiguration.defaultSessionConfiguration()
        let session = NSURLSession(configuration: config)
        
        let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
            
            // notice that I can omit the types of data, response and error
            do{
                if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary {
                    
                   print(jsonResult)
                     dispatch_async(dispatch_get_main_queue()) {
                    let routes = jsonResult.valueForKey("routes")
                   print(routes)
                    
                    let overViewPolyLine = routes![0]["overview_polyline"]!!["points"] as! String
                    print(overViewPolyLine)
//                    }
                    if overViewPolyLine != ""{
                        
                        //Call on Main Thread
//                        dispatch_async(dispatch_get_main_queue()) {
                        
                            self.addPolyLineWithEncodedStringInMap(overViewPolyLine)
                        }
                        
                        
                    }
                    
                }
            }
            catch{
                
                print("Somthing wrong")
            }
        });
        
        // do whatever you need with the task e.g. run
        task.resume()
    }
    
    func addPolyLineWithEncodedStringInMap(encodedString: String) {
        
        
        let camera = GMSCameraPosition.cameraWithLatitude(18.5204, longitude: 73.8567, zoom: 10.0)
        let mapView = GMSMapView.mapWithFrame(homeMapView.bounds, camera: camera)
//        mapView.myLocationEnabled = true
         homeMapView .addSubview(mapView)
        
        let path = GMSMutablePath(fromEncodedPath: encodedString)
        let polyLine = GMSPolyline(path: path)
        polyLine.strokeWidth = 5
        polyLine.strokeColor = UIColor.redColor()
        polyLine.map = mapView
        
        let smarker = GMSMarker()
        smarker.position = CLLocationCoordinate2D(latitude:17.421542, longitude: 78.452169)
        smarker.title = "nizam"
        smarker.snippet = "hyderabad"
        smarker.map = mapView
        
        let dmarker = GMSMarker()
        dmarker.position = CLLocationCoordinate2D(latitude: 18.7603, longitude: 73.8630)
        dmarker.title = "Chakan"
        dmarker.snippet = "Maharshtra"
        dmarker.map = mapView
        
      //  homeMapView = mapView
        

    }
Previous
Next Post »