GMS Map View always showing Europe - ios

I'm trying to use Google Maps SDK to display a map of my current location (Berkeley, CA) as the focus, but regardless of what lat/lon I put in, it always shows Europe, as shown below:
Here is the code for the map view:
let camera = GMSCameraPosition.cameraWithLatitude(37.8750360, longitude: -122.2573240, zoom: 1)
mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
mapView.delegate = self
I'm doing very similar things in other parts of my application, but have never gotten an issue. If someone knows how to fix this, please let me know! Thanks

Please make sure that you are test on real device. not be Simulator,
If you use Simulator than enter latitude and longitude manually.
Like this-

You can test is on simulator as well and you can change location without enter lat-long
for that you need to change location from your Xcode (above console Please rifler the below screen shot
)

Maybe this is because you are setting the mapView's frame to CGRectZero. This may cause the map to have zero height and zero width.
Example based from this tutorial:
override func viewDidLoad()
{
let camera: GMSCameraPosition = GMSCameraPosition.cameraWithLatitude(37.8750360, longitude: -122.2573240, zoom: 1)
mapView = GMSMapView.mapWithFrame(self.view.frame, camera: camera)
mapView.camera = camera
}
Check these related questions:
Current Location in Google Maps with swift
Show GoogleMaps on View in Swift?

You're setting up your view before even setting your delegate (so your code isn't even attached to your instance of mapView). Fix it like this:
1: Make sure you've defined your GMSMapViewDelegate for the class
2: Set your delegates first
mapView.delegate = self
mapView.myLocationEnabled = true
let camera = GMSCameraPosition.cameraWithLatitude(37.8750360, longitude: -122.2573240, zoom: 1)
mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)

Related

How to remove default marker from GMSMapView

I used GMSMapView for iOS app. I want to remove default marker/annotation from GMSMapView but it's not removed. I use maptype property to remove it which is working perfect in Android but in iOS it's not working. Let me share code sample here.
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: long, zoom: zoom)
mapView = GMSMapView.map(withFrame: rect, camera: camera)
mapView.mapType = .terrain //<--- it's not working somehow in code i have tried all options.
Is there any other way to remove all default marker/annotation from GMSMapView in iOS?
Let me attached image for visibility.
screen shot of issue
Please Refer this ans i have tested its working
iOS Google Maps SDK - Hide / remove labels
first add myStyle.json file in your project and then call the do catch
You need to customize the map's style and you can do that with a JSON file in the app's bundle. Those markers are POI markers and you can remove them entirely with the following:
[
{
"featureType": "poi",
"stylers": [
{
"visibility": "off"
}
]
}
]
JSON generator: https://mapstyle.withgoogle.com/
API reference: https://developers.google.com/maps/documentation/ios-sdk/style-reference

(iOS Swift)Google Maps animateWithCameraUpdate not working

I am working with Google maps in iOS. I wanted to animate my Camera such that it shows all the required Lat, Lngs. I am storing those locations in the array(as CLLocationCoordinate2D instances) and using GMSCoordinateBounds class to add these locations. This is my code:
let bounds = GMSCoordinateBounds()
bounds.includingCoordinate(firstLocation)
bounds.includingCoordinate(lastLocation)
let camUpdate = GMSCameraUpdate.fit(bounds, withPadding: 60)
mMapView.camera = GMSCameraPosition.camera(withLatitude: firstLocation.latitude, longitude: firstLocation.longitude, zoom: 18)
mMapView.animate(with: camUpdate)
firstLocation and lastLocation have correct values but the camera is not animating. Am I missing something?
Very silly mistake. includingCoordinates function returns GMSCoordinateBounds object. I had to set to itself again for it to work like so:
bounds = bounds.includingCoordinate(firstLocation)
bounds = bounds.includingCoordinate(lastLocation)
If i set like above, it is working fine.

GMSMarker icon in the top left corner of the view (iOS)

I am trying to create a UITableViewCell containing a GMSMapView with a GMSMarker at the center of the current Position.
The problem is that the marker always appears at the top left corner of the current position and I don't know how to solve the problem.
I tried to follow these steps: Implementing a Google Map with UItableviewCell
here is my code from cellForRowAt:
let locationCell = tableView.dequeueReusableCell(withIdentifier: "activityLocationCell") as! ActivityLocationCell
let latitude = CLLocationDegrees(activity.coordinates![0])
let longitude = CLLocationDegrees(activity.coordinates![1])
let position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
locationCell.googleMapView.camera = GMSCameraPosition.camera(withTarget: position, zoom: 15)
let marker = GMSMarker(position: position)
marker.groundAnchor = CGPoint(x: 0.5, y: 0.5)
marker.map = locationCell.googleMapView
return locationCell
Here is a screenshot of my problem:
marker is at the top left corner of the map
I had a pretty similar issue. I resolved it by changing the moment I configure the map in the view lifecycle.
In my case, I was using a child view controller. I was configuring the map before viewWillAppear was called which caused the map to not center properly (the marker was on the top left corner). I moved my call to after the viewWillAppear and it fixed it. A good place would be viewDidAppear.
If you are using a cell, you will probably need to investigate with the view lifecycle instead of the controller lifecycle.
This is not written anywhere on the Google documentation.
you have to draw map in
func viewDidLayoutSubviews()
Try creating Marker when map is ready completely. for eg: use the delegate.
var ifMapReady: Bool = false
...
...
func mapViewSnapshotReady(_ mapView: GMSMapView) {
ifMapReady = true
}
//Call this method from where ever you want to load map
func updateMap() {
if ifMapReady {
//Load Map
}
}
This delegate will be called multiple times(eg: map is swiped or moved etc) whenever the Map tiles are ready. So we can use a boolean value for understanding that map loaded successfully. Based on that value we can load the Map properly when initiating.
I want to add one more thing. #Gabriel Cartier's answer worked for me with one additional change in my code.
[self->mapView_ animateToCameraPosition:camera];
And I replaced with
[self->mapView_ setCamera:camera];

Google Maps iOS SDK Prevent camera zoom after each location update

I have an app which requires the user's location to be constantly updated so I can display their current coordinates and altitude. I'm doing this using the didUpdateLocations function:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
mapView.camera = GMSCameraPosition(target: locationManager.location!.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
let locValue : CLLocationCoordinate2D = manager.location!.coordinate
let altitude : CLLocationDistance = Double(round(1000*manager.location!.altitude)/1000)
let long = Double(round(10000*locValue.longitude)/10000)
let lat = Double(round(10000*locValue.latitude)/10000)
let alt = String(altitude) + " m"
latitudeLabel.text = String(lat)
longitudeLabel.text = String(long)
altitudeLabel.text = alt
showLearningObjectsWithinRange(location)
}
}
The problem is, when I try to zoom in on a certain spot on the map, if I move the device even slightly the camera zooms back out again. Obviously this is because of the first line in my didUpdateLocations function setting the camera position, but if I remove that line, the map doesn't center to their location at all.
I tried moving the GMSCameraPosition code to the viewDidLoad, viewWillAppear, and several other places, but this caused the app to crash because it couldn't locate the user in time.
Does anyone have any suggestions on how to make this work? Thanks.
use this instead of that certain line (mapview.camera = ...)
mapView.animate(toLocation: CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude))
With regards to implementing location update, there was an issue posted in GitHub - Implement Responsive User Location Tracking Mode due to location update optimization and going through the thread, a given workaround to show the user location on the map is to call
nMapView.setMyLocationEnabled(true);
instead of:
nMapView.setMyLocationTrackingMode(MyLocationTrackingMode.TRACKING_FOLLOW);
Then, with regards to camera zoom, as discussed in Camera and View - Zoom, you can try setting a minimum or maximum zoom to restrict zoom level. As stated,
You can restrict the range of zoom available to the map by setting a min and max zoom level.
You may also try the solutions given in this SO post - Google Maps iOS SDK, Getting Current Location of user. Hope it helps.

Custom user location dot in Google maps for iOS (GMSMapview)

Is there an official way to set a custom user location dot in Google maps for iOS (GMSMapView)?
Is there a known way to "hack" it? Like iterating through all subviews and layers and fish the blue dot?
Even if you can't customise its appearance, can you control its z order index? When you have many markers, the little blue dot becomes hidden, and sometimes you want it to be visible at all times.
Thanks
You can try to find the image on:
GoogleMaps.framework > Resources > GoogleMaps.bundle
OR
GoogleMaps.framework > Resources > GoogleMaps.bundle > GMSCoreResources.bundle
I did a quick search on those and the only associated file I found with that blue dot is GMSSprites-0-1x.
Please read the google maps terms and conditions because this might not be legal.
You can set the maps myLocationEnabled to NO. That will hide the default location dot. Then use an instance of CLLocationManager to give you your position. Inside CLLocationManager didUpdateLocations method you can set a custom GMSMarker. Set its icon property to whatever you want your dot to look like using [UIImage imageNamed:]. This will allow you to achieve the desired effect.
Swift 4
Disable the default Google Map current location marker (it's disabled by default):
mapView.isMyLocationEnabled = false
Create a marker as an instance property of the view controller (because a delegate will need access to this):
let currentLocationMarker = GMSMarker()
The GMSMarker initializer allows for a UIImage or a UIView as a custom graphic, not a UIImageView unfortunately. If you want more control over the graphic, use a UIView. In your loadView or viewDidLoad (wherever you configured the map), configure the marker and add it to the map:
// configure custom view
let currentLocationMarkerView = UIView()
currentLocationMarkerView.frame.size = CGSize(width: 40, height: 40)
currentLocationMarkerView.layer.cornerRadius = 40 / 4
currentLocationMarkerView.clipsToBounds = true
let currentLocationMarkerImageView = UIImageView(frame: currentLocationMarkerView.bounds)
currentLocationMarkerImageView.contentMode = .scaleAspectFill
currentLocationMarkerImageView.image = UIImage(named: "masterAvatar")
currentLocationMarkerView.addSubview(currentLocationMarkerImageView)
// add custom view to marker
currentLocationMarker.iconView = currentLocationMarkerView
// add marker to map
currentLocationMarker.map = mapView
All that remains is giving the marker a coordinate (initially and every time the user's location changes), which you do through the CLLocationManagerDelegate delegate.
extension MapViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let lastLocation = locations.last!
// update current location marker
currentLocationMarker.position = CLLocationCoordinate2D(latitude: lastLocation.coordinate.latitude, longitude: lastLocation.coordinate.longitude)
}
}
The first few locations that the location manager produces may not be very accurate (although sometimes it is), so expect your custom marker to jump around a bit at first. You can wait until the location manager has gathered a few coordinates before applying it to your custom marker by waiting until locations.count > someNumber but I don't find this approach very attractive.

Resources