2017-03-01 84 views
0

我是iOS新手,我正在面临关于获取用户当前城市信息的问题,当点击mycurrenLocation按钮时我还需要用户当前的经纬度。如何在用户点击Objective C中的myLocation按钮时执行操作?

enter image description here

为图像中,当我点击这个当前位置的按钮,我需要当前的经纬度,并当前城市的名称的信息。这是可能的吗?如果是,如何做到这一点?

在此先感谢!

+1

开始从这里得到的位置:HTTPS:/ /developer.apple.com/library/content/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html获取经纬度后,使用'reverseGeoCoding'获取有关位置的信息,城市。 – Sunny

回答

1

找位置,如果你拍了拍My location button以下的委托方法致电

  • (BOOL)didTapMyLocationButtonForMapView:(GMSMapView中*)的MapView

,你会得到输出

,您可以通过

(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 
+0

我怎样才能得到经度和纬度。 – Muju

+1

@Muju - mapView.myLocation.coordinate.latitude或mapView.myLocation.latitude –

+0

尝试@ Anbu.Karhik谢谢。 – Muju

0

您可以使用CoreLocation获取经度和纬度。

#import <CoreLocation/CoreLocation.h> 

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

然后

float latitude = locationManager.location.coordinate.latitude; 
float longitude = locationManager.location.coordinate.longitude; 

而且通过lattitude和logitude到谷歌的位置API,你会得到街道名称。

还要检查此链接:

Google current location API

Stackoverflow answer

+0

检查此:http://stackoverflow.com/questions/12736086/how-to-get-location-latitude-longitude-value-in-variable-on-ios –

+0

我怎样才能得到城市名称。 – Muju

+0

借助Google API:https://developers.google。COM /地图/文档/ IOS-SDK /参考/ interface_g_m_s_address –

0

你可以从didUpdateToLocation位置被调用,然后使用地理编码方法来获取位置

// this delegate is called when location is found 
- (void)locationManager:(CLLocationManager *)manager  didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    // an MKReverseGeocoder is created to find a placemark coordinates 
    MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate]; 
    geoCoder.delegate = self; 
    [geoCoder start]; 
} 

// this delegate is called to convert into placemakrk 
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark 
{ 
    MKPlacemark * myPlacemark = placemark; 
    // we are now retrieving the city name 
    NSString *city = [myPlacemark.addressDictionary objectForKey: (NSString*) kABPersonAddressCityKey]; 
    } 
+0

我使用谷歌地图。 – Muju

0

检查这个代码

在.h文件中导入CoreLocation框架

#import <CoreLocation/CoreLocation.h> 

.m文件

@interface yourViewController : UIViewController<CLLocationManagerDelegate> 
    { 
     CLLocationManager *locationManager; 
     CLLocation *currentLocation; 
    } 



- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self currentlocation]; 
} 


-(void)currentlocation 
{ 

    locationManager = [CLLocationManager new]; 
    locationManager.delegate = self; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    [locationManager startUpdatingLocation]; 

} 

第5步:使用此方法

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    currentLocation = [locations objectAtIndex:0]; 
    [locationManager stopUpdatingLocation]; 
    CLGeocoder *geocoder = [[CLGeocoder alloc] init] ; 
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) 
    { 
     if (!(error)) 
     { 
      CLPlacemark *placemark = [placemarks objectAtIndex:0]; 
      NSLog(@"\nCurrent Location Detected\n"); 
      NSLog(@"placemark %@",placemark); 
      NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "]; 
      NSString *Address = [[NSString alloc]initWithString:locatedAt]; 
      NSString *Area = [[NSString alloc]initWithString:placemark.locality]; 
      NSString *Country = [[NSString alloc]initWithString:placemark.country]; 
      NSString *CountryArea = [NSString stringWithFormat:@"%@, %@", Area,Country]; 
      NSLog(@"%@",CountryArea); 
     } 
     else 
     { 
      NSLog(@"Geocode failed with error %@", error); 
      NSLog(@"\nCurrent Location Not Detected\n"); 
      //return; 
      CountryArea = NULL; 
     } 
     /*---- For more results 
     placemark.region); 
     placemark.country); 
     placemark.locality); 
     placemark.name); 
     placemark.ocean); 
     placemark.postalCode); 
     placemark.subLocality); 
     placemark.location); 
      ------*/ 
    }]; 
} 
相关问题