In Objective-C:
Take the location GMSMarker as global variable and update its position whenver you get new location coordinates.
@interface YourViewController ()
{
GMSMarker *marker;
}
- (void)viewDidLoad
{
[super viewDidLoad];
/*Your code here which includes map initialisation too.*/
// After map initialisation
marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(lat,lng);
marker.title = @"Your location";
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.map = myMapView;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
currLocation = [locations firstObject];
marker.position = CLLocationCoordinate2DMake(currLocation.latitude,currLocation.longitude);
}
For rotation, I've faced the same problem too, as its not rotating as expected. So I've took an UIImageView instead of GMSMarker and updating its center and rotating it according the angle between to previous and current location coordinates.
for more info please check this Link
Or here is the required code:
#import "SampleViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <GoogleMaps/GoogleMaps.h>
#import <QuartzCore/QuartzCore.h>
#import <CoreMotion/CoreMotion.h>
@interface SampleViewController ()<CLLocationManagerDelegate,GMSMapViewDelegate>
{
GMSMapView *myMapView;
CLLocationCoordinate2D previousSourcePoint;
CLLocation *prevCurrLocation, *currLocation;
//sample
UIImageView *sampleImgView;
}
@property CLLocationManager *locatnManager;
@end
@implementation SampleViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
prevCurrLocation = [[CLLocation alloc]initWithLatitude:0.0 longitude:0.0];
//GMS MapView
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:23.2500 longitude:77.4170 zoom:14.0];
CGRect mapRect;
//sample frames of mapview
if ([UIScreen mainScreen].bounds.size.height==736)// iPhone 6 Plus 414*736
{
mapRect = CGRectMake(0, 0, 414, 736-66);
}
else if ([UIScreen mainScreen].bounds.size.height==667)// iphone 6 375*667
{
mapRect = CGRectMake(0, 0, 375, 667-66);
}
else if ([UIScreen mainScreen].bounds.size.height==568)// iphone 5, 5C, 5S 320*568
{
mapRect = CGRectMake(0, 0, 320, 568-66);
}
else if ([UIScreen mainScreen].bounds.size.height==480) // iphone 4, 4S 320*480
{
mapRect = CGRectMake(0, 0, 320, 480-66);
}
else{
mapRect = CGRectMake(60, 66, [UIScreen mainScreen].bounds.size.width + 100, [UIScreen mainScreen].bounds.size.height + 130);
}
myMapView = [GMSMapView mapWithFrame:mapRect camera:camera];
myMapView.myLocationEnabled = NO;
myMapView.delegate = self;
[myMapView.settings setAllGesturesEnabled:NO];
[myMapView.settings setZoomGestures:YES];
[myMapView.settings setScrollGestures:YES];
//Compass
myMapView.settings.compassButton = YES;
myMapView.padding = UIEdgeInsetsMake (40, 0, 0, 0);
[myMapView setMinZoom:0.0 maxZoom:20.0];
for (id gestureRecognizer in myMapView.gestureRecognizers)
{
if (![gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
{
[myMapView removeGestureRecognizer:gestureRecognizer];
}
if (![gestureRecognizer isKindOfClass:[UIRotationGestureRecognizer class]])
{
[myMapView removeGestureRecognizer:gestureRecognizer];
}
}
[self.view addSubview:myMapView];
myMapView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
previousSourcePoint = CLLocationCoordinate2DMake(0.0, 0.0);
sampleImgView = [[UIImageView alloc]init];
sampleImgView.center = myMapView.center;
sampleImgView.backgroundColor = [UIColor clearColor];
[myMapView addSubview:sampleImgView];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.8f];
sampleImgView.transform = CGAffineTransformMakeRotation(45);
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
sampleImgView.transform = CGAffineTransformMakeRotation(0);
[UIView commitAnimations];
CGPoint ImgCenter = sampleImgView.center;
CGRect frameRect = sampleImgView.frame;
frameRect.size.width = 20.0f;
frameRect.size.height = 30.0f;
sampleImgView.image = [UIImage imageNamed:@"CAR"]; // place your own vehicle image
sampleImgView.center = ImgCenter;
sampleImgView.frame = frameRect;
if ([CLLocationManager headingAvailable]) {
[self locationManager];
} else {
[[[UIAlertView alloc] initWithTitle:@"Error"
message:@"Device doesn't support heading updates."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
}
#pragma mark - above are test gms marker rotation
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
myMapView.settings.zoomGestures = YES;
}
-(void)viewDidUnload{
[super viewDidUnload];
@try {
[_locatnManager stopUpdatingLocation];
[self.locationManager stopUpdatingLocation];
}
@catch (NSException *exception) {
}
@finally {
//Display Alternative
}
}
#pragma mark CLLocationManagerDelegate methods
-(CLLocationManager *)locationManager {
if (_locatnManager == nil) {
_locatnManager = [[CLLocationManager alloc] init];
_locatnManager.desiredAccuracy = kCLLocationAccuracyBest;
_locatnManager.delegate = self;
// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([_locatnManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[_locatnManager requestWhenInUseAuthorization];
}
// Start the location updates
if ([CLLocationManager locationServicesEnabled]) {
_locatnManager.distanceFilter = kCLDistanceFilterNone;
[_locatnManager startUpdatingLocation];
}
}
return _locatnManager;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
currLocation = [locations firstObject];
CGPoint anglepoint = [myMapView.projection pointForCoordinate:currLocation.coordinate];
CGFloat angle = [self getAngle:anglepoint];
if(!isnan(angle)){
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.8f];
sampleImgView.transform = CGAffineTransformMakeRotation(angle);
[sampleImgView setCenter:[myMapView.projection pointForCoordinate:CLLocationCoordinate2DMake(currLocation.coordinate.latitude, currLocation.coordinate.longitude)]];
[UIView commitAnimations];
prevCurrLocation = currLocation;
}
else{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.8f];
[sampleImgView setCenter:[myMapView.projection pointForCoordinate:CLLocationCoordinate2DMake(currLocation.coordinate.latitude, currLocation.coordinate.longitude)]];
[UIView commitAnimations];
prevCurrLocation = currLocation;
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if ([error code] != kCLErrorLocationUnknown) {
[self.locationManager stopUpdatingLocation];
self.locationManager.delegate = nil;
}
}
-(CGFloat) getAngle: (CGPoint) touchedPoints
{
CGPoint previousLocationPoint = [myMapView.projection pointForCoordinate:prevCurrLocation.coordinate];
CGFloat x1 = previousLocationPoint.x;
CGFloat y1 = previousLocationPoint.y;
CGFloat x2 = touchedPoints.x;
CGFloat y2 = touchedPoints.y;
CGFloat x3 = x1;
CGFloat y3 = y2;
CGFloat oppSide = sqrtf(((x2-x3)*(x2-x3)) + ((y2-y3)*(y2-y3)));
CGFloat adjSide = sqrtf(((x1-x3)*(x1-x3)) + ((y1-y3)*(y1-y3)));
CGFloat angle = atanf(oppSide/adjSide);
// Quadrant Identifiaction
if(x2 < previousLocationPoint.x)
{
angle = 0-angle;
}
if(y2 > previousLocationPoint.y)
{
angle = M_PI/2 + (M_PI/2 -angle);
}
return angle;
}
- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position{
[sampleImgView setCenter:[myMapView.projection pointForCoordinate:CLLocationCoordinate2DMake(currLocation.coordinate.latitude, currLocation.coordinate.longitude)]];
}
I am new to swift but I've managed to get required output.
In Swift:
import UIKit
import GoogleMaps
import Darwin
var locManager = CLLocationManager()
var prevCurrLocation = CLLocation()
var currLocation = CLLocation()
//sample
var sampleImgView = UIImageView();
var previousSourcePoint = CLLocationCoordinate2D()
let map = GMSMapView(frame: CGRect.zero )
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude:151.20, zoom: 18)
class ViewController: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func loadView() {
// Create a GMSCameraPosition that tells the map to display the
prevCurrLocation = CLLocation.init(latitude: 0.0, longitude: 0.0)
previousSourcePoint = CLLocationCoordinate2DMake(0.0, 0.0)
map.delegate = self
map.camera = camera
self.view = map
map.isMyLocationEnabled = true
map.settings.myLocationButton = true
locManager = CLLocationManager()
locManager.delegate = self
locManager.desiredAccuracy = kCLLocationAccuracyBest
locManager.startUpdatingLocation()
let image : UIImage = UIImage(named:"Bus")!
sampleImgView = UIImageView(image: image)
sampleImgView.center = map.center
sampleImgView.backgroundColor = UIColor.clear
map.addSubview(sampleImgView)
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(0.3)
sampleImgView.transform = CGAffineTransform(rotationAngle: 0)
UIView.commitAnimations()
let ImgCenter = CGPoint(x:sampleImgView.center.x, y:sampleImgView.center.y)
var frameRect = CGRect(origin: CGPoint(x:sampleImgView.frame.origin.x, y:sampleImgView.frame.origin.y), size: CGSize(width: sampleImgView.frame.size.width, height: sampleImgView.frame.size.height))
frameRect.size.width = 20.0
frameRect.size.height = 30.0
sampleImgView.center = ImgCenter;
sampleImgView.frame = frameRect;
}
/*Google Maps Delegate Methods*/
f