我正在尝试使用Google Maps API,但在获取用户的位置时遇到了问题。观察值似乎永远不会改变,因为observeValueForKeyPath从未被调用过。
注意:我运行的是Xcode6-Beta5和iOS8测试版(代码在Swift中)
override func viewDidLoad() {
super.viewDidLoad()
var camera:GMSCameraPosition? = nil
mapView_ = GMSMapView.mapWithFrame(CGRectZero, camera:camera);
mapView_!.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.fromRaw(0x01)!, context: nil);
camera = GMSCameraPosition.cameraWithTarget(mapView_!.myLocation.coordinate, zoom: 6);
mapView_!.camera = camera;
self.view = mapView_;
dispatch_async(dispatch_get_main_queue(), {
mapView_!.myLocationEnabled = true;
});
}
override func observeValueForKeyPath(keyPath: String!, ofObject object: AnyObject!, change: [NSObject : AnyObject]!, context: UnsafeMutablePointer<()>) {
if (keyPath == "myLocation" && object.isKindOfClass(GMSMapView)) {
mapView_!.animateToCameraPosition(GMSCameraPosition.cameraWithTarget(mapView_!.myLocation.coordinate, zoom: 6));
}
}发布于 2014-09-10 09:57:50
到目前为止,谷歌地图iOS软件开发工具包还没有更新到支持iOS 8。所以为了使用定位功能,你需要自己进行iOS 8的位置授权。
您可以通过实例化一个CLLocationManager对象并在该对象上执行-requestWhenInUseAuthorization或-requestAlwaysAuthorization来完成此操作。在将myLocationEnabled设置为YES之前,您需要这样做。
请确保还在Info.plist文件中包含必要的密钥。如果您想要使用always authorization (如下面的代码示例所示),请提供NSLocationAlwaysUsageDescription,或者如果需要,在使用authorization时提供NSLocationWhenInUseUsageDescription。其中一个是必需的,如果你不提供它,定位功能将不起作用。
下面是一个使用Obj-C的示例。抱歉,我还不了解Swift的所有内容。:)
// Rather than setting -myLocationEnabled to YES directly,
// call this method:
- (void)enableMyLocation
{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusNotDetermined)
[self requestLocationAuthorization];
else if (status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted)
return; // we weren't allowed to show the user's location so don't enable
else
[self.view setMyLocationEnabled:YES];
}
// Ask the CLLocationManager for location authorization,
// and be sure to retain the manager somewhere on the class
- (void)requestLocationAuthorization
{
_locationAuthorizationManager = [[CLLocationManager alloc] init];
_locationAuthorizationManager.delegate = self;
[_locationAuthorizationManager requestAlwaysAuthorization];
}
// Handle the authorization callback. This is usually
// called on a background thread so go back to main.
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if (status != kCLAuthorizationStatusNotDetermined) {
[self performSelectorOnMainThread:@selector(enableMyLocation) withObject:nil waitUntilDone:[NSThread isMainThread]];
_locationAuthorizationManager.delegate = nil;
_locationAuthorizationManager = nil;
}
}发布于 2014-10-25 19:08:03
以下是在iOS swift中遇到同样问题的其他人的代码:
添加委派:
GMSMapViewDelegate, CLLocationManagerDelegate变量:
@IBOutlet var gmaps: GMSMapView?
var firstLocationUpdate: Bool?
let locationManager = CLLocationManager()功能:
func startMaps() {
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.startUpdatingLocation()
locationManager.requestWhenInUseAuthorization()
let location: CLLocation = locationManager.location
var target: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
var camera: GMSCameraPosition = GMSCameraPosition(target: target, zoom: 6, bearing: 0, viewingAngle: 0)
if let map = gmaps? {
map.myLocationEnabled = true
map.camera = camera
map.delegate = self
}
}希望这能有所帮助。
发布于 2016-01-05 13:19:29
请不要忘记将其添加到Info.plist中
<key>NSLocationWhenInUseUsageDescription</key>
<true/>
<key>NSLocationAlwaysUsageDescription</key>
<true/>https://stackoverflow.com/questions/25514074
复制相似问题