Adding marker on Google maps - ios

This may be a repeated question (looked at many answers before writing it), but I am unable to solve it.
I want to simple integrate google maps into my swift app. All is working great except getting that marker to show on the map. I am following google map api docs, but no luck.
Here is the body of the function:
// Do any additional setup after loading the view.
/**Testing Google Maps **/
let camera = GMSCameraPosition.cameraWithLatitude(-33.86,
longitude: 151.20, zoom: 6)
//let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
googleMapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
let position = CLLocationCoordinate2DMake(10, 10)
let marker = GMSMarker(position: position)
marker.title = "Hello World"
marker.map = googleMapView
This is executing in viewDidLoad(). I get the map to show but its not showing any marker.
Thanks for the help.

This code works for me:
var marker = GMSMarker()
marker.postion = CLLocationCoordinate2DMake(10,10)
marker.title ="ENTER TITLE"
marker.snippet ="ENTER SNIPPET"
marker.map = googleMapView
Hope this helps :)

After long trying and researching, got it to work by tweaking one line to get the same map view in the UIView with type GMSMap:
let camera = GMSCameraPosition.cameraWithLatitude(-33.86,
longitude: 151.20, zoom: 6)
googleMapView.camera = camera //This line
googleMapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)

did you set the delegate of mapView in viewDidLoad
mapView.delegate = self
and accept GMSMapViewDelegate protocol in your ViewController like this
class ViewController: UIViewController, GMSMapViewDelegate {
}

Related

Is there a way to have InfoWindow open on App load in Google Maps API?

Currently my app opens a custom infowindow when the MapView delegate calls the didTap protocol. This then calls a function which initializes the needed aspects of the infowindow shown below ...
var tappedOverlay : GMSOverlay?
var customInfoWindow : CustomInfoWindow?
var mapView: GMSMapView!
override func viewDidLoad(){
super.viewDidLoad()
self.customInfoWindow = CustomInfoWindow().loadView()
let camera = GMSCameraPosition.camera(withLatitude: 37, longitude: -77, zoom: 16.0)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.delegate = self
self.view = mapView
//this is where i attempt to force a infoWindow to open
tappedOverlay = overlay
initInfoWindow(overlay: overlay)
}
fun initInfoWindow(overlay: GMSOverlay){
mapView.animate(toLocation: position!)
let point = mapView.projection.point(for: position!)
let newPoint = mapView.projection.coordinate(for: point)
let camera = GMSCameraUpdate.setTarget(newPoint)
mapView.animate(with: camera)
let background = UIColor.infoWindowBackground
customInfoWindow?.layer.backgroundColor = background.cgColor
customInfoWindow?.layer.cornerRadius = 8
customInfoWindow?.center = mapView.projection.point(for: position!)
customInfoWindow?.center.y -= 100
customInfoWindow?.CustomWindowLabel.text = overlay.title
customInfoWindow?.CustomWindowSubLabel.lineBreakMode = .byWordWrapping
customInfoWindow?.CustomWindowSubLabel.numberOfLines = 0
mapView.addSubview(customInfoWindow!)
}
func mapView(_ mapView: GMSMapView, didTap Overlay: GMSOverlay){
initInfoWindow(overlay: overlay)
}
Now I would like to have the app automatically open a certain infowindow as soon as it loads.
When I try to manually display the infoWindow on load by calling the function in viewDidLoad(), I have to comment out the mapView.animate(with: camera) line otherwise the map tiles will never load. Someone pointed out to me that the mapView methods have some 'background' information about the overlay when they are called, so I added the tappedOverlay = overlay line, this gets close in that part of the infoWindow is displayed when the app is loaded. However, this and all other 'regular' infoWindows appear without a background and not centered on the point and cannot be interacted with.
The infoWindow's are a subclass of UIView loaded from a XIB file.
The above code is what I thought would be relevant extracted from a larger code base. Let me know if anything needs to be added!
Any help is appreciated!
Maybe this answer will help someone else. The issue was in the mapView.projection functions, they were returning essentially default values. I'm not entirely sure, but it seems when you click on a marker the mapView changes part of the frame = ( 0 0;) variable in the background, so this lead me to change the mapView initialization.
So instead of ...
let camera = GMSCameraPosition.camera(withLatitude: 37, longitude: -77, zoom: 16.0)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
We need ...
let camera = GMSCameraPosition.camera(withLatitude: LAT_OF_YOUR_INFOWINDOW,
longitude: LONG_OF_YOUR_INFOWINDOW,
zoom: 16.0)
mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)

Google Maps API for iOS - Marker is not displayed

I'm new to coding for iOS and I wanted to have a Map with a path/way on it in my Swift App. After trying to apply an KML Overlay, which didn't work, I decided to use a polyline. But the polyline wasn't desplayed either, so I tried a Marker, exactly following the tutorial at https://developers.google.com/maps/documentation/ios-sdk/marker, but the marker is also not displayed. What am I doing wrong? Here is my code for the Marker:
import UIKit
import GoogleMaps
class MapViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
GMSServices.provideAPIKey("MY_KEY")
let camera = GMSCameraPosition.camera(withLatitude: 48.194857, longitude: 13.955710, zoom: 17)
let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
mapView.mapType = .satellite
mapView.setMinZoom(17, maxZoom: 19)
view = mapView
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: 13.9553836,longitude: 48.1956954)
marker.title = "Hello World"
marker.map = mapView
// Do any additional setup after loading the view.
}
}
Thank you in advance!
I think the code is okay, but the marker is not visible because it is in another part of the world.
Try to exchange latitude and longitude of your marker
marker.position = CLLocationCoordinate2D(latitude: 13.9553836,longitude: 48.1956954)
Or set a lower zoom to your map

Change the camera position of GMS Map View with animation

I have tried to zoom in and zoom out the GMSCamera but failed to perform this with animation. I can have gone through the documentation as well. but nothing helped.
UIView.animate(withDuration: 5.0) {
let zoom = GMSCameraUpdate.zoom(to: 15.0)
self.mapView.animate(toZoom: zoom)
}
The above code does not animate the mapview.
Try the below code. You may update it for latest swift syntax.
mapView.camera = GMSCameraPosition.cameraWithLatitude(58.998400,longitude: 10.035604, zoom: 1)
CATransaction.begin()
CATransaction.setValue(2.0, forKey: kCATransactionAnimationDuration)
let city = GMSCameraPosition.cameraWithLatitude(58.998400,longitude: 10.035604, zoom: 15)
self.mapView.animateToCameraPosition(city)
CATransaction.commit()
You do not need to use UIView.animate on google map as google map has its own method to animate with zoom level.
You just need to call this method.
let lat = 21.78841 //latitude of the location to display on map
let lng = 72.25478 //longitude of the location to display on map
let camera = GMSCameraPosition(target: CLLocationCoordinate2D(latitude: lat, longitude: lng), zoom: 17)
mapView.animate(to: camera)
From Google's Doc.
/** Animates the camera of this map to |cameraPosition|. */
- (void)animateToCameraPosition:(GMSCameraPosition *)cameraPosition;

Why GMSMapView is not set at the appropriate coordinate?

This might be a frequently asked question, but believe it or not, I didn't find an answer that helped me fix my issue.
Nevertheless I tried multiple solutions and I don't understand why sometimes the camera of the GMSMapView object does not initialize at the right position.
I have dragged an UIView in my UIViewController scene from the Interface Builder, set it as GMSMapView object.
Here is my setCamera method :
private func setCamera(lat: Double, long: Double, zoom: Float) {
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: long, zoom: zoom)
mapView.camera = camera
mapView.setMinZoom(5, maxZoom: 15)
}
I tried to call it in viewDidLoad or viewWillAppear it's the same behavior.
1 out of 5 launches (approximately), the camera is not centered at the right coordinate, it feels like the GMSMapView is initialized with random camera.
How can I fix it ?
It's my initialization of GMSMapView:
let camera = GMSCameraPosition.camera(withTarget: myLocation.coordinate,
zoom: mapDefaultZoom)
let mapView = GMSMapView.map(withFrame: view.bounds,
camera: camera)
view.addSubview(mapView)
It's update of the camera position:
let bounds = GMSCoordinateBounds().includingCoordinate(position)
let update = GMSCameraUpdate.fit(bounds,
withPadding: 60)
mapView.animate(with: update)
Hope, it will help you.

Adding a marker to a GMSMapView subview ios

I'm using the Google maps cocoapod and I can't figure out how to add a marker to my map. My map is a in subview. I put it in an array of views in viewDidLoad because for some reason I don't understand it always gets deleted before I can access it in methods outside of viewDidLoad - not ideal, but it was the only way I could figure out to save it. Anyway, I'm trying to add a marker. When I set the map to my main view like this it works fine:
var camera: GMSCameraPosition = GMSCameraPosition.cameraWithLatitude(37.7833, longitude: -122.4167, zoom: 6)
var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
self.view = mapView
var marker = GMSMarker()
marker.position = camera.target
marker.map = mapView
However, when I try to add the marker to the subview I get from the array of subviews like this, it doesn't show up:
var tempMapSubView = self.views[0] as! GMSMapView
var camera: GMSCameraPosition = GMSCameraPosition.cameraWithLatitude(37.7833, longitude: -122.4167, zoom: 6)
var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
tempMapSubView.camera = camera
tempMapSubView = mapView
var marker = GMSMarker()
marker.position = camera.target
marker.map = tempMapSubView
The array of subviews solution works really well with all the other stuff I'm trying to do. Thanks for your help!
Your code is getting the map from self.views[0], then creating a new map, then adding a marker to that new map. Then you do nothing with the new map, so it won't be displayed. Nothing has been added to the original map, and so you don't see the marker.
I think you need something like this:
var tempMapSubView = self.views[0] as! GMSMapView
var position = CLLocationCoordinate2DMake(37.7833, -122.4167)
var marker = GMSMarker()
marker.position = position
marker.map = tempMapSubView
Are you using google maps to get your device sensor via IP or GPS? If So it should automatically place a marker. You can then pass the marker coordinates to a string.

Resources