GMS map view myLocation button action iOS Swift
In GMS map view when you try with the following.
self.maps.isMyLocationEnabled = true
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
Post a Comment