I'm using google maps. Im getting user current location with course(angle) from cllocationmanger. After getting the location I'm adding to the GMSMapview.
Now my problem is car movement and map rotation. I want my map view to behave like googlemaps navigation. What I'm doing is after getting course of location I'm assigning it bearing of GMSMapview and not rorating the car.
But in navigation app they are rotating navigation arrow and also the map changing to north direction when ever there is a turn(left or right).
To achieve that do I need use both course and heading of CLLocationmanager.
here is my code. when ever there is a chnage in location im calling this method.
func ShowcurrentMarker()
{
currentlocationCoordinate = CLLocationCoordinate2DMake(LatestLocation.coordinate.latitude, LatestLocation.coordinate.longitude)
if(CurrentLocationMarker == nil)
{
CurrentLocationMarker = GMSMarker(position:currentlocationCoordinate)
CurrentLocationMarker.map = self.mapView
CurrentLocationMarker.icon = UIImage.init(named:"car_icon")
self.mapView.camera = GMSCameraPosition.camera(withLatitude: LatestLocation.coordinate.latitude, longitude: LatestLocation.coordinate.longitude, zoom: 18, bearing: CLLocationDirection.abs(LatestLocation.course), viewingAngle: 65)
}
else
{
CATransaction.begin()
CATransaction.setAnimationDuration(2)
CurrentLocationMarker.position = currentlocationCoordinate
self.mapView.camera = GMSCameraPosition.camera(withLatitude: LatestLocation.coordinate.latitude, longitude: LatestLocation.coordinate.longitude, zoom: 18, bearing: CLLocationDirection.abs(LatestLocation.course), viewingAngle: 65)
CATransaction.commit()
}
}
Please help me..
Thank you...
Related
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 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;
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.
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)
}
I'm working with a Google Maps View and I want to add a button to the map that when tapped, will move the camera to a specific location. I currently have a button outlet and an action connected to the button.
#IBAction func locationTapped(_ sender: Any) {
print("tapped")
let location = GMSCameraPosition.camera(withLatitude: place.latitude, longitude: place.longitude, zoom: 17.0)
mapView.camera = location
}
place exists but for some reason, the camera will not budge. I've tried different versions of code and looked at the Google Maps documentation but none of the options are producing results. Can anyone tell me what I'm doing wrong?
The GMSMapView class has the following function:
animate(to: GMSCameraPosition)
So in your code sample, instead of doing this:
mapView.camera = location
Try doing this:
mapView.animate(to: location)
Hope this helps!
in Swift3 and Swift4
for moving marker to current position use this:
func myLocationBtnAction(_ sender: UIButton) {
mapView.moveCamera(GMSCameraUpdate.setTarget(CLLocationCoordinate2D(latitude: (mapView.myLocation?.coordinate.latitude)!, longitude: (mapView.myLocation?.coordinate.longitude)!), zoom: 16))
and for a specific location use this:
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lng, zoom: 16)
mapView?.camera = camera
mapView?.animate(to: camera)
and don't forget to extend GMSAutocompleteViewControllerDelegate for current location
Swift 2.3
This code is used for my purpose. In which marker tap event used, which moves camera position of map. Hope you find your solution.
func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
mapView.selectedMarker = marker
var point = mapView.projection.pointForCoordinate(marker.position)
let camera = mapView.projection.coordinateForPoint(point)
let position = GMSCameraUpdate.setTarget(camera)
mapView.animateWithCameraUpdate(position)
return true
}
For Objective-c the method is:
[mapView moveCamera:[GMSCameraUpdate setTarget:<CLLocationCoordinate2DMake>]];
Maybe this is to late but i resolve that problem with adding this:
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500), execute: {
let camera = GMSCameraPosition.camera(withLatitude: lat!, longitude: lon!, zoom: 17.0)
self.mMap.animate(to: camera)
})
You have to wait until map load delegate
func moveMapCamera(at cordinate: CLLocationCoordinate2D, animated: Bool = false) {
let camera = MKMapCamera()
camera.centerCoordinate = cordinate
camera.pitch = 0
camera.altitude = 9000
mapV.setCamera(camera, animated: animated)
}