如何将经度和纬度值作为参数传递给ns url?

时间:2011-08-18 04:30:58

标签: iphone

我开发了一个应用程序。在那里我使用谷歌api获取位置信息。为此我使用CLLocationManager获取位置纬度和经度值。我的pblm是如何将这些纬度和经度值传递给nsurl .and iam直接将一个位置值给予URL,如下所示。

 NSURL *URL = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search           /json?location=40.7143528,-74.0059731&radius=10000&types=school&sensor=false&key=AIzaSyDbiWWIOmc08YSb9DAkdyTWXh_PirVuXpM"];

我在下面的方法中写了这个,用于获取并传递纬度和经度值。

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

所以请告诉我如何将newlocation.Coordinates.latitude和newlocation.Coordinates.longitude传递给该方法中的url。

根据其他人的建议我改变了下面的代码。

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

    NSString *address = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f, %f&radius=10000&types=school&sensor=false&key=AIzaSyDbiWWIOmc08YSb9DAkdyTWXh_PirVuXpM", newLocation.lattitude, newLocation.longitude];
    NSURL *url = [NSURL URLFromString:address];
}

当执行第一行时,这也会出现类似Exe_Bad_Access的错误。请告诉我如何编写该错误。

3 个答案:

答案 0 :(得分:4)

-(void)getLocation{
    locationManager = [[CLLocationManager alloc] init] ;
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];

}


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

    NSString * lat = [[[NSString alloc] initWithFormat:@"%f", newLocation.coordinate.latitude]autorelease];
    NSString * lon = [[[NSString alloc] initWithFormat:@"%f", newLocation.coordinate.longitude]autorelease];


    [locationManager stopUpdatingLocation]; 

    //set google maps locations 
    NSString * googleString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@+%@",lat,lon];


}

答案 1 :(得分:0)

试试这个,

NSURL *addressUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%@,%@&radius=10000&types=school&sensor=false&key=AIzaSyDbiWWIOmc08YSb9DAkdyTWXh_PirVuXpM",newLocation.latitude, newLocation.longitude, nil]];

答案 2 :(得分:0)

这将有效:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
    NSString *address = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f, %f&radius=10000&types=school&sensor=false&key=AIzaSyDbiWWIOmc08YSb9DAkdyTWXh_PirVuXpM", newLocation.coordinate.latitude, newLocation.coordinate.longitude];
    NSURL *url = [NSURL URLFromString:address];
}