0

こんにちは、アプリに mapAnnotations の配列があります。各注釈オブジェクトには、アドレスと座標があります。CLLocationDistance オブジェクトであるユーザーの現在の場所を見つけるために、場所マネージャーを使用しています。次に、mapAnnotations 配列を反復処理し、ユーザーの位置を各注釈の位置座標と比較して、ユーザーに近いものを見つけようとしています。コードは次のとおりです。

-(void)locationManager:(CLLocationManager*)_manager 
    didUpdateToLocation:(CLLocation *)newLocation 
    fromLocation:(CLLocation *)oldLocation
{
    self.userLocation = newLocation;
    NSLog(@"Usser location is: %@", self.userLocation);
    NSLog(@"USer location latitude = %.4f", self.userLocation.coordinate.latitude );
    NSLog(@"USer location longitude = %.4f", self.userLocation.coordinate.longitude);
    if (self.userLocation)
    {
        [self.locMgr stopUpdatingLocation];
    }
    [self calculateShortestDistanceBetweenUserAndBiz:self.userLocation];
}

//==============================================================================
-(void)calculateShortestDistanceBetweenUserAndBiz:(CLLocation *)_userLocation
{
    NSLog(@"Entering %s", __FUNCTION__);
    for(LocationMapAnnotation *tempAnnot in self.mapAnnotations)
    {
        CLLocation *tempLocation = [[CLLocation alloc] initWithLatitude:tempAnnot.coordinate.latitude longitude:tempAnnot.coordinate.longitude];
        NSLog(@"tempLocation created %@ ", tempLocation);
        CLLocationDistance tempDistance = [_userLocation distanceFromLocation: tempLocation];
        NSLog(@"tempDistance  was calculated at %d", tempDistance);
        if(!self.shortestDistance) 
        {
            self.shortestDistance = tempDistance;
            NSLog(@"Assigned value %d to shortestDistance", self.shortestDistance);
            continue;
        }
        if(self.shortestDistance >= tempDistance)
        {
        NSLog(@"Replacing shortest distance value from %d to %d", self.shortestDistance, tempDistance);
            self.shortestDistance = tempDistance;
        }
    }
}

メソッドは正常に機能し、コンソール出力は次のとおりです。

Created a new locationmodel

Usser location is: <+43.47878384, -80.53074371> +/- 1629.00m (speed -1.00 mps / course -1.00) @ 11-05-22 8:58:10 PM Eastern Daylight Time

USer location latitude = 43.4788

USer location longitude = -80.5307

Entering -[locationModel calculateShortestDistanceBetweenUserAndBiz:]

tempLocation created <+43.69445000, -79.75080000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at 629486151

Assigned value 629486151 to shortestDistance

tempLocation created <+43.62192000, -79.52238000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at 209234637
tempLocation created <+43.59233000, -79.54258000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at -1442027836
tempLocation created <+43.57980000, -79.61713000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at -680604989

tempLocation created <+43.55260000, -79.58553000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at -1882725832
tempLocation created <+43.58191000, -79.71417000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at 947780311

Replacing shortest distance value from 629486151 to 1089496493

tempLocation created <+43.65530000, -79.41298000> +/- 0.00m (speed -1.00 mps / course -1.00) 

tempDistance  was calculated at 1561068529

tempLocation created <+43.64854000, -79.38776000> +/- 0.00m (speed -1.00 mps / course -1.00) 
tempDistance  was calculated at 466814513

tempLocation created <+43.47690000, -80.52500000> +/- 0.00m (speed -1.00 mps / course -1.00) 
tempDistance  was calculated at -1816387020
Replacing shortest distance value from 947780311 to 1089489801
Received memory warning. Level=1

私の質問は、関数が機能しているということです。値の意味を理解しようとしていますか? たとえば、最短距離と温度距離に割り当てられた値は膨大な数ですが、それらは何を表しているのでしょうか? 関数が正常に動作していることをどのように確認できますか。理解できない。この最短距離の値を使用して、ここで MKMapview を設定できるようにしたいのですが、以下の行を、ユーザーの場所である cetercoord からの最短距離に対応する値に置き換えません。

MKCoordinateRegion preRegion = MKCoordinateRegionMakeWithDistance(centerCoord, 15000, 15000); 

助けてください。

4

1 に答える 1

3

CLLocationDistanceは型であるため、 (int の場合)の代わりにdouble使用する必要があります。これがおそらく、非常に大きな無意味な数値が表示される理由です。%f%d

shortestDistanceも a として宣言されていると仮定するとCLLocationDistance、それをログに記録するときにも同じことを行います。

CLLocationDistanceメートル単位でありMKCoordinateRegionMakeWithDistance、最後の2つのパラメーターにも期待されるものであるため、行は次のようになります。

MKCoordinateRegion preRegion = MKCoordinateRegionMakeWithDistance
    (centerCoord, shortestDistance, shortestDistance); 

別の無関係なこと:

tempLocationメモリ リークがあります。呼び出し後に解放する必要がありますdistanceFromLocation

于 2011-05-23T01:40:59.717 に答える