如何将经度和纬度值传递给ns url?

时间:2011-08-18 03:53:43

标签: iphone

我正在开发一个应用程序。我使用google 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.latitudenewlocation.Coordinates.longitude传递给该方法中的该网址。或者有没有办法在任何其他方法中传递这些值。

4 个答案:

答案 0 :(得分:2)

只需使用此..

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

答案 1 :(得分:1)

这应该有效,但是大脑编译:)

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
//Filter the location first here, it could be an invalid location or maybe too recent to the last one because this method is called several times in short times of periods.

//Then use the value
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];
//do your thing here...

}

我希望它有所帮助

答案 2 :(得分:1)

当位置管理员找到Google API的新位置时,您可以传递纬度和经度:

NSString *address = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f&output=csv", newLocation.lattitude, newLocation.longitude];
NSURL *url = [NSURL URLFromString:address];

答案 3 :(得分: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]];