GMS map view myLocation button action iOS Swift


In GMS map view when you try with the following.

        self.maps.isMyLocationEnabled = true

Then it will display myLocation button.When you tap that it will zoom the camera to your current location.But you cannot manually implement the action method.So it is better to manually add the button and set the actions.When you want to display the marker to current location if the user taps on the myLocation button then follow the below code.

    var currentmarker = GMSMarker()

Above class declare the marker as common.


    @IBAction func didTapOnMyLocation(_ sender: Any) {

//This code is used for move the marker to the user based location

        if CLLocationCoordinate2DIsValid(currentmarker.position) {
            currentmarker.position = (self.maps.myLocation?.coordinate)!
            currentmarker.snippet = self.getAddressForLatLng(latitude: String(currentmarker.position.latitude), longitude:String(currentmarker.position.longitude))
            self.maps.selectedMarker = currentmarker
            let camera  = GMSCameraPosition.camera(withLatitude: currentmarker.position.latitude, longitude: currentmarker.position.longitude, zoom: 20)
            self.maps.camera = camera

        } else {

//This code is used to create a marker in the user location
            currentmarker = GMSMarker(position: (self.maps.myLocation?.coordinate)!)
            currentmarker.appearAnimation = kGMSMarkerAnimationPop
            currentmarker.position = (self.maps.myLocation?.coordinate)!
            currentmarker.title = "Add Note"
            currentmarker.snippet = self.getAddressForLatLng(latitude: String(currentmarker.position.latitude), longitude:String(currentmarker.position.longitude))
            currentmarker.icon = GMSMarker.markerImage(with:UIColor.green)
            currentmarker.infoWindowAnchor = CGPoint(x: 0.5, y: 0.5)
            currentmarker.map = self.maps
            let camera  = GMSCameraPosition.camera(withLatitude: currentmarker.position.latitude, longitude: currentmarker.position.longitude, zoom: 20)
            self.maps.camera = camera
        }
    }

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Saved Image in document directory and save path in coredata not able to fetch the image file iOS swift