func callWebService(){
for i in 0..<viaStationLongitude_Array.count {
let latitude:String = self.viaStationLatitude_Array .objectAtIndex(i) as! String
print(latitude)
combine_viaLatLongArray.addObject(latitude)
print(combine_viaLatLongArray)
let longitude: String = self.viaStationLongitude_Array .objectAtIndex(i) as! String
combine_viaLatLongArray.addObject(longitude)
print(combine_viaLatLongArray)
combine_viaLatLongArray .addObject("|")
print(combine_viaLatLongArray)
}
let greeting:String = combine_viaLatLongArray .componentsJoinedByString(",")
let old:String = greeting .stringByReplacingOccurrencesOfString(",|,", withString: "|")
new = old .stringByReplacingOccurrencesOfString(",|", withString: "")
print(new)
// let url = NSURL(string: "https://maps.googleapis.com/maps/api/directions/json?origin=\(fromLatitude),\(fromLongitude)&destination=\(toLatitude),\(toLongitude)&waypoints=\(new)&key=AIzaSyDxSgGQX6jrn4iq6dyIWAKEOTneZ3Z8PtU")
let str = "https://maps.googleapis.com/maps/api/directions/json?origin=\(fromLatitude),\(fromLongitude)&destination=\(toLatitude),\(toLongitude)&waypoints=\(new)&key=AIzaSyBkjmHAHytmX3iftucKIeqJn4qMEIjhkFo"
print(str)
let url = NSURL(string: str.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())!)!
// var request = NSURLRequest(URL: url, cachePolicy: NSURLRequestUseProtocolCachePolicy, timeoutInterval: 60.0)
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(CDouble(currentLatitude)!, longitude: CDouble(currentLongitude)!, zoom: 16.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
//current location
let currentMarker = GMSMarker()
currentMarker.position = CLLocationCoordinate2D(latitude: CDouble(currentLatitude)!, longitude: CDouble(currentLongitude)!)
currentMarker.title = "Chakan"
currentMarker.snippet = "Maharshtra"
currentMarker.icon = UIImage(named: "pin-6-24 (1).png")!
currentMarker.map = mapView
//from location
let fromMarker = GMSMarker()
fromMarker.position = CLLocationCoordinate2D(latitude:CDouble(fromLatitude)!, longitude: CDouble(fromLongitude)!)
fromMarker.title = "nizam"
fromMarker.icon = UIImage(named: "pin-6-24.png")!
fromMarker.snippet = "hyderabad"
fromMarker.map = mapView
//to location
let toMarker = GMSMarker()
toMarker.position = CLLocationCoordinate2D(latitude: CDouble(toLatitude)!, longitude: CDouble(toLongitude)!)
toMarker.title = "Chakan"
toMarker.snippet = "Maharshtra"
toMarker.icon = UIImage(named: "pin-6-24 (1).png")!
toMarker.map = mapView
// homeMapView = mapView
//via stops viaStationLongitude_Array
for i in 0..<viaStationPlace_Array.count {
let latStr = viaStationLatitude_Array .objectAtIndex(i)
let longStr = viaStationLongitude_Array.objectAtIndex(i)
let placeStr = viaStationPlace_Array.objectAtIndex(i)
let viaMarker = GMSMarker()
viaMarker.position = CLLocationCoordinate2D(latitude: CDouble(latStr as! String)!, longitude: CDouble(longStr as! String)!)
viaMarker.icon = UIImage(named: "dotcircle.png")!
viaMarker.title = placeStr as? String
viaMarker.map = mapView
}
}
Sign up here with your email
ConversionConversion EmoticonEmoticon