[英]Google Maps iOS SDK, Getting Current Location of user
For my iOS
app (building in iOS7
),i need to show user's current location when the app load.I am using Google Maps iOS SDK
. 对于我的
iOS
应用(在iOS7
中iOS7
),我需要在应用加载时显示用户的当前位置。我正在使用Google Maps iOS SDK
。 I am following this Google Map But i can't figure it out. 我正在追踪这张Google Map,但我无法弄清楚。 Please help if you go through the path.
如果您走这条路,请提供帮助。
Forget my previous answer. 忘了我以前的答案。 It works well if you use the native MapKit.framework.
如果您使用本地MapKit.framework,它会很好地工作。
In fact GoogleMaps for iOS do all the work for you. 实际上,iOS版GoogleMaps可以为您完成所有工作。 You don't have to use CoreLocation directly.
您不必直接使用CoreLocation。
The only thing you have to do is to add yourMapView.myLocationEnabled = YES;
您唯一要做的就是添加
yourMapView.myLocationEnabled = YES;
and the framework will do everything. 该框架将完成所有工作。 (Except center the map on you position).
(除了将地图居中放置在您的位置之外)。
What I have done : I simply followed the steps of the following documentation . 我做了什么:我只是按照以下文档中的步骤进行操作。 And I got a map centered on Sydney but if I zoomed out and moved to my place (if you use a real device, otherwise use simulator tools to center on Apple's location), I could see the blue point on my position.
我得到了一张以悉尼为中心的地图,但是如果我缩小并移到了自己的位置(如果您使用的是真实设备,否则请使用模拟器工具以Apple的位置为中心),则可以看到我位置上的蓝点。
Now if you want to update the map to follow your position, you can copy Google example MyLocationViewController.m
that is included in the framework directory. 现在,如果您要更新地图以跟随您的位置,则可以复制框架目录中包含的Google示例
MyLocationViewController.m
。 They just add a observer on the myLocation
property to update the camera properties: 他们只是在
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
With the doc I gave you and the samples included in the framework you should be able to do what you want. 借助我给您的文档以及框架中包含的示例,您应该能够做您想做的事情。
It seems Google Maps iOS SDK
cannot access to the device position. 看来
Google Maps iOS SDK
无法访问设备位置。 So you have to retrieve the position by using CLLocationManager
of iOS
. 因此,您必须使用
iOS
CLLocationManager
检索位置。
First, add the CoreLocation.framework
to your project : 首先,将
CoreLocation.framework
添加到您的项目中:
Project Navigator
Project Navigator
Build Phases
Build Phases
CoreLocation.framework
in the Link Binary with Libraries
Link Binary with Libraries
的Link Binary with Libraries
CoreLocation.framework
中添加CoreLocation.framework
Then all you need to do is to follow the basic exemple of Apple documentation . 然后,您需要做的就是遵循Apple文档的基本示例。
Create a CLLocationManager
probably in your ViewDidLoad
: 在您的
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];
With the CLLocationManagerDelegate
every time the position is updated, you can update the user position on your Google Maps
: 每次更新位置时,都可以使用
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);
}
}
Xcode + Swift + Google Maps iOS Xcode + Swift + Google Maps iOS
Step by step recipe: 逐步食谱:
1.) Add key string to Info.plist (open as source code): 1.)将密钥字符串添加到Info.plist(作为源代码打开):
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs your location to function properly</string>
2.) Add CLLocationManagerDelegate
to your view controller class: 2.)将
CLLocationManagerDelegate
添加到您的视图控制器类:
class MapViewController: UIViewController, CLLocationManagerDelegate {
...
}
3.) Add CLLocationManager
into your class: 3.)将
CLLocationManager
添加到您的类中:
var mLocationManager = CLLocationManager()
var mDidFindMyLocation = false
4.) Ask for permission and add observer: 4.)寻求许可并添加观察员:
override func viewDidLoad() {
super.viewDidLoad()
mLocationManager.delegate = self
mLocationManager.requestWhenInUseAuthorization()
yourMapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.new, context: nil)
...
}
5.) Wait for authorization and enable location in Google Maps: 5.)等待授权并在Google Maps中启用位置:
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if (status == CLAuthorizationStatus.authorizedWhenInUse) {
yourMapView.isMyLocationEnabled = true
}
}
6.) Add observable for change of location: 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!")
}
}
That's it! 而已!
On any iOS device, get the user's location with Core Location . 在任何iOS设备上,都可以通过Core Location获取用户的位置。 Specifically, you want the CLLocation class (and CLLocationManager).
具体来说,您需要CLLocation类(和CLLocationManager)。
Is delegate method didTapMyLocationButton is not way? 是委托方法didTapMyLocationButton不是吗?
https://developers.google.com/maps/documentation/ios/reference/protocol_g_m_s_map_view_delegate-p#ac0e0171b811e839d9021800ca9fd33f4 https://developers.google.com/maps/documentation/ios/reference/protocol_g_m_s_map_view_delegate-p#ac0e0171b811e839d9021800ca9fd33f4
- (BOOL)didTapMyLocationButtonForMapView:(GMSMapView *)mapView {
return YES;
}
And you can get location by 您可以通过以下方式获取位置
(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天,却不知道它不会模拟位置
there are many methods... I used this method and it works in all cases. 有很多方法...我使用了这种方法,它在所有情况下都有效。 Google gives you everything with the reponse in json format and its on you how you deal with that data.
Google会以json格式向您提供所有内容以及响应,以及您如何处理该数据。
Some steps are there to load google map in your project. 有一些步骤可以将google map加载到您的项目中。
find the api key from this link https://developers.google.com/places/ios-api/ sign in with your google account and add your project and create a ios key. 从此链接中找到api密钥https://developers.google.com/places/ios-api/使用您的Google帐户登录并添加您的项目并创建ios密钥。 then use this in your project
然后在您的项目中使用
enable all the api needed for google map 启用Google Map所需的所有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 ios的a-googlemaps sdk ios b-googlemap方向api c-“” javasripts api d- picker api e- api ios距离矩阵api
in appdelegate method... 在appdelegate方法中...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GMSServices provideAPIKey:@"xxxxxxxx4rilCeZeUhPORXWNJVpUoxxxxxxxx"];
return YES;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.