Search code examples
swiftgoogle-mapsios9google-maps-sdk-ios

screen not zoom to right coordinates - swift google map api


when getting dynamic google map with using GMSMap the map doesn't show the current location that i gave him and not zoom the map

import UIKit
import GoogleMaps
import MapKit

class LocationItemViewController: UIViewController,GMSMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var lblInfo: UILabel!    
@IBOutlet weak var lblTitle: UILabel!
@IBOutlet weak var viewMap: UIView!
@IBOutlet weak var googleMapView: GMSMapView!


override func viewDidLoad() {

    super.viewDidLoad()     
    let latitude = (String(format:"%.02f", locationItem.itemLatitude ) as NSString).doubleValue
    let longgitude = (String(format:"%.02f", locationItem.itemLongitude) as NSString).doubleValue
    let camera = GMSCameraPosition.cameraWithLatitude(latitude,longitude: longgitude, zoom: 7)

    let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
    mapView.myLocationEnabled = true
    self.googleMapView = mapView
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake((locationItem.itemLatitude as NSString).doubleValue, (locationItem.itemLongitude as NSString).doubleValue)
    marker.title = "somelocation"
    marker.snippet = "anywhere"
    marker.map = mapView
    lblTitle.text = locationItem.itemName
    lblInfo.text = locationItem.itemAddress
}

I was locate the GMSServices.provideAPIKey("YOUR_API_KEY") in app delegate


Solution

  • To display your current location you have to do the same thing which we do for apple maps to access our location.

    In plist you have to add 2 entries and take permission from User

    1. NSLocationWhenInUseUsageDescription
    2. NSLocationAlwaysUsageDescription

    Check this link for more details
    iOS 8 CLLocationManagerDelegate methods are never called

    Now regarding your google maps zoom issue

    var mapView:GMSMapView?
    

    Now in your viewDidLoad

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        GMSServices.provideAPIKey("YOUR_API_KEY")
    
        //Taking HardCoded lat longs for the time being. These lat longs are of New Delhi, India
        let camera = GMSCameraPosition.cameraWithLatitude(28.6139, longitude: 77.2090, zoom: 10)
        mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
        mapView!.myLocationEnabled = true
        self.view = mapView
    
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2DMake(28.6139, 77.2090)
        marker.title = "Delhi"
        marker.snippet = "India"
        marker.map = mapView
    
        //As view takes some time to load so I am calling my zoom function after a delay of 1 second. You can use the zoom function code in viewDidAppear too
        //Also this syntax is the latest Swift 2.2 syntax. If you are using the old Swift version, make the changes accordingly for performSelector method
        self.performSelector(#selector(zoom), withObject: nil, afterDelay: 1.0)
    
    }
    

    Here is the zoom method

    func zoom() {
    
        CATransaction.begin()
        CATransaction.setValue(1, forKey: kCATransactionAnimationDuration)
    
        // It will animate your camera to the specified lat and long
        let camera = GMSCameraPosition.cameraWithLatitude(28.6139, longitude: 77.2090, zoom: 15)
        mapView!.animateToCameraPosition(camera)
    
        CATransaction.commit()
    }