通过经度和;按钮点击事件的纬度

时间:2011-10-13 12:54:47

标签: iphone cllocationmanager

您好我使用CoreLocation在我的地图应用程序中获取纬度和经度,以通过使用以下方法获取纬度和经度

- (void)viewDidLoad {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
    [super viewDidLoad];
   }

_(void)locationManager ....方法未获得调用

 - (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation
  {
int degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                 degrees, minutes, seconds];

degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                   degrees, minutes, seconds];

   }

我想打电话给lati& longi on Button单击事件到URL。但它没有用。它通过了0000.0 lati& longi到我的网址。有人可以告诉我如何纠正这段代码

4 个答案:

答案 0 :(得分:3)

我没有看到你的CLLocationManager的初始问题。如果你没有得到回调,也许尚未有可用的位置。您还应该实现错误回调,看看它是否被调用。

我建议您尝试其中一个CLLocationManager examples provided by Apple并查看是否可以运行示例。

答案 1 :(得分:2)

我这样做是为了在按钮点击时将纬度和经度传递到网络服务网址...

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

    if((fabs(newLocation.coordinate.latitude) > 0.001) ||    (fabs(newLocation.coordinate.longitude) > 0.001)) {
    NSLog(@"Got location %f,%f", newLocation.coordinate.latitude,   newLocation.coordinate.longitude);
    if (currentLocation != nil) {
        [currentLocation release];
    }
    currentLocation = newLocation;
    [currentLocation retain];
}}

和点击事件代码我刚刚传递了locationmanager.location.coordinate.latitude .....

NSString *url = [NSString stringWithFormat:@"http://.....Url..../hespdirectory/phpsqlsearch_genxml.php?lat=%f&lng=%f&radius=%f",locationManager.location.coordinate.latitude,locationManager.location.coordinate.longitude,radius];

答案 2 :(得分:1)

U可以直接将CLLocation更改为NSString

NSString *lat=[NSString stringWithFormat:@"%f", newLocation.latitude];
NSString *lon=[NSString stringWithFormat:@"%f", newLocation.longitude];

答案 3 :(得分:0)

答案没有直接显示,但您应该将控制器指定为CLLocationManagerDelegate以接收回调正确吗?