How to detect when tap on google map iOS Swift? - ios

I want to check and detect when tap on map marker or anything. Do any delegates exist for this case with google maps in iOS Swift 4?
Thanks.

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
//Write your code here...
}
if the delegate function is not called then set the mapview delegate in viewDidAppear() method or in LocationManager didUpdateLocations() method.

Related

Get lat/long from the google mapview

I need to get the lat/long of the location where user taps on the google map view. Is there a necessity to open GooglePlacePickerViewController for this? I need to achieve this in the GMSMapView which is used to show user current location.
It is simple use Delegate method
First set delegate
self.mapView.delegate = self
Then just
extension YourViewController:GMSMapViewDelegate {
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
// USER TAP ON coordinate
}
}
Suggestion : Just go through this tutorial https://medium.freecodecamp.org/how-you-can-use-the-google-maps-sdk-with-ios-using-swift-4-a9bba26d9c4d will not take more than 20 min that my promise will conver all the basic stuff. :)
If you need points of view based system. for example if you want what is exact point where user tap according to view X, Y
Then
you can do it like this
let points:CGPoint = mapView.projection.point(for:coordinates)
Bingo !!
assign delegate like this.
mapView.delegate = self
And use GMSMapViewDelegate to use below method
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
print("You tapped at Location \(coordinate.latitude), \(coordinate.longitude)")
}

Polyline Tap in Google Maps

I wonder if there is a tap listener for polylines drawn in Google Map. The marker's tap is this:
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
return false
}
Is there any equivalent function for polylines? I believe in Google Maps version 3 for web, you have the ability to add event listener for polylines like this:
google.maps.event.addListener(elines[i], 'click', function()
{
lineClick(this);
});
Thanks!
Is this also possible in ios/swift?
You can use isTappable property of GMSPolyline.
isTappable
If this overlay should cause tap notifications.
polyline.isTappable = true
Whenever the polyline is tapped, the GMSMapViewDelegate method didTapOverlay is called
func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) {
//Write your code here
}
For further information refer https://developers.google.com/maps/documentation/ios-sdk/reference/protocol_g_m_s_map_view_delegate-p.html#a3a2bf2ff4481528f931183cb364c0f4b

Mapbox - setCenterCoordinate is deselecting annotations

I'm trying out mapbox (using the ios sdk) and I've run into a problem that I think I've narrowed down pretty far. This is my code:
func centerMap(location: CLLocationCoordinate2D) {
map.setCenterCoordinate(location,
zoomLevel: 14,
animated: true)
}
func mapView(mapView: MGLMapView, didDeselectAnnotation annotation: MGLAnnotation) {
dealDetails.hidden = false
}
func mapView(mapView: MGLMapView, didUpdateUserLocation userLocation: MGLUserLocation?) {
if let currentLocation = userLocation?.coordinate {
centerMap(currentLocation)
}
}
If I don't re-center the map when the user's location is updated (i.e., just commenting out the centerMap(currentLocation) call) then the annotation remains selected. Re-centering the map calls the didDeselectAnnotation function, and I can't figure out how to keep that annotation selected. Any help is appreciated!
I don't think there's any way around that if you update the center coordinate. You'd have to re-select the annotation. However, you probably don't need to do that. If you set the userTrackingMode on the map view to .Follow, it should re-center automatically.

How to detect camera position for google map in swift

I get to know after searching that idleAtCameraPosition and didChangeCameraPosition this methods are called when the map moves .
How can i write this method for swift? I have set delegate to CLLocationManagerDelegate and GMSMapViewDelegate but still this methods are not called. I have written method as below:
func mapView(mapView: GMSMapView!, didChangeCameraPosition position: GMSCameraPosition!) {
print(position)
}
func mapView(mapView:GMSMapView!,idleAtCameraPosition position:GMSCameraPosition!)
{
print(position)
}
But my method is not getting called.
Also I want to customise info window just like done here : Custom Info Window for Google Maps . But can't find the methods how to do same in swift. I am new to swift s o not getting this.
Maybe too late, but you must extend the class with GMSMapViewDelegate. For example:
class NearMeViewController: CLLocationManagerDelegate, GMSMapViewDelegate
Then, how Ztan says, you have to delegate your map with:
self.mapView.delegate = self
With this, the method didChangeCameraPosition will respond every time that that camera's position is changed.
Hope it helps.
You need to import the GMSMapViewDelegate at the top of your class
put self.mapView.delegate = self in your viewDidLoad()
and then you can use this method
func mapView(mapView: GMSMapView, idleAtCameraPosition position: GMSCameraPosition) {
// You can use the position.target for get the current camera position
}

Custom info window in IOS google maps sdk

being a newby IOS developer, I'm really struggling to get something basic to work.
I have a need to display this kind of custom info window upon a marker click in the google maps sdk for ios.
Any help would be appreciated.
I've already seen the third party components, but even with them I cannot get this to display. There is always a title, snippet, left image and right image part. The real question is how do you get the gold star rating in the window, with the text next to it.
Make Xib as you want...set Text and image
set delegate GMSMapViewDelegate
-(UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker{
CustomInfoWindow *infoWindow=[[[NSBundle mainBundle] loadNibNamed:#"InfoWindow" owner:self options:nil] objectAtIndex:0];
return infoWindow;
}
https://www.youtube.com/watch?v=ILiBXYscsyY
for more help
see this video..Uploded by google
I was suffering from the same problem of Info window customization in GoogleMapsSdk for iOS for a lot of days, got frustrated & did it my self!
Clean, Completely customizable & Own UIControls with your custom actions code can be found on Github Right here
Happy coding :)
Swift 3.0 Solution
Google Map CustomInfoWindow
//empty the default infowindow
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
return UIView()
}
// reset custom infowindow whenever marker is tapped
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
customInfoView.removeFromSuperview()
// customInfoView.button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
self.view.addSubview(customInfoView)
// Remember to return false
// so marker event is still handled by delegate
return false
}
// let the custom infowindow follows the camera
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
if (locationMarker != nil){
let location = locationMarker.position
customInfoView.center = mapView.projection.point(for: location)
}
}
// take care of the close event
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
customInfoView.removeFromSuperview()
}
ended up using SMCalloutView # https://github.com/nfarina/calloutview

Resources