2013-06-12 79 views
9

Tôi hiện đang làm việc với API Google Maps trong dự án của mình. Tôi đang cố gắng đặt máy ảnh/thu phóng mặc định thành vị trí của người dùng. Tôi làm điều này:API Google Maps: Nhận tọa độ của vị trí hiện tại iOS

@implementation ViewController{ 

GMSMapView *mapView_; 

} 
@synthesize currentLatitude,currentLongitude; 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
mapView_.settings.myLocationButton = YES; 
mapView_.myLocationEnabled = YES; 


} 
- (void)loadView{ 

CLLocation *myLocation = mapView_.myLocation; 

GMSMarker *marker = [[GMSMarker alloc] init]; 
marker.position = CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude); 
marker.title = @"Current Location"; 
marker.map = mapView_; 
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:myLocation.coordinate.latitude 
                 longitude:myLocation.coordinate.longitude 
                  zoom:6]; 
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera]; 

self.view = mapView_; 
    NSLog(@"%f, %f", myLocation.coordinate.latitude, myLocation.coordinate.longitude); 

} 

Tuy nhiên, nó không hoạt động, kể từ khi tôi làm

NSLog(@"%f, %f", myLocation.coordinate.latitude, myLocation.coordinate.longitude); 

nó trả về 0, 0, và nó không cung cấp cho các vị trí hiện tại tọa độ. Làm thế nào tôi có thể có được đúng tọa độ của người dùng?

+0

được dòng này làm gì vậy? CLLocation * myLocation = mapView_.myLocation; Để có được vị trí hiện tại, chúng tôi cần 1. Thêm Khung Vị trí Core 2. Tạo một đối tượng của CLLocationManager CLLocationManager * locationManager = [[CLLocationManager alloc] init]; 3. [locationManager startUpdatingLocation]; CLLocation * location = [locationManager location]; CLLocationCoordinate2D tọa độ = [tọa độ vị trí]; –

+0

Google Maps có chức năng nhận vị trí của người dùng, nhưng tôi không biết cách lấy nó – Alexyuiop

+0

Tham khảo: http: // stackoverflow.com/questions/16331767/how-to-get-current-location-in-google-map-sdk-in-iphone Hy vọng Điều này sẽ giúp –

Trả lời

10

.h

#import <CoreLocation/CoreLocation.h> 
@property(nonatomic,retain) CLLocationManager *locationManager; 

.m

- (NSString *)deviceLocation 
{ 
NSString *theLocation = [NSString stringWithFormat:@"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude]; 
return theLocation; 
} 

- (void)viewDidLoad 
{ 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m 
    [locationManager startUpdatingLocation]; 
} 

trả lời here.

+0

Nhấp vào liên kết để xem vị trí nào là Trình quản lý vị trí .. – DrDev

+1

Bạn sẽ cần gọi '[super viewDidLoad];' khi bạn thực hiện 'viewDidLoad' –

+0

sau khi khởi tạo, bạn cần đặt đại biểu; nếu không, sẽ không hoạt động. Trong Swift: 'locationManager.delegate = self' Bạn cần phải tuân theo ủy nhiệm' CLLocationManagerDelegate' và thực hiện 'func locationManager (người quản lý: CLLocationManager, didUpdateLocations locations: [CLLocation]) {...}'. Bạn cũng có thể muốn ngừng cập nhật vị trí tại một số thời điểm: 'self.locationManager.stopUpdatingLocation()' – oyalhi

6

Khi ứng dụng khởi động lần đầu tiên có thể chưa biết vị trí của bạn, vì thường mất một lúc để thiết bị GPS khóa vị trí của bạn (nếu nó vừa mới bắt đầu) và đặc biệt nếu đây là lần đầu tiên ứng dụng đã được chạy và do đó người dùng chưa trả lời lời nhắc để cấp cho ứng dụng quyền truy cập vào vị trí của họ. Ngoài ra, có vẻ như mapView.myLocation luôn trống (hoặc không có hoặc có tọa độ 0,0) khi chế độ xem bản đồ vừa được tạo.

Vì vậy, bạn sẽ cần đợi cho đến khi vị trí của người dùng được xác định và sau đó cập nhật vị trí máy ảnh.

Một cách có thể đang sử dụng mã tại how to get current location in google map sdk in iphone theo đề xuất của Puneet, nhưng lưu ý rằng mã mẫu thiếu chi tiết về việc thiết lập trình quản lý vị trí (như thiết lập đại biểu của người quản lý vị trí). 't làm việc cho bạn.

Một lựa chọn khác có thể được sử dụng KVO trên mapView.myLocation, như mô tả ở đây: about positioning myself,some problems

Bằng cách này trong mã mẫu của bạn, bạn đang truy cập mapView.myLocation trước khi bạn tạo ra các mapView, và do đó vị trí sẽ luôn là con số không anyway.

2

Tôi vừa tải xuống Google Maps SDK Demo mới cho iOS. Dưới đây là những gì tôi đã thấy từ mã nguồn như thế nào "Vị trí hiện tại" đạt được thông qua KVO.

#if !defined(__has_feature) || !__has_feature(objc_arc) 
#error "This file requires ARC support." 
#endif 

#import "SDKDemos/Samples/MyLocationViewController.h" 

#import <GoogleMaps/GoogleMaps.h> 

@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 

Hy vọng nó có thể giúp bạn.

+0

Phương pháp này khá dài và sẽ tốt hơn nếu chúng ta đi đến Trình quản lý vị trí CLLocation. –

1

Chỉ trong trường hợp bất cứ ai muốn câu trả lời tuyệt vời DrDev trong Swift 3:

var locationManager: CLLocationManager? 

func deviceLocation() -> String { 
    let theLocation: String = "latitude: \(locationManager.location.coordinate.latitude) longitude: \(locationManager.location.coordinate.longitude)" 
    return theLocation 
} 

func viewDidLoad() { 
    locationManager = CLLocationManager() 
    locationManager.distanceFilter = kCLDistanceFilterNone 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters 
    // 100 m 
    locationManager.startUpdatingLocation() 
} 
Các vấn đề liên quan