Search code examples
iosswiftgoogle-mapsstreet-address

Use address to create route with google maps (without latitude and longitude)


How can I use address for to create a route with google maps? Without use latitude and longitude, because in my firebase i don't use latitude and longitude. I use only full address.

Example: John F.Kennedy International Airport, Van Wyck Expressway, Jamaica, New York.

My code to build the route with google maps:

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    if (control as? UIButton)?.buttonType == UIButtonType.detailDisclosure {

        mapView.deselectAnnotation(view.annotation, animated: false)

    } else if (control as? UIButton)?.buttonType == UIButtonType.custom {

        mapView.deselectAnnotation(view.annotation, animated: false)

        let alertController = UIAlertController(title: "Выберите навигатор для построение маршрута", message: nil, preferredStyle: .actionSheet)

        let googleMaps = UIAlertAction(title: "Google Maps", style: .default, handler: { (action: UIAlertAction) in

            if (UIApplication.shared.canOpenURL(URL(string: "comgooglemaps://")!)) {

                let googleUrl = URL(string: "comgooglemaps://?daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York&directionsmode=driving")!

                UIApplication.shared.open(googleUrl, options: [:], completionHandler: nil)

            } else {

                let itunesGoogleMaps = URL(string: "https://itunes.apple.com/us/app/google-maps-gps-navigation/id585027354?mt=8")

                UIApplication.shared.open(itunesGoogleMaps!, options: [:], completionHandler: nil)

            }

        })

        let cancel = UIAlertAction(title: "Отмена", style: .cancel, handler: nil)

        alertController.addAction(googleMaps)
        alertController.addAction(cancel)

        self.present(alertController, animated: true, completion: nil)

    }
}

And i use variable to get address from firebase and build the route:

var studioInfoFromDetail: Hall?

studioInfoFromDetail?.studioAddress

This variable have address on rus language.

Example:

Москва, ул. Правды д.24, строение 3 in English would be so Moscow, Pravdy street, home 24, building 1

Москва, ул.Электрозаводская, д.21 in English would be so Moscow, Elektrozavodska street, home 21

So how can i place in this variable

let googleUrl = URL(string: "comgooglemaps://?daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York&directionsmode=driving")! 

my address from firebase?

I tried to write so:

let googleUrl = URL(string: "comgooglemaps://?daddr=\(self.studioInfoFromDetail?.studioAddress)&directionsmode=driving")!

But i get a nil :(


Solution

  • I think you need to escape the non-ASCII part of the url string. Call the method addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) on your studioAddress before inserting in into the url string.

    Update:

    Since you are using a String optional (aka String? / Optional<String>) in your interpolation (see this question that I mentioned in the comment):

    let str = "comgooglemaps://?daddr=\(self.studioInfoFromDetail?.studioAddress)&directionsmode=driving"
    

    You will get:

    "comgooglemaps://?daddr=\Optional(<... your string ...>)&directionsmode=driving"
    

    Thus you need to unwrap the optional before using it.

    if let address = self.studioInfoFromDetail?.studioAddress {
        let str = "comgooglemaps://?daddr=\(address)&directionsmode=driving"
    }
    

    You can also force unwrap (with an operator !), but I would not recommend doing that.