我正在使用1.9.0 Bundle在Google地图模块上工作,定期查找我的位置,比如说10秒并创建一个按钮,将我的位置按钮作为mapview设置之一
当执行和位置服务已打开时,单击右下方按钮后没有响应,位置管理器无法正常工作。我可以显示使用位置服务的任何迹象。
我不确定在设置此地图时我缺少什么,因为我严格遵循SDKDemos - ios google演示项目来设置我的模块。你能告诉我确切的设置方法吗?
以下是我的代码:
@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];
}
我已经在项目中设置了pinfo.list的以下参数
NSLocationAlwaysUsageDescription - >需要位置以了解您的位置 NSLocationWhenInUseUsageDescription - >需要位置才能找到你的位置
我的测试设备是8.3,iphone 6
答案 0 :(得分:4)
你的- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
被叫了吗?
如果没有,您需要确保MapViewController
实施CLLocationDelegate
。 (例如:@interface ViewController : UIViewController<CLLocationManagerDelegate>
文件中的MapViewController.h
)
您可以从GitHub尝试this working example,或从此要点尝试the code snippet。
此外,您应该在真实设备中测试它,否则您必须在XCode模拟器中模拟一个位置(参见下图,但它可能并不总是有效)。
您必须将NSLocationWhenInUseUsageDescription
或NSLocationAlwaysUsageDescription
添加到info.plist
文件中(注意:请勿重命名或更改info.plist
文件的扩展名,扩展名必须为是.plist
):