我正在开发一个iOS应用程序(基于iOS7),需要在应用程序加载时显示用户的当前位置。我正在使用Google Maps iOS SDK。我遵循了这个Google Map,但是我无法弄清楚。如果您经过这条路,请帮助我。
我正在开发一个iOS应用程序(基于iOS7),需要在应用程序加载时显示用户的当前位置。我正在使用Google Maps iOS SDK。我遵循了这个Google Map,但是我无法弄清楚。如果您经过这条路,请帮助我。
请忘记我的先前回答。如果您使用本地的MapKit.framework,它可以很好地工作。
实际上GoogleMaps for iOS会为您完成所有工作。您不必直接使用CoreLocation。
您需要做的唯一一件事是添加yourMapView.myLocationEnabled = YES;
,框架将完成其他所有工作。(除了将地图居中于您的位置之外)。
我所做的只是简单地按照以下文档的步骤。我得到了一个以悉尼为中心的地图,但如果我缩小并移动到我的位置(如果您使用真实设备,否则使用模拟器工具来将地图定位在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
有了我给你的文档和框架中包含的示例,你应该能够做到你想要的。
Google Maps iOS SDK
无法访问设备位置信息,因此您需要使用iOS
的CLLocationManager
来获取位置信息。CoreLocation.framework
添加到您的项目中:
Project Navigator
Build Phases
选项卡Link Binary with Libraries
中添加CoreLocation.framework
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);
}
}
GMSMapView
时,您需要激活位置,使用yourMapView.myLocationEnabled = YES;
,并在CLLocationManager Delegate
方法中尝试更新属性yourMapView.myLocation(location)
。这里的参数location
是从委托中检索到的CLLocation
。我认为您应该在地图上看到通常的蓝点。 - Maxime CapelleXcode + Swift + Google Maps 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.) 添加用于位置变更的观察器:
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)。
- (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
当前位置不会在模拟器上显示...连接一个真实设备并尝试一下吧。我在模拟器上跑了两天,都不知道它不会模拟位置。
有许多方法...
我使用了这种方法,并且在所有情况下都有效。Google会以JSON格式返回所有内容,你需要自己处理这些数据。
以下是在项目中加载Google地图的步骤:
从此链接https://developers.google.com/places/ios-api/获取API密钥,使用你的Google账户登录并添加你的项目,创建一个iOS密钥,然后在你的项目中使用它。
启用所有需要的Google地图API
a-googlemaps sdk for ios b-googlemap direction api c-" " javasripts api d- picker api e- places api for ios f distance matrix api
在AppDelegate方法中...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GMSServices provideAPIKey:@"xxxxxxxx4rilCeZeUhPORXWNJVpUoxxxxxxxx"];
return YES;
}
MyLocationViewController.m
)的功能。当我在我的iPhone上启动应用程序时,地图将以我的位置为中心。要创建一个带有您当前位置的JSON,您可能应该将代码放在由观察者触发的方法中。在那里,您可以使用您的坐标构建JSON,并对其进行任何操作。 - Maxime Capelledealloc
上尝试删除观察者(不存在),因为您在viewDidLoad
中启动了 KVO,它将会崩溃。 - DanSkeelNSLocationWhenInUseUsageDescription
或NSLocationAlwaysUsageDescription
。 - hash3r