【问题标题】:Passing Current longitude & latitude on buttonclick event在按钮单击事件上传递当前经度和纬度
【发布时间】:2011-10-13 12:54:47
【问题描述】:

您好,我正在使用 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];

   }

我想在按钮点击事件上调用纬度和经度到 URL。但它不起作用。它将 0000.0 纬度和经度传递给我的网址。谁能告诉我如何纠正这个代码

【问题讨论】:

    标签: iphone cllocationmanager


    【解决方案1】:

    我认为您的 CLLocationManager 初始化没有任何明显问题。如果您没有收到回调,则可能它没有可用的位置。您还应该实现错误回调并查看它是否被调用。

    我建议您尝试CLLocationManager examples provided by Apple 之一,看看您是否可以运行该示例。

    【讨论】:

      【解决方案2】:

      我这样做是为了在单击按钮时将纬度和经度传递给 Web 服务 URL...

      - (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];
      

      【讨论】:

        【解决方案3】:

        U可以直接把CLLocation改成NSString

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

        【讨论】:

        • 上面的代码我已经粘贴在我的按钮点击事件中以在 URL 中传递坐标.....它显示了 newLocation undeclare
        • 你没有在这里得到新的位置。你在里面添加这个 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 方法。并声明lat 和 lon 作为全局并使用它们
        • 我的 .....(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 没有被调用。我通过设置断点进行检查。我该怎么办
        • R u 检查模拟器。如果是这样,它不会被调用。这些代表称为 oly 设备
        • 在设备上调用该方法。现在我应该在按钮单击事件上编写代码以在 url 中传递 lati .....我给了 NSString * lati = [[[NSString alloc] initWithFormat:@"%f", coordinate.latitude]autorelease];它抛出 EXC_BAD_AcCess
        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-07-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多