1

私のアプリはリアルタイム トラッカーです。複数のユーザーがログインし、自分の座標を Web サービスに送信して位置情報を更新します。Web サービスは 2 分ごとにコールバックされ、すべてのユーザーを表示しますMapView

メソッドでWebサービスからユーザーの場所を取得するたびに、それらをconnectionDidFinishLoading解析、作成、追加しています:polylinepointsArrayoverlay

-(void) connectionDidFinishLoading: (NSURLConnection *) connection
{
userLatitudeArray = [[NSMutableArray alloc]init];
userLongitudeArray = [[NSMutableArray alloc]init];
userIdArray = [[NSMutableArray alloc]init];
userNameArray = [[NSMutableArray alloc]init];
userProfilePicArray = [[NSMutableArray alloc]init];
profilePicURLStringArray = [[NSMutableArray alloc]init];

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

NSArray *trackingDict = [NSJSONSerialization JSONObjectWithData:empJsonData options:kNilOptions error:nil];

if ([trackingDict count] >= 2) {
    for (trackUsersCount = 0; trackUsersCount< trackingDict.count; trackUsersCount++) {
        NSLog(@"trackUsersCount %i", trackUsersCount);

        NSMutableArray *latlongArray = [[NSMutableArray alloc]init];
        latlongArray = [[trackingDict objectAtIndex:trackUsersCount]objectForKey:@"latlong"];

        [userLongitudeArray removeAllObjects];
        [userLatitudeArray removeAllObjects];

        for (int i = 0; i<latlongArray.count; i++) {
            [userLatitudeArray addObject:[[latlongArray objectAtIndex:i]objectForKey:@"lat"]];
            [userLongitudeArray addObject:[[latlongArray objectAtIndex:i]objectForKey:@"long"]];

        }

        NSString *name = [[trackingDict objectAtIndex:trackUsersCount]objectForKey:@"user_firstName"];

        // ProfilePIC URL
        profilePicURLString = [[trackingDict objectAtIndex:trackUsersCount]objectForKey:@"user_profilePicture"];


        [userNameArray addObject:name];
        [profilePicURLStringArray addObject:profilePicURLString];

        int i;
        if (userLatitudeArray.count>1) {

            for (i = 0; i<userLatitudeArray.count; i++) {
                CLLocationCoordinate2D userLocation;
                userLocation.latitude = [[userLatitudeArray objectAtIndex:i]doubleValue];
                userLocation.longitude = [[userLongitudeArray objectAtIndex:i] doubleValue];
                MKMapPoint * pointsArray = malloc(sizeof(CLLocationCoordinate2D)*userLongitudeArray.count);
                pointsArray[i] = MKMapPointForCoordinate(userLocation);
                polyline = [MKPolyline polylineWithPoints:pointsArray count:i];
                free(pointsArray);    
            }
            polyline.title = name;
            [mapView addOverlay:polyline];
        }
   }
  }
}

私がやりたいのはpolyline、ユーザーごとに作成されたそれぞれを制御することです。そのため、ボタンをクリックして色を変更し、非表示/表示できます(1つは自分のトラックを表示/非表示にし、もう1つは他のすべてのユーザー用)。 、これがタイトルを追加する理由です。polyline同じに追加していることがわかりますがoverlay、これは間違っていると思います。しかし、Webサービスに何人のユーザーがいるかわからないので、それらの複数のオーバーレイを追加できます.

最初はタイトルのある特定のものを削除できると思っていましたが、プロパティが更新さpolylineれるとすべて削除されることに気付きました。polyline.title

どんな助けでも大歓迎です!

4

1 に答える 1

1

他のユーザーに関連するこれらのトラックの配列を収集し、現在のユーザーの単一のトラックを保持できます。関数の開始時に配列を消去し、connectionDidFinishLoading現在オーバーレイをマップに追加している場所に配置するとaddOverlay、最後に呼び出す新しい関数に を移動します。

- (void) resetMap
{
  if (showOtherTracks)
  {
      [mapView addOverlays:otherUserTracks];
  } else {
      [mapView removeOverlays:otherUserTracks];
  }
  if (showMyTrack)
  {
      [mapView addOverlay:myTrack];
  } else {
      [mapView removeOverlay:myTrack];
  }
}

ボタンが押されて状態が変化するたびに、これを呼び出すこともできます。

于 2013-02-01T07:30:10.390 に答える