【问题标题】:How to find current user location to use in Google Places API for iOS如何在 iOS 版 Google Places API 中查找当前用户位置
【发布时间】:2012-08-21 10:13:14
【问题描述】:

我希望使用此 Google Places API 网址在 iOS 应用中进行搜索。而不是使用指定的位置,我想使用用户的当前位置:

NSURL *googlePlacesURL = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/xml?location=34.0522222,-118.2427778&radius=500&types=spa&sensor=false&key=AIzaSyAgcp4KtiTYifDSkIqd4-1IPBHCsU0r2_I"];

是否需要存储用户当前的纬度/经度位置,然后将这些值放入 NSURL?

感谢您的帮助

【问题讨论】:

    标签: iphone objective-c ios google-maps core-location


    【解决方案1】:

    您的 URl 显示您想列出当前位置附近的地点。

    为此,您需要当前位置坐标、半径、搜索到的地名和​​关键字。

    这样做:

    第 1 步:添加 CoreLocation 框架

    第 2 步:#import

    第 3 步:添加 CLLocationManagerDelegate 委托

    第 4 步:制作 CLLocationManager *locationManager;

    第 5 步:初始化 CLLocationManager(通常在 ViewDidLoad 中)

    self.locationManager = [[CLLocationManager alloc] init];// autorelease];
    // This is the most important property to set for the manager. It ultimately determines how the manager will
    // attempt to acquire location and thus, the amount of power that will be consumed.
    locationManager.desiredAccuracy = kCLLocationAccuracyBest ;
    // Once configured, the location manager must be "started".
    [locationManager startUpdatingLocation];   
    locationManager.delegate = self;
    

    第 6 步:为 CLLocationManager 编写代码

    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
    { 
        StrCurrentLongitude=[NSString stringWithFormat: @"%f", newLocation.coordinate.longitude]; // string value
        StrCurrentLatitude=[NSString stringWithFormat: @"%f", newLocation.coordinate.latitude];
        appDeleg.newlocation=newLocation;
        [locationManager stopUpdatingLocation]; // string Value
    }
    

    第 7 步:使用坐标和开火请求:

    [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/xml?location=%@,%@&radius=%@&name=%@&sensor=false&key=YOU-API-KEY",StrCurrentLatitude,StrCurrentLongitude,appDeleg.strRadius,strSelectedCategory]
    

    享受

    【讨论】:

    • 请注意,委托方法-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation在iOS 6.0中已被弃用,建议使用- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations参考here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    相关资源
    最近更新 更多