Hi i am beginner in Ios and in my project i am using Google SDK for getting route when user walking or driving along the road
But i am not able to get current location and showing in my x code console 0.0 and 0.0(latitude and longitude values) please help me some one for getting current location
My code is below:-
#import "ViewController.h"
#interface ViewController ()
{
GMSMapView *_mapView;
NSMutableArray *_coordinates;
LRouteController *_routeController;
GMSPolyline *_polyline;
GMSMarker *_markerStart;
GMSMarker *_markerFinish;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_mapView.settings.myLocationButton = YES;
_mapView.myLocationEnabled = YES;
_mapView.delegate = self;
}
- (void)loadView{
CLLocation *myLocation = _mapView.myLocation;
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude);
marker.title = #"Current Location";
marker.map = _mapView;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:myLocation.coordinate.latitude
longitude:myLocation.coordinate.longitude
zoom:6];
_mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.view = _mapView;
NSLog(#"%f, %f", myLocation.coordinate.latitude, myLocation.coordinate.longitude);
}
#pragma mark - GMSMapViewDelegate
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
_polyline.map = nil;
_markerStart.map = nil;
_markerFinish.map = nil;
[_coordinates addObject:[[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude]];
if ([_coordinates count] > 1)
{
[_routeController getPolylineWithLocations:_coordinates travelMode:TravelModeDriving andCompletitionBlock:^(GMSPolyline *polyline, NSError *error) {
if (error)
{
NSLog(#"%#", error);
}
else if (!polyline)
{
NSLog(#"No route");
[_coordinates removeAllObjects];
}
else
{
_markerStart.position = [[_coordinates objectAtIndex:0] coordinate];
_markerStart.map = _mapView;
_markerFinish.position = [[_coordinates lastObject] coordinate];
_markerFinish.map = _mapView;
_polyline = polyline;
_polyline.strokeWidth = 3;
_polyline.strokeColor = [UIColor blueColor];
_polyline.map = _mapView;
}
}];
}
}
#end
getting the location is an asynchronous process. So you need to 'wait' for the location to be updated -- in this case by observing "myLocation"
observe location
this code only works if _mapView is onscreen
// Listen to the myLocation property of GMSMapView.
[_mapView addObserver:self
forKeyPath:#"myLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
// Ask for My Location data after the map has already been added to the UI.
dispatch_async(dispatch_get_main_queue(), ^{
_mapView.myLocationEnabled = YES;
});
get the location updates:
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if (!_firstLocationUpdate) {
// If the first location update has not yet been recieved, then jump to that
// location.
_firstLocationUpdate = YES;
CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
_mapView.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
zoom:14];
}
}
Related
I'm trying to display my Google Map inside a custom UIView instead of in self.view. Currently, my code works fine if I set it to display in self.view; the map is displayed with all applied styles and specified camera. But the minute I try and display it in a custom UIView (self.mapView), the map displays but it's completely empty (ie. no camera set, no markers, nothing). Any idea why this is?
MapViewController.h
#interface MapViewController : ViewController <CLLocationManagerDelegate, GMSMapViewDelegate>
#property (strong, nonatomic) IBOutlet GMSMapView *mapView;
#property (nonatomic, strong) IBOutlet GMSCameraPosition *camera;
MapViewController.m
-(void)viewDidLoad {
self.camera = [GMSCameraPosition cameraWithLatitude:-33.868
longitude:151.2086
zoom:12];
self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:self.camera];
self.mapView.settings.compassButton = YES;
self.mapView.settings.myLocationButton = YES;
[self.mapView addObserver:self
forKeyPath:#"myLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
[self.view addSubview:_mapView];
NSBundle *mainBundle = [NSBundle mainBundle];
NSURL *styleUrl = [mainBundle URLForResource:#"style" withExtension:#"json"];
NSError *error;
// Set the map style by passing the URL for style.json.
GMSMapStyle *style = [GMSMapStyle styleWithContentsOfFileURL:styleUrl error:&error];
NSLog(#"This is what %#", style);
if (!style) {
NSLog(#"The style definition could not be loaded: %#", error);
} else {
NSLog(#"Style Loaded");
}
self.mapView.mapStyle = style;
dispatch_async(dispatch_get_main_queue(), ^{
self.mapView.myLocationEnabled = YES;
});
}
- (void)dealloc {
[self.mapView removeObserver:self
forKeyPath:#"myLocation"
context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if (!firstLocationUpdate) {
firstLocationUpdate = YES;
CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
self.mapView.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
zoom:14];
}
}
I want to move the marker on basis of lat,long which i'm getting continously I'm doing this like below...
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations (NSArray<CLLocation *> *)locations
{
CLLocationCoordinate2D coor2D = CLLocationCoordinate2DMake(longitude, latitude);
self.marker.position = coor2D;
}
The method is updating lat long but the markers is not moving why...
In info.plist I have added..
Try this, assuming you have set your delegate:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations (NSArray<CLLocation *> *)locations
{
CLLocationCoordinate2D coor2D = CLLocationCoordinate2DMake(longitude, latitude);
self.marker.position = coor2D;
[self.mapView animateToLocation:coord2D.coordinate]
}
If all you're trying to do is update your current location. You should probably just use KVO for that.
#implementation MyLocationViewController {
GMSMapView *mapView_;
BOOL firstLocationUpdate_;
}
- (void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
longitude:151.2086
zoom:12];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.settings.compassButton = YES;
mapView_.settings.myLocationButton = YES;
// Listen to the myLocation property of GMSMapView.
[mapView_ addObserver:self
forKeyPath:#"myLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
self.view = mapView_;
// Ask for My Location data after the map has already been added to the UI.
dispatch_async(dispatch_get_main_queue(), ^{
mapView_.myLocationEnabled = YES;
});
}
- (void)dealloc {
[mapView_ removeObserver:self
forKeyPath:#"myLocation"
context:NULL];
}
#pragma mark - KVO updates
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if (!firstLocationUpdate_) {
// If the first location update has not yet been recieved, then jump to that
// location.
firstLocationUpdate_ = YES;
CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
mapView_.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
zoom:14];
}
}
#end
I am working on iOS app using Google Map SDK. I try to set on the CLLocationManager to get location of my device for 10 seconds and right bottom button to get my location instantly. When it comes to the implementation, the app show no response to initiate the method startUpdatingLocation to get my location. Would you please tell me the way to use locationManager to finish the aim?
The following is my working :
-(bool)isNetworkAvailable
{
SCNetworkReachabilityFlags flags;
SCNetworkReachabilityRef address;
address = SCNetworkReachabilityCreateWithName(NULL, "www.apple.com" );
Boolean success = SCNetworkReachabilityGetFlags(address, &flags);
CFRelease(address);
bool canReach = success
&& !(flags & kSCNetworkReachabilityFlagsConnectionRequired)
&& (flags & kSCNetworkReachabilityFlagsReachable);
return canReach;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// [self getTime];
if (![CLLocationManager locationServicesEnabled]) {
NSLog(#"Please enable location services");
return;
}
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
NSLog(#"Please authorize location services");
return;
}
if([self isNetworkAvailable]){
NSLog(#"connected ");
}else {
NSLog(#"not connected ");
}
CarArray = [[NSMutableArray alloc] init];
GMSMarker *marker = [[GMSMarker alloc] init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
float latitide = [defaults floatForKey:#"lati"];
float longitude = [defaults floatForKey:#"longi"];
NSString *desp = [defaults objectForKey:#"desp"];
NSLog(#"assadsd arrived map");
if(latitide!=0.00&&longitude!=0.00) {
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(latitide, longitude);
marker.position = CLLocationCoordinate2DMake(position.latitude, position.longitude);
camera = [GMSCameraPosition cameraWithLatitude:latitide longitude:longitude zoom:12];
}else{
camera = [GMSCameraPosition cameraWithLatitude:22.2855200 longitude:114.1576900 zoom:12];
marker.position = CLLocationCoordinate2DMake(22.2855200, 114.1576900);
}
if(desp.length > 0 ){
marker.title = desp;
}
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest ;
self.locationManager.distanceFilter = 5.0f;
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
marker.snippet = #"HK";
marker.map = mapView_;
mapView_.mapType = kGMSTypeSatellite;
mapView_.delegate = self;
dispatch_async(dispatch_get_main_queue(), ^{
mapView_.myLocationEnabled = YES;
});
mapView_.settings.compassButton = YES;
mapView_.settings.myLocationButton = YES;
[mapView_ addObserver:self
forKeyPath:#"myLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
self.view = mapView_;
[self.locationManager startUpdatingLocation];
NSLog(#"assadsd configured d map");
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
NSLog(#"Please authorize location services");
return;
}
NSLog(#"CLLocationManager error: %#", error.localizedFailureReason);
NSLog(#"didFailWithError: %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"application_name", nil) message:NSLocalizedString(#"location_error", nil) delegate:nil cancelButtonTitle:NSLocalizedString(#"ok", nil) otherButtonTitles:nil];
[errorAlert show];
return;
}
-(void) handleDoubleTap {
NSLog(#"location double tap ");
}
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
// CLLocationDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations{
// Optional: check error for desired accuracy
CLLocation* location = [locations lastObject];
NSLog(#"location x : %f" , location.coordinate.longitude);
NSLog(#"location y : %f" , location.coordinate.latitude);
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
[manager allowDeferredLocationUpdatesUntilTraveled:CLLocationDistanceMax timeout:10];
if (markera == nil) {
markera = [[GMSMarker alloc] init] ;
markera.position = CLLocationCoordinate2DMake( location.coordinate.latitude , location.coordinate.longitude );
markera.groundAnchor = CGPointMake(0.5f, 0.97f); // Taking into account walker's shadow
markera.map = mapView_;
}else {
markera.position = location.coordinate;
}
GMSCameraUpdate *move = [GMSCameraUpdate setTarget:location.coordinate zoom:17];
[mapView_ animateWithCameraUpdate:move];
}
I've cannot change the camera's value in the Google Maps API. So that it displays the users position as soon as you open the app, using CoreLocation and passing those values into the camera. I've had a look at some tutorials online, but I haven't managed to solve it yet.
I've tried to make a method that observes when the GPS coordinates have been found and then pass them to the camera. But it still shows 0,0.
#implementation HMSBViewController{
CLLocationManager *_locationManager;
}
#synthesize mapView;
- (NSString *)deviceLocation
{
NSString *theLocation = [NSString stringWithFormat:#"latitude: %f longitude: %f", _locationManager.location.coordinate.latitude, _locationManager.location.coordinate.longitude];
return theLocation;
}
- (void)viewDidLoad
{
mapView.delegate = self;
//[mapView addObserver:self forKeyPath:#"myLocation" options:0 context:nil];
//mapView.settings.myLocationButton = YES;
mapView.myLocationEnabled = YES;
_locationManager = [[CLLocationManager alloc] init];
_locationManager.distanceFilter = kCLDistanceFilterNone;
_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; // Setting the accuracy to the best possible
if([_locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) {
[_locationManager requestAlwaysAuthorization];
}
[_locationManager startUpdatingLocation];
}
- (void)viewWillAppear{
[mapView addObserver:self forKeyPath:#"myLocation" options:0 context:nil];
}
- (void)loadView{
CLLocation *myLocation = mapView.myLocation;
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude);
marker.title = #"Current Location";
marker.map = mapView;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:_locationManager.location.coordinate.latitude
longitude:_locationManager.location.coordinate.longitude
zoom:1];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.view = mapView;
NSLog(#"%f, %f", _locationManager.location.coordinate.latitude, _locationManager.location.coordinate.longitude);
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if([keyPath isEqualToString:#"myLocation"]) {
CLLocation *location = [object myLocation];
CLLocationCoordinate2D target = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
[mapView animateToLocation:target];
#try {
[mapView removeObserver:self forKeyPath:#"myLocation"];
} #catch(id exception){
;
}
}
}
Any suggestions would be much appreciated!
You need to set the locationManager delegate and then use the delegate method
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
to retrieve the location. Once you have the location, then call your map function to populate Google
I've been struggling to change the camera's value in the Google Maps API. So that it displays the users position as soon as you open the app, using CoreLocation and passing those values into the camera.
There's some other tutorials online, but nothing that works for me.
If you could help it would be massively appreciated.
#implementation HMSBViewController{
CLLocationManager *_locationManager;
}
- (NSString *)deviceLocation
{
NSString *theLocation = [NSString stringWithFormat:#"latitude: %f longitude: %f", _locationManager.location.coordinate.latitude, _locationManager.location.coordinate.longitude];
return theLocation;
}
- (void)viewDidLoad
{
//mapView.settings.myLocationButton = YES;
mapView.myLocationEnabled = YES;
_locationManager = [[CLLocationManager alloc] init];
_locationManager.distanceFilter = kCLDistanceFilterNone;
_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; // Setting the accuracy to the best possible
if([_locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) {
[_locationManager requestAlwaysAuthorization];
}
[_locationManager startUpdatingLocation];
}
- (void)loadView{
CLLocation *myLocation = mapView.myLocation;
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude);
marker.title = #"Current Location";
marker.map = mapView;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:_locationManager.location.coordinate.latitude
longitude:_locationManager.location.coordinate.longitude
zoom:6];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.view = mapView;
NSLog(#"%f, %f", _locationManager.location.coordinate.latitude, _locationManager.location.coordinate.longitude);
}
When the app runs the coordinates are always set to zero.
Thanks
This happens because Google Maps API only get your current location after it's placed on the controller view. To solve this limitation you should create a notification that tells your controller when the current location is available.
On your viewWillApper add this observer:
[mapView addObserver:self forKeyPath:#"myLocation" options:0 context:nil];
Then add -(void)observeValueForKeyPath:ofObject:change:context: that is called everytime the keyPath value changes. On this method you can now get the current location and animate the map to it so it shows centered in there. At the end of the method you should deregister the observer since if it's not deregistered this method will be called every time the position changes.
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if([keyPath isEqualToString:#"myLocation"]) {
CLLocation *location = [object myLocation];
CLLocationCoordinate2D target = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
[mapView animateToLocation:target];
#try {
[map removeObserver:self forKeyPath:#"myLocation"];
} #catch(id exception){
//do nothing, obviously it wasn't attached because an exception was thrown
}
}
}
The map can take a while to get the current location, but it's usually fast.