我是iOS的新手,我在获取当前用户城市的信息方面遇到了问题,当我点击mycurrenLocation按钮时,我还需要用户当前的经纬度。
在我点击此当前位置按钮的图像中,我需要当前纬度和经度的信息以及当前城市的名称。有可能吗?如果是,该怎么做?
提前致谢!
答案 0 :(得分:2)
如果您点按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
答案 1 :(得分: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;
将格子和逻辑传递到谷歌位置api,你将获得街道名称。
同时选中此链接:
答案 2 :(得分: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];
}
答案 3 :(得分: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);
------*/
}];
}