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.
Related
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)
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
i Have a car icon in google map and which have to be moved in regular time interval. the location coordinates will be fetched from server and i have managed to change the location of marker by doing the code below but didint got a smooth movement
let camera = GMSCameraPosition.camera(withLatitude: (((self.driverArrival.value(forKey: "latitude")) as AnyObject).doubleValue)!,
longitude: (((self.driverArrival.value(forKey: "longitude")) as AnyObject).doubleValue)!, zoom: 15)
let position = CLLocationCoordinate2DMake((((self.driverArrival.value(forKey: "latitude")) as AnyObject).doubleValue)!, (((self.driverArrival.value(forKey: "longitude")) as AnyObject).doubleValue)!)
driverMarker.position = position
driverMarker.map = self.mapView
here driverdetails contains all the required data. Just want to know i can use any animation functions to achieve this?
You can use animate(to: GMSCameraPosition) to update map position with animation an example will look like this :-
func updateMapLocation(lattitude:CLLocationDegrees,longitude:CLLocationDegrees){
let camera = GMSCameraPosition.camera(withLatitude: lattitude, longitude: longitude, zoom: 16)
mapView?.camera = camera
mapView?.animate(to: camera)
}
and call the method like this
updateMapLocation(lattitude:-33.8683,longitude:151.2086)
For more information
Edit
For marker position update you can use a single marker and update its position with this code
CATransaction.begin()
CATransaction.setAnimationDuration(2.0)
marker.position = coordindates // CLLocationCoordinate2D coordinate
CATransaction.commit()
Please Don't use GMSCameraPosition for move pin in the map
You can use mapView.animate(toLocation: YOUR_COORDINATES) method to smoothly move pin in the map
self.mapView.animate(toLocation: CLLocationCoordinate2D(latitude: YOUR_LATITUDE, longitude: YOUR_LONGITUDE))
self.marker.position = coordinate
self.marker.map = self.mapView
Hope this helps!
Try this ...
Add GMSMapViewDelegate to your class,
self.mapView.delegate = self //Call delegate
//MARK - MapView delegates
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
self.marker.map = mapView;
self.marker.position = position.target //Your target position
self.mapView.selectedMarker = self.marker //Your marker
DispatchQueue.main.async {
self.marker.snippet = "Getting address... " //Your snippet title
}
}
func mapView(_ mapView:GMSMapView, idleAt position:GMSCameraPosition) {
//Get address with village names and street names
self.getAddressForLatLng(latitude: "\(position.target.latitude)", longitude: "\(position.target.longitude)", zoomLevel: position.zoom)
}
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 {
}
I'm trying to set up a map search using the google maps sdk in ios. So far, I have the autocompleted search working correctly and when I search, the results show up in a tableview. I want to be able to click on a cell in the table view and reveal the map, then show the table again when the user touches back into the search box. My strategy to do this is to layer the mapview and the table view in the same view Controller, and show/hide or push whichever view to the top. However, because the google map is not part of mapkit, I'm not sure that I can manipulate it like this. Please let me know if you know an easy way to do this. Setting the view to tableview also results in a black screen
mapView.hidden just reveals a black screen
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
view.bringSubviewToFront(self.tableView)
placesClient = GMSPlacesClient()
var camera = GMSCameraPosition.cameraWithLatitude(-33.86,
longitude: 151.20, zoom: 6)
var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
//mapView.hidden = true
mapView.myLocationEnabled = true
self.view = mapView
self.view = self.tableView
var marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView
}
I am using the google maps cocoapod
This depends on the order which they have in the view controller's list of subviews. If mapView and tableView are both direct subviews of the root view, you can just call insertSubview:belowSubview:
[self.view insertSubview:mapView belowSubview:tableView];
and vice versa.
I found out that I had to add my own subview, change the class of that subview to GMSMapview and assign the map to that subview.
var camera = GMSCameraPosition.cameraWithLatitude(-33.86,
longitude: 151.20, zoom: 6)
var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
self.mapSubView.camera = camera
self.mapSubView = mapView