BhavinBhadani / MapDemo

3 stars 0 forks source link

im getting expection #1

Open premkumarsepuri opened 5 years ago

premkumarsepuri commented 5 years ago

im using xcode 10.1 and swift 4.1 but it successfully degraded but im getting 11db expection can you please help

BhavinBhadani commented 5 years ago

At which line you got the issue?

premkumarsepuri commented 5 years ago
          At this point im getting 11db expection 

DispatchQueue.main.async(execute: { () -> Void in do { let directionsData = try? Data(contentsOf: directionsURL!)

                    if let dictionary = try JSONSerialization.jsonObject(with: directionsData!, options: .allowFragments) as? [String: Any]{
premkumarsepuri commented 5 years ago

// // myMapScreen.swift // Mapper // // Created by QUADRANT on 1/18/19. // Copyright © 2019 QUADRANT. All rights reserved. //

import UIKit import CoreLocation import GoogleMaps import Alamofire import SwiftyJSON import GooglePlacesSearchController import GooglePlaces

class myMapScreen: UIViewController,CLLocationManagerDelegate {

enum Location {
    case source
    case destinationLocation

}

 var number = "11"

var locationManager : CLLocationManager!
var locationSelected = Location.source
var locatedSelected = Location.destinationLocation

var locationStart = CLLocation()
var locationEnd = CLLocation()

// var sourceLat = 17.4947 // var sourceLong = 78.3986 // var DestinationLat = 17.4837465 // var DestinationLong = 78.4170762

var startLOC = CLLocation()
var endLOC = CLLocation()
var API_KEY = "AIzaSyAExU-P2Rs3bscPtZC-VjOGsjNBVjlz7qg"

@IBOutlet var myView: GMSMapView!

@IBOutlet weak var source: UITextField!

@IBAction func sourceTF(_ sender: UIButton) {
    number = "11"
    locatedSelected = .source
    source.resignFirstResponder()
    let acController = GMSAutocompleteViewController()
    acController.delegate = self as? GMSAutocompleteViewControllerDelegate
    UISearchBar.appearance().setTextColor(color: UIColor.black)
    present(acController, animated: true, completion: nil)
}

@IBOutlet weak var destination: UITextField!

@IBAction func destinationAction(_ sender: UIButton) {
    locatedSelected = .destinationLocation
    destination.resignFirstResponder()
    let controller2 = GMSAutocompleteViewController()
    controller2.delegate = self as? GMSAutocompleteViewControllerDelegate
   UISearchBar.appearance().setTextColor(color: UIColor.black)
    present(controller2,animated: true ,completion: nil)

}

//

override func viewDidLoad() {
    super.viewDidLoad()
   locationManager = CLLocationManager()
    locationManager.delegate = self
   locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    locationManager.requestWhenInUseAuthorization()
  locationManager.startUpdatingLocation()

    // Do any additional setup after loading the view.
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  self.showCurrentLocationMap()
   self.locationManager.stopUpdatingLocation()
}

func showCurrentLocationMap(){

// let camera = GMSCameraPosition.camera(withLatitude:(self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.latitude)!, zoom: 14) //// // let mapView = GMSMapView.map(withFrame: CGRect(x: 0, y: 0, width: self.myView.frame.size.width, height: self.myView.frame.size.height), camera: camera) // myView.settings.myLocationButton = true // myView.isMyLocationEnabled = true //// // let marker = GMSMarker() // marker.position = camera.target // marker.snippet = "currentLocation" // marker.appearAnimation = .pop // marker.map = myView // self.myView.addSubview(myView)

}

// function create marker

func createMarker(titleMarker: String, iconMarker: UIImage, latitude: CLLocationDegrees, longitude: CLLocationDegrees) {
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(latitude, longitude)
    marker.title = titleMarker
    marker.icon = iconMarker
    marker.map = myView
}

func drawPath(startLocation: CLLocation, endLocation: CLLocation)
{
    let origin = "\(startLocation.coordinate.latitude),\(startLocation.coordinate.longitude)"
    let destination = "\(endLocation.coordinate.latitude),\(endLocation.coordinate.longitude)"
    let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving&key=\(API_KEY)"

    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 as Any)   // result of response serialization

        do
        {
        let json = try JSON(data: response.data!)
        let routes = json["routes"].arrayValue
        print(json)
        // print route using Polyline

        DispatchQueue.global(qos: .default).async(execute: {() -> Void in
            // Do something...
            DispatchQueue.main.async(execute: {() -> Void in
                // self.hideHUD()
            })
        })
        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.strokeWidth = 4
            polyline.strokeColor =  UIColor.black
            polyline.map = self.myView

        }
        }
        catch
        {
            print("error")
        }
    } // json
    }

@IBAction func button5(_ sender: Any) {

// self.startLOC = CLLocation(latitude: sourceLat, longitude: sourceLong) // self.endLOC = CLLocation(latitude: DestinationLat, longitude: DestinationLong) // // drawPath(startLocation: startLOC, endLocation: endLOC)

// // let marker = GMSMarker() // marker.position = CLLocationCoordinate2D(latitude: sourceLat, longitude: sourceLong) // // marker.icon = userImage.af_imageScaled(to: CGSize(width: 50, height: 50)).af_imageRoundedIntoCircle() // marker.title = "Source" // marker.map = myView // // // let markerr = GMSMarker() // markerr.position = CLLocationCoordinate2D(latitude: DestinationLat, longitude: DestinationLong) // // markerr.icon = washerImage.af_imageScaled(to: CGSize(width: 50, height: 50)).af_imageRoundedIntoCircle() // markerr.title = "Desintation" // markerr.map = myView // // let camera = GMSCameraPosition.camera(withLatitude: sourceLat, longitude: sourceLong, zoom: 14.0) // self.myView.camera = camera // self.myView.animate(to: camera)

    self.drawPath(startLocation: locationStart, endLocation: locationEnd)

}

}// end of the class

extension myMapScreen: GMSAutocompleteViewControllerDelegate { func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) { print("Error: ", error.localizedDescription) }

func wasCancelled(_ viewController: GMSAutocompleteViewController) {
      dismiss(animated: true, completion: nil)
}

func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
    // Get the place name from 'GMSAutocompleteViewController'
    // Then display the name in textField

    // source latitude and longtitude

    let camera = GMSCameraPosition.camera(withLatitude: place.coordinate.latitude, longitude: place.coordinate.longitude, zoom: 16.0
    )

    if locationSelected == .source {

        self.source.text = "\(place.coordinate.latitude), \(place.coordinate.longitude)"
        self.locationStart = CLLocation(latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
        self.createMarker(titleMarker: "Location Start", iconMarker: #imageLiteral(resourceName: "mapspin"), latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)

    } else
    {
        self.destination.text = "\(place.coordinate.latitude), \(place.coordinate.longitude)"
        locationEnd = CLLocation(latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
        createMarker(titleMarker: "Location End", iconMarker: #imageLiteral(resourceName: "mapspin"), latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)

    }

    self.myView.camera = camera

   // destination.text = place.name

    // Dismiss the GMSAutocompleteViewController when something is selected
    dismiss(animated: true, completion: nil)
    //soure.text = ""
   // destination.text = ""

}

func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
    UIApplication.shared.isNetworkActivityIndicatorVisible = true
}

func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
    UIApplication.shared.isNetworkActivityIndicatorVisible = false
}

func viewController(_ viewController: GMSAutocompleteViewController, didSelect prediction: GMSAutocompletePrediction) -> Bool {
   // print("1111111: \(prediction.attributedPrimaryText)")
   // print("22222222: \(prediction.attributedSecondaryText)")

    return true

}

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
    print("COORDINATE \(coordinate)") // when you tapped coordinate
}

} public extension UISearchBar {

public func setTextColor(color: UIColor) {
    let svs = subviews.flatMap { $0.subviews }
    guard let tf = (svs.filter { $0 is UITextField }).first as? UITextField else { return }
    tf.textColor = color
}

}

premkumarsepuri commented 5 years ago

i want to draw ployline by using google autocomplete view controller but i can't able to compare by using enum so that im getting only start location can you help me plz

premkumarsepuri commented 5 years ago

just like this https://www.youtube.com/watch?v=dSoa93ASylA

premkumarsepuri commented 5 years ago

screen shot 2019-01-24 at 6 49 18 pm