I am working on Google Map Module , using 1.9.0 Bundle, to find my location on regular basis , say 10 seconds and create a button to get my location button as one of mapview settings
When it comes to the execution and location service is turned on, there are no responses after clicking the right bottom button or not, the location manager is not working. I can show any signs of using location service.
I am not sure what I am missing on setting this map as i follows strictly on the SDKDemos - ios google demo project to set my module. Would you please tell me the exact way to set it ?
The following is my code:
@implementation MapViewController
-(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)requestAlwaysAuthorization
{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
// If the status is denied or only granted for when in use, display an alert
if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusDenied) {
NSString *title;
title = (status == kCLAuthorizationStatusDenied) ? @"Location services are off" : @"Background location is not enabled";
NSString *message = @"To use background location you must turn on 'Always' in the Location Services Settings";
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Settings", nil];
[alertView show];
}
// The user has not enabled any location services. Request background authorization.
else if (status == kCLAuthorizationStatusNotDetermined) {
[locationManager requestAlwaysAuthorization];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
if([self isNetworkAvailable]){
NSLog(@"connected ");
}else {
NSLog(@"not connected ");
}
CarArray = [[NSMutableArray alloc] init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
float latitide = [defaults floatForKey:@"lati"];
float longitude = [defaults floatForKey:@"longi"];
NSString *desp = [defaults objectForKey:@"desp"];
GMSMarker *marker = [[GMSMarker alloc] init];
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);
}
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
marker.snippet = @"HK";
mapView_.mapType = kGMSTypeSatellite;
mapView_.delegate = self;
mapView_.settings.myLocationButton = YES;
mapView_.settings.compassButton = YES;
dispatch_async(dispatch_get_main_queue(), ^{
mapView_.myLocationEnabled = YES;
});
if(desp.length > 0 ){
marker.title = desp;
}
// Setup location services
if (![CLLocationManager locationServicesEnabled]) {
NSLog(@"Please enable location services");
return;
}
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
NSLog(@"Please authorize location services");
return;
}
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = 5.0f;
[mapView_ addObserver:self
forKeyPath:@"myLocation"
options:(NSKeyValueObservingOptionNew |
NSKeyValueObservingOptionOld)
context:NULL];
marker.map = mapView_;
self.view = mapView_;
[locationManager startUpdatingLocation];
GMSCircle *geoFenceCircle = [GMSCircle circleWithPosition: CLLocationCoordinate2DMake(22.2855200, 114.1576900) radius:1400];
geoFenceCircle.tappable = true;
[geoFenceCircle setFillColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:.5]];
geoFenceCircle.strokeWidth = 13;
geoFenceCircle.strokeColor = [UIColor orangeColor];
geoFenceCircle.map = mapView_; // Add it to the map.
NSLog(@"assadsd configured d map");
}
- (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];
}
}
- (void)dealloc {
[mapView_ removeObserver:self
forKeyPath:@"myLocation"
context:NULL];
}
-(void)mapView:(GMSMapView *)mapView didDragMarker:(GMSMarker *)marker {
[mapView clear];
// Re-draw marker
marker.map = mapView;
// Create your circle with the new marker
GMSCircle *circ = [GMSCircle circleWithPosition:marker.position radius:1000];
circ.fillColor = [UIColor grayColor];
circ.strokeColor = [UIColor redColor];
circ.strokeWidth = 5;
circ.map = mapView;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"buttonIndex:%ld",(long)buttonIndex);
if (alertView.tag == 121 && buttonIndex == 1)
{
//code for opening settings app in iOS 8
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"%@",error.userInfo);
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
NSLog(@"Please authorize location services");
return;
}
NSLog(@"CLLocationManager error: %@", error.localizedFailureReason);
return;
}
-(void) handleDoubleTap {
NSLog(@"location double tap ");
}
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
// CLLocationDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations{
CLLocation *location = [locations lastObject];
if (markera == nil) {
markera = [[GMSMarker alloc] init] ;
markera.position = CLLocationCoordinate2DMake(22.86, 111.20);
markera.groundAnchor = CGPointMake(0.5f, 0.97f); // Taking into account walker's shadow
markera.map = mapView_;
}else {
[CATransaction begin];
[CATransaction setAnimationDuration:2.0];
markera.position = location.coordinate;
[CATransaction commit];
}
GMSCameraUpdate *move = [GMSCameraUpdate setTarget:location.coordinate zoom:17];
[mapView_ animateWithCameraUpdate:move];
}
I have already set the following parameters of pinfo.list in my project
NSLocationAlwaysUsageDescription -> Location is required to find out where you are
NSLocationWhenInUseUsageDescription -> Location is required to find out where you are
My testing device is 8.3 , iphone 6
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…