0

I am working on a project in which you can save the address of any new client the user adds onto the app. Now I want this address to be interactive so when tapped it should open google maps in the user's phone. This is what I have in my ContentView so far:

enter image description here

enter image description here

However, it seems something is wrong in my view as this error pops up close to the constants toLatitude and toLongitude: Type of expression is ambiguous without more context

  • This code is outdated code UIApplication.sharedApplication() use UIApplication.shared.canOpen......... – Raja Kishan May 21 '21 at 11:31
  • See [this](https://stackoverflow.com/a/48703592/5941807) – Joannes May 21 '21 at 12:24
  • Thanks @Joannes, It does't find fromLongitude, toLongitude, fromLatitude and toLatitude in scope when I use that code. Should I set two constants for those values? I am not sure how to work the 'to' and 'from' – Paolo Farina May 21 '21 at 13:14
  • Everything inside your “if block” needs to go inside the action of a Button — you can’t mix imperative code inside your view hierarchy like that. That’s why you’re getting at least the first error. – jnpdx May 21 '21 at 14:52
  • Thanks @jnpdx. I have added the if content inside a button (please see the edited question) but I still get the same error – Paolo Farina May 21 '21 at 16:43
  • You need to follow the button with `{ }` and include the action content inside the curly brackets. – jnpdx May 21 '21 at 16:59

2 Answers2

0

You can use custom WebView (wrapper around WKWebView ) and open html with needed google maps coordinates.

Sample of WebView you can find here: https://stackoverflow.com/a/66429366/4423545

But for you will be better to set size manually.

Andrew_STOP_RU_WAR_IN_UA
  • 9,318
  • 5
  • 65
  • 101
0

It's not a bug but something unwanted code is there or has some brackets issue.

For better code make a separate function for navigating to Google Maps.

struct ContentView: View {
    var body: some View {
        Button ("Open Google Map") {
            let toLatitude = (self.destinationLocation?.latitude)!
            let toLongitude = (self.destinationLocation?.longitude)!
            
            let fromLatitude = 10.00
            let fromLongitude = 10.00
            
            self.navigateOnGoogleMap(sourceLatitude: fromLatitude, sourceLongitude: fromLongitude, destinationLatitude: toLatitude, destinationLongitude: toLongitude)
        }
    }
    
    func navigateOnGoogleMap(sourceLatitude : Double, sourceLongitude : Double, destinationLatitude : Double, destinationLongitude : Double) {
        let urlGoogleMap : URL = URL(string: "comgooglemaps://?saddr=\(sourceLatitude),\(sourceLongitude)&daddr=\(destinationLatitude),\(destinationLongitude)&directionsmode=driving")!
        
        if UIApplication.shared.canOpenURL(urlGoogleMap) {
            UIApplication.shared.open(urlGoogleMap, options: [:], completionHandler: nil)
            
        } else {
            let urlString = URL(string:"http://maps.google.com/?saddr=\(sourceLatitude),\(sourceLongitude)&daddr=\(destinationLatitude),\(destinationLongitude)&directionsmode=driving")
            
            UIApplication.shared.open(urlString!, options: [:], completionHandler: nil)
        }
    }
}
Raja Kishan
  • 16,767
  • 2
  • 26
  • 52