So I have a custom info window that appears whenever a user taps a pin in Google Maps. However, I want the info window to appear at the bottom of the screen when the user taps a pin and not above the pin as is default. Basically, when you tap the pin, the map centers on the pin as it does normally but I want the info window to appear at the very bottom of the screen, above the Google Maps logo. I'm currently using this function to show the custom info window:
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) ->
UIView? {
let infoWindow = Bundle.main.loadNibNamed("customInfoWindow", owner: self, options: nil)?.first! as! customInfoWindow
infoWindow.title.text = marker.title
return infoWindow
}
According to the post above, i think you need to add a custom Label rather that the default info window. Implement the below delegate method.
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
// Add a Label on the Map at the desired position,
// get the coordinates of the marker from 'marker.position'
// use the coordinates for displaying it in the label or rev geo to get the address and display (according to the required spec.)
}
Related
I posted another question with the same code, but this question is different.
I want to add buttons in the bottom right corner of my Speech bubble that displays
Hello World!
Welcome to my marker!
I want to know how to place the buttons there, but if you want to know what the buttons would do, one of them would keep track of how many upvotes the bubble got by other users, and the other would send a request to another user.
Also, I found this example that looks like it implements a different version of a speech bubble(popup) that may be better to use
import Mapbox
class ViewController: UIViewController, MGLMapViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Set the map’s center coordinate and zoom level.
mapView.setCenter(CLLocationCoordinate2D(latitude: 40.7326808, longitude: -73.9843407), zoomLevel: 12, animated: false)
view.addSubview(mapView)
// Set the delegate property of our map view to `self` after instantiating it.
mapView.delegate = self
// Declare the marker `hello` and set its coordinates, title, and subtitle.
let hello = MGLPointAnnotation()
hello.coordinate = CLLocationCoordinate2D(latitude: 40.7326808, longitude: -73.9843407)
hello.title = "Hello world!"
hello.subtitle = "Welcome to my marker"
// Add marker `hello` to the map.
mapView.addAnnotation(hello)
}
// Use the default marker. See also: our view annotation or custom marker examples.
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
return nil
}
// Allow callout view to appear when an annotation is tapped.
func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
return true
}
}
Below is what I would like my expected output to look like approximately
If you want to use the built-in Mapbox callout you may want to consider implementing the -mapView:rightCalloutAccessoryViewForAnnotation:
delegate method that allows you to further customize an MGLCallout as illustrated in this example: https://www.mapbox.com/ios-sdk/maps/examples/default-callout/. That delegate method returns a UIView so you can customize the UIView however you'd like to contain the buttons you want.
You'll notice in the example that another delegate method, -mapView:annotation:calloutAccessoryControlTapped: is also implemented. This gets called when the right callout accessory view (returned by -mapView:rightCalloutAccessoryViewForAnnotation:) is selected, so you could adapt this by placing your logic in that delegate method when a user selects the right side of the callout view.
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
I am using google maps SDK for ios. I want to make a marker fixed in the center of the screen, so when user drags the map, the marker does not move and stays in the center. I am also trying to read the coordinate of the center after the drag. Any input would be appreciated. Thanks!
I would rather display view on top of GMSMapView (do not use markers for that). Since you have screen position for map view, that should be easy to place your view in correct position.
To get coordinates you can use mapView.projection.coordinateForPoint
Documentation is here
To know that drag is finished, make you view controller (or any other object) delegate of map view (GMSMapViewDelegate) and implement mapView:idleAtCameraPosition: method.
Docs are here
Create outlet of GMSMapView and attached Image at the center of the map and search bar at the top to present the location name.
Drag the map on Device, which will trigger 2 delegate method of GMSMapViewDelegate.
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition)
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition)
/**
* Called repeatedly during any animations or gestures on the map (or once, if the camera is
* explicitly set). This may not be called for all intermediate camera positions. It is always
* called for the final position of an animation or gesture.
*/
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
print("didchange")> >
returnPostionOfMapView(mapView: mapView)
}
/**
* Called when the map becomes idle, after any outstanding gestures or animations have completed (or
* after the camera has been explicitly set).
*/
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
print("idleAt")
//called when the map is idle
returnPostionOfMapView(mapView: mapView)
}
//Convert the location position to address
func returnPostionOfMapView(mapView:GMSMapView){
let geocoder = GMSGeocoder()
let latitute = mapView.camera.target.latitude
let longitude = mapView.camera.target.longitude
let position = CLLocationCoordinate2DMake(latitute, longitude)
geocoder.reverseGeocodeCoordinate(position) { response , error in
if error != nil {
print("GMSReverseGeocode Error: \(String(describing: error?.localizedDescription))")
}else {
let result = response?.results()?.first
let address = result?.lines?.reduce("") { $0 == "" ? $1 : $0 + ", " + $1 }
self.searchBar.text = address
}
}
}
Github Demo Project
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
Ok it is a long title. I am having trouble when i use google maps iOS sdk. I want to show an info window about a marker which user tapped. According the documentation if snippet and title properties of GMSMarker are both selected info window will be shown when user tapped that marker. But I also implement mapView:didTapMarker: method from GMSMapViewDelegate protocol. If I comment out that method info window is visible otherwise info window is not visible. So how can I show info window when that method implemented?
Implement GMSMapViewDelegate's mapView:didTapMarker: method and make it return false.
Swift Implementation:
func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {
return false
}
For those interested - Swift 5.5 ios 15
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
// return false - show marker info, return true - not show
return false
}