Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show direction button in swift mapView using googleMaps api

I'm developing an app for both android and ios. in android mapview when I tap on marks of the map 2 buttons are shown in the bottom of the view. (directions and googlemaps) see the uploaded image

enter image description here

following the instruction from google I setup the mapView in swift to show maps from google. but when I tap on the mark, I don't see any button in the mapView. why is that?

google instruction for setting up the mapview in swift: https://developers.google.com/maps/documentation/ios-sdk/start

like image 354
Lithium Avatar asked Mar 28 '16 10:03

Lithium


1 Answers

I googled for this but I found nothing. so I did it manually. by overriding 'didTapMarker' I added these two buttons:

    func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
            if marker.title != nil {

                let mapViewHeight = mapView.frame.size.height
                let mapViewWidth = mapView.frame.size.width


                let container = UIView()
                container.frame = CGRectMake(mapViewWidth - 100, mapViewHeight - 63, 65, 35)
                container.backgroundColor = UIColor.whiteColor()
                self.view.addSubview(container)

                let googleMapsButton = CustomButton()
                googleMapsButton.setTitle("", forState: .Normal)
                googleMapsButton.setImage(UIImage(named: "googlemaps"), forState: .Normal)
                googleMapsButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
                googleMapsButton.frame = CGRectMake(mapViewWidth - 80, mapViewHeight - 70, 50, 50)
                googleMapsButton.addTarget(self, action: "markerClick:", forControlEvents: .TouchUpInside)
                googleMapsButton.gps = String(marker.position.latitude) + "," + String(marker.position.longitude)
                googleMapsButton.title = marker.title
                googleMapsButton.tag = 0

                let directionsButton = CustomButton()

                directionsButton.setTitle("", forState: .Normal)
                directionsButton.setImage(UIImage(named: "googlemapsdirection"), forState: .Normal)
                directionsButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
                directionsButton.frame = CGRectMake(mapViewWidth - 110, mapViewHeight - 70, 50, 50)
                directionsButton.addTarget(self, action: "markerClick:", forControlEvents: .TouchUpInside)
                directionsButton.gps = String(marker.position.latitude) + "," + String(marker.position.longitude)
                directionsButton.title = marker.title
                directionsButton.tag = 1
                self.view.addSubview(googleMapsButton)
                self.view.addSubview(directionsButton)
            }
            return true
        }

func markerClick(sender: CustomButton) {
        let fullGPS = sender.gps
        let fullGPSArr = fullGPS!.characters.split{$0 == ","}.map(String.init)

        let lat1 : NSString = fullGPSArr[0]
        let lng1 : NSString = fullGPSArr[1]


        let latitude:CLLocationDegrees =  lat1.doubleValue
        let longitude:CLLocationDegrees =  lng1.doubleValue

        if (UIApplication.sharedApplication().openURL(NSURL(string:"comgooglemaps://")!)) {
            if (sender.tag == 1) {
                UIApplication.sharedApplication().openURL(NSURL(string:
                    "comgooglemaps://?saddr=&daddr=\(latitude),\(longitude)&directionsmode=driving")!)
            } else if (sender.tag == 0) {
                UIApplication.sharedApplication().openURL(NSURL(string:
                    "comgooglemaps://?center=\(latitude),\(longitude)&zoom=14&views=traffic")!)
            }

        } else {
            let regionDistance:CLLocationDistance = 10000
            let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
            let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
            var options = NSObject()
            if (sender.tag == 1) {
                options = [
                    MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
                    MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span),
                    MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving
                ]
            } else if (sender.tag == 0) {
                options = [
                    MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
                    MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span)
                ]
            }

            let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
            let mapItem = MKMapItem(placemark: placemark)
            mapItem.name = sender.title
            mapItem.openInMapsWithLaunchOptions(options as? [String : AnyObject])
        }
    }

two buttons are added in the bottom right of the mapView and after tapping them, the googleMaps app (if exists) will be opened. thanks for vote down :(

Custom Button class:

class CustomButton: UIButton {
    var gps = ""
    override func awakeFromNib() {
        super.awakeFromNib()

        //TODO: Code for our button
    }
}
like image 89
Lithium Avatar answered Oct 17 '22 13:10

Lithium