Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable Google Maps zoom controls using Swift

I was reading the documentation of Google maps for swift , but compared with Android, I didn't find a way to set the 'Zoom Controls' on my map and by default are disabled.

Exist a way with the Google Maps iOS SDK to display the controls?

like image 443
Benjamin RD Avatar asked May 09 '17 19:05

Benjamin RD


2 Answers

I think @Scriptable is right, the documentation hasn't a section for Zoom Controls for iOS SDK.

Well, I made my own (and very basic) controls. enter image description here

Keep this order (MapView, Button, Button), else, you can't see the buttons.

enter image description here

First one, you must select your UIVIew and change the class to GSMMapView

and, in the MapViewController

import Foundation
import UIKit
import GoogleMaps

class MapViewController: UIViewController {

    struct Place {
        let id: Int
        let name: String
        let lat: CLLocationDegrees
        let lng: CLLocationDegrees
        let icon: String
    }

    @IBOutlet weak var mapView: GMSMapView!
    var markerDict: [Int: GMSMarker] = [:]

    var zoom: Float = 15

    override func viewDidLoad() {
        super.viewDidLoad()

        let camera = GMSCameraPosition.camera(withLatitude: 34.1381168, longitude: -118.3555723, zoom: zoom)
        self.mapView.camera = camera

        do {
            if let styleURL = Bundle.main.url(forResource: "style", withExtension: "json") {
                mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
            } else {
                NSLog("Unable to find style.json")
            }
        } catch {
            NSLog("One or more of the map styles failed to load. \(error)")
        }

        let places = [
            Place(id: 0, name: "MrMins", lat: 34.1331168, lng: -118.3550723, icon: "i01"),
        ]

        for place in places {
            let marker = GMSMarker()
            marker.position = CLLocationCoordinate2D(latitude: place.lat, longitude: place.lng)
            marker.title = place.name
            marker.snippet = "Custom snipet message \(place.name)"
            marker.appearAnimation = kGMSMarkerAnimationPop
            //marker.icon = self.imageWithImage(image: UIImage(named: place.icon)!, scaledToSize: CGSize(width: 35.0, height: 35.0))
            marker.map = self.mapView

            markerDict[place.id] = marker
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    @IBAction func btnZoomIn(_ sender: Any) {
        zoom = zoom + 1
        self.mapView.animate(toZoom: zoom)
    }

    @IBAction func btnZoomOut(_ sender: Any) {
        zoom = zoom - 1
        self.mapView.animate(toZoom: zoom)
    }


}
like image 117
Benjamin RD Avatar answered Nov 16 '22 00:11

Benjamin RD


You should catch current value of zoom, because If you hardcode zoom value and user will use not only buttons but buttons and gestures when user will pressed on button after zoom out by gestures you will zoom to old zoom value (very close to map)

to fix this moment you should catch zoom value here (in GMSMapViewDelegate)

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
    zoom = mapView.camera.zoom
}

all code will be looks like this

    class A: UIViewController {
      var zoom: Float = 15

    @IBAction func ZoomInButtonPressed(_ sender: UIButton) {
        let nextZoom = zoom + 1
        mapView.animate(toZoom: nextZoom)
    }
}

extension A: GMSMapViewDelegate { 
    func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
        zoom = mapView.camera.zoom
    }
}
like image 37
Vladimir Pchelyakov Avatar answered Nov 16 '22 01:11

Vladimir Pchelyakov