2010-01-13 80 views
26

是否可以使用MKMapView自己的位置管理器來返回用戶當前位置以傳遞到Web服務?從MKMapView獲取用戶位置

我有mapView.showsUserLocation=YES;,這確實在我的位置返回一個有效的藍點,但在模擬器,其Cupertino的 - 這是很好的,但是當我看到

mapView.userLocation.coordinate.latitude,它等於180,而CLLocationManager返回正確的一個,37.3317。

我想避免爲我的三個選項卡使用多個位置管理器,因此使用mapViews將會很有幫助。

謝謝。

+0

嗨joec,你是什麼意思「這不會返回一個有效的藍點在我的位置」?看看我的答案,它可以是有幫助的。 – vfn 2010-01-14 06:50:13

+0

好問題! +1,但選定的答案永遠不會工作!所以-1! – 2013-01-07 11:03:20

+0

這裏接受的答案是錯誤的,它不會工作, 爲正確的解決方案閱讀此問題的答案[http://stackoverflow.com/questions/1449486/iphone-current-user-location-coordinates-showing-作爲-0-0](HTTP://計算器。com/questions/1449486/iphone-current-user-location-coordinates-showing-as-0-0)希望這是有道理的 – 2013-01-07 11:08:32

回答

40

可以獲得從的MKMapView用戶位置。你只是在檢索它時錯過了一個屬性。它應該是:

mapView.userLocation.location.coordinate.latitude; 

userLocation只存儲一個CLLocation位置屬性和一個BOOL更新屬性。你必須去位置屬性來獲得座標。

-DREW

編輯:的MKMapView的用戶位置不更新,直到地圖加載完成,並檢查太早將返回零。爲了避免這種情況,我建議使用MKMapViewDelegate方法 -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

+3

嗨joec,爲你做了這項工作。在我的情況下,它返回0.0。 – Aisha 2011-02-21 09:25:29

+0

其在我的情況下返回0.0000 :( – Piscean 2012-01-31 11:05:05

+0

如果您獲得0.000作爲位置,請參閱上面的答案中的編輯。 – 2013-01-08 17:25:09

3

因此,使用獨特的CLLocateManager,你可以創建一個類是委託你的地圖,這樣,而不是做:

self.locationManager = [[CLLocationManager alloc] init]; 
    _locationManager.delegate = self;

做這樣的事情:

self.locationManager = [[CLLocationManager alloc] init]; 
    _locationManager.delegate = mySharedDelegate;

其中mySharedDelegate是具有所有CLLocationManager委託方法的類。

你只能得到該用戶位置的有效協調,中

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

第一通話後,當這個方法被調用這是因爲GPS已經找到了新的位置,讓藍色圓點將被移動到那裏並且userLocation將具有新的座標。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"---------- locationManager didUpdateToLocation"); 
    location=newLocation.coordinate; 

    NSLog(@"Location after calibration, user location (%f, %f)", _mapView.userLocation.coordinate.latitude, _mapView.userLocation.coordinate.longitude); 
}

你有這個想法:

的時候才發現

用下面的方法對你CLLocationManager委託記錄當前位置?

乾杯,
VFN

+0

那好吧,爲我的每個選項卡設置一個單獨的位置管理器,並只共享委託然後?或者我應該在應用程序委託中分配和初始化一個位置管理器並使用它?謝謝。 – joec 2010-01-14 12:56:41

0
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
    { 
     NSLog(@"welcome into the map view annotation"); 

     // if it's the user location, just return nil. 
     if ([annotation isKindOfClass:[MyMapannotation class]]) 
     { 
      MyMapannotation *annotation12=(MyMapannotation *)annotation; 
      // try to dequeue an existing pin view first 
      static NSString* AnnotationIdentifier = @"AnnotationIdentifier"; 
      MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc] 
              initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] ; 
      pinView.animatesDrop=YES; 
      pinView.canShowCallout=YES; 
      pinView.pinColor=MKPinAnnotationColorPurple; 
      pinView.tag=annotation12.tag; 

      UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
      [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
      rightButton.tag=annotation12.tag; 
      [rightButton addTarget:self 
          action:@selector(showDetails:) 
        forControlEvents:UIControlEventTouchUpInside]; 
      pinView.rightCalloutAccessoryView = rightButton; 
      UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"artpin"]]; 
      pinView.image = profileIconView.image; 
      return pinView; 
     } 
     else 
      return nil; 
    } 

    -(IBAction)showDetails:(id)sender 
    { 
     UIButton *btn=(UIButton *)sender; 

    } 


    -(void)Load_mapview 
    { 


     for (int i=0; i<[arr_nearby count]; i++) 
     { 
      NSNumber *latitude = [[[[arr_nearby objectAtIndex:i] valueForKey:@"geometry"] valueForKey:@"location"] valueForKey:@"lat"]; 

      NSNumber *longitude = [[[[arr_nearby objectAtIndex:i] valueForKey:@"geometry"] valueForKey:@"location"] valueForKey:@"lng"]; 

      NSString *title = [[arr_nearby objectAtIndex:i] valueForKey:@"name"]; 

      //Create coordinates from the latitude and longitude values 

      CLLocationCoordinate2D coord; 

      coord.latitude = latitude.doubleValue; 

      coord.longitude = longitude.doubleValue; 

      MyMapannotation *annotation = [[MyMapannotation alloc] initWithTitle:title AndCoordinate:coord andtag:i]; 
      [_map_nearby addAnnotation:annotation]; 


      // [annotations addObject:annotation]; 

     } 

     [self zoomToLocation]; 


    } 
    -(void)zoomToLocation 

    { 

     CLLocationCoordinate2D zoomLocation; 

     zoomLocation.latitude = [[[[[arr_nearby objectAtIndex:0] valueForKey:@"geometry"] valueForKey:@"location"] valueForKey:@"lat"] floatValue]; 

     zoomLocation.longitude= [[[[[arr_nearby objectAtIndex:0] valueForKey:@"geometry"] valueForKey:@"location"] valueForKey:@"lng"] floatValue]; 

     MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 7.5*5,7.5*5); 

     [_map_nearby setRegion:viewRegion animated:YES]; 

     [_map_nearby regionThatFits:viewRegion]; 

    } 

// 
// MyMapannotation.h 
// IOS_Googgle 
// 
// Created by Vivek Chauhan on 27/06/16. 
// Copyright (c) 2016 anand. All rights reserved. 
// 

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 


@interface MyMapannotation : NSObject <MKAnnotation> 

@property (nonatomic,copy) NSString *title; 
@property (nonatomic,assign) int tag; 


@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

-(id) initWithTitle:(NSString *) title AndCoordinate:(CLLocationCoordinate2D)coordinate andtag:(int)tagofbutton; 


@end 


// 
// MyMapannotation.m 
// IOS_Googgle 
// 
// Created by Vivek Chauhan on 27/06/16. 
// Copyright (c) 2016 anand. All rights reserved. 
// 

#import "MyMapannotation.h" 

@implementation MyMapannotation 

@synthesize coordinate=_coordinate; 

@synthesize title=_title; 
@synthesize tag=_tag; 


-(id) initWithTitle:(NSString *) title AndCoordinate:(CLLocationCoordinate2D)coordinate andtag:(int)tagofbutton 

{ 

    self = [super init]; 

    _title = title; 

    _coordinate = coordinate; 
    _tag=tagofbutton; 

    return self; 

} 
@end