【问题标题】:How to perform action when user click on myLocation button in Objective C?当用户单击 Objective C 中的 myLocation 按钮时如何执行操作?
【发布时间】:2017-03-01 12:46:21
【问题描述】:

我是 iOS 新手,在获取当前用户所在城市的信息时遇到问题,当我点击 mycurrenLocation 按钮时,我还需要用户当前的纬度和经度。

如图所示,当我单击此当前位置按钮时,我需要当前纬度和经度信息以及当前城市的名称。有可能吗?如果是,该怎么做?

提前致谢!

【问题讨论】:

标签: ios objective-c google-maps


【解决方案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

【讨论】:

  • 如何获取经纬度。
  • @Muju - mapView.myLocation.coordinate.latitude 或尝试使用 mapView.myLocation.latitude
  • @Anbu.Karhik 谢谢。
  • @Muju - 你用的是哪一个
  • @Anub.karthik didTapMyLocationButtonForMapView 方法和您的评论行。
【解决方案2】:

您可以从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】:

您可以使用 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;

然后将纬度和对数传递到 google location api,您将获得街道名称。

另请查看此链接:

Google current location API

Stackoverflow answer

【讨论】:

【解决方案4】:

检查此代码

在 .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);
          ------*/
     }];
}

【讨论】:

    猜你喜欢
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    相关资源
    最近更新 更多