【发布时间】:2014-01-24 21:23:57
【问题描述】:
对于我的iOS 应用程序(在iOS7 中构建),我需要在应用程序加载时显示用户的当前位置。我正在使用Google Maps iOS SDK。我正在关注这个
Google Map
但我无法弄清楚。如果您经过这条路,请提供帮助。
【问题讨论】:
标签: ios objective-c google-maps-sdk-ios
对于我的iOS 应用程序(在iOS7 中构建),我需要在应用程序加载时显示用户的当前位置。我正在使用Google Maps iOS SDK。我正在关注这个
Google Map
但我无法弄清楚。如果您经过这条路,请提供帮助。
【问题讨论】:
标签: ios objective-c google-maps-sdk-ios
忘记我之前的回答。如果您使用本机 MapKit.framework,效果会很好。
事实上,iOS 版 GoogleMaps 会为您完成所有工作。您不必直接使用 CoreLocation。
你唯一需要做的就是添加yourMapView.myLocationEnabled = YES;,框架会做所有事情。 (除了将地图居中放置在您的位置上)。
我做了什么:我只是按照以下documentation的步骤进行操作。我得到了一张以悉尼为中心的地图,但如果我缩小并移动到我的位置(如果你使用真实设备,否则使用模拟器工具以 Apple 的位置为中心),我可以看到我位置上的蓝点。
现在,如果您想更新地图以跟随您的位置,您可以复制框架目录中包含的 Google 示例 MyLocationViewController.m。他们只是在myLocation 属性上添加一个观察者来更新相机属性:
@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
有了我给你的文档和框架中包含的示例,你应该能够做你想做的事。
【讨论】:
MyLocationViewController.m)。当我在 iPhone 上启动应用程序时,我的地图以我的位置为中心。要使用您当前的位置创建 JSON,您可能应该将代码放入由观察者触发的方法中。在那里你可以用你的坐标构建一个 JSON 并用它来制作你想要的东西。
似乎Google Maps iOS SDK无法访问设备位置。
所以你必须使用CLLocationManagerof iOS来检索位置。
首先,将CoreLocation.framework 添加到您的项目中:
Project Navigator
Build Phases
Link Binary with Libraries 中添加CoreLocation.framework
那么你需要做的就是按照Apple documentation的基本例子。
可能在您的ViewDidLoad 中创建一个CLLocationManager:
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
//Configure Accuracy depending on your needs, default is kCLLocationAccuracyBest
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
// Set a movement threshold for new events.
locationManager.distanceFilter = 500; // meters
[locationManager startUpdatingLocation];
每次更新位置时使用CLLocationManagerDelegate,您可以在Google Maps 上更新用户位置:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
// If it's a relatively recent event, turn off updates to save power.
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0) {
// Update your marker on your map using location.coordinate.latitude
//and location.coordinate.longitude);
}
}
【讨论】:
yourMapView.myLocationEnabled = YES; 实例化GMSMapView 时激活位置,并在CLLocationManager Delegate 方法中尝试更新属性yourMapView.myLocation(location)。这里的参数location 是从委托中检索到的CLLocation。我认为您应该在地图上找到通常是蓝色的点。
Xcode + Swift + 谷歌地图 iOS
一步一步的食谱:
1.) 将密钥字符串添加到 Info.plist(作为源代码开放):
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs your location to function properly</string>
2.) 将CLLocationManagerDelegate 添加到您的视图控制器类:
class MapViewController: UIViewController, CLLocationManagerDelegate {
...
}
3.) 将CLLocationManager 添加到您的班级:
var mLocationManager = CLLocationManager()
var mDidFindMyLocation = false
4.) 请求许可并添加观察者:
override func viewDidLoad() {
super.viewDidLoad()
mLocationManager.delegate = self
mLocationManager.requestWhenInUseAuthorization()
yourMapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.new, context: nil)
...
}
5.) 等待授权并在 Google 地图中启用位置:
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if (status == CLAuthorizationStatus.authorizedWhenInUse) {
yourMapView.isMyLocationEnabled = true
}
}
6.) 添加 observable 以更改位置:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if (!mDidFindMyLocation) {
let myLocation: CLLocation = change![NSKeyValueChangeKey.newKey] as! CLLocation
// do whatever you want here with the location
yourMapView.camera = GMSCameraPosition.camera(withTarget: myLocation.coordinate, zoom: 10.0)
yourMapView.settings.myLocationButton = true
mDidFindMyLocation = true
print("found location!")
}
}
就是这样!
【讨论】:
在任何 iOS 设备上,使用 Core Location 获取用户的位置。具体来说,您需要 CLLocation 类(和 CLLocationManager)。
【讨论】:
didTapMyLocationButton 是不是委托方法?
- (BOOL)didTapMyLocationButtonForMapView:(GMSMapView *)mapView {
return YES;
}
你可以通过
获得位置(lldb) po mapView.myLocation
<+37.33243033,-122.03088128> +/- 386.93m (speed -1.00 mps / course -1.00) @ 5/19/14, 6:22:28 PM Moscow Standard Time
【讨论】:
当前位置不会显示在模拟器上...连接一个真实设备并尝试一下 我在模拟器里跑了2天,不知道它不模拟位置
【讨论】:
有很多方法... 我使用了这种方法,它适用于所有情况。 Google 以 json 格式为您提供所有响应,以及您如何处理这些数据。
有一些步骤可以在您的项目中加载谷歌地图。
从此链接https://developers.google.com/places/ios-api/找到api密钥 使用您的谷歌帐户登录并添加您的项目并创建一个 ios 密钥。 然后在你的项目中使用它
启用谷歌地图所需的所有 api
a-googlemaps sdk for ios b-googlemap 方向api c-"" javasripts api d- 选择器 api ios的e-places api f距离矩阵api
在 appdelegate 方法中...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GMSServices provideAPIKey:@"xxxxxxxx4rilCeZeUhPORXWNJVpUoxxxxxxxx"];
return YES;
}
【讨论】: