1

ピン/注釈をマップビューに配置するアプリケーションを作成しています。ユーザーがピンに近づくと、ピンの色が変わります。すべてが正常に機能し、ピンは希望する場所に配置され、十分に近づくとアラート メッセージが表示されます。しかし、ピンの色を更新する方法がわかりません。注釈を削除して置き換える必要がありますか? 不要なようです。私が探しているのは、注釈を置き換えることなくマップビューを更新/更新することだけです。

CLLocation *place =[[CLLocation alloc] initWithCoordinate:location altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil];
    AddresAnnotation *ann = [[AddresAnnotation alloc] initWithCoordinate:place.coordinate];
    [ann setTitle:[rows placeName]];
    [mapView addAnnotation:ann];

location は CLLocationCoordinate2D
です rows は、さまざまな場所とその情報を含むオブジェクトです

mapView デリゲート メソッドは次のとおりです (最後の「}」がコード サンプルの外にある理由はわかりません)。

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{

    if (annotation != self.mapView.userLocation) {
        annView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Plats"] autorelease];
        [annView setPinColor:MKPinAnnotationColorRed];
        annView.animatesDrop = TRUE;
        annView.canShowCallout = YES;
        return annView;
    } else {
        zoomButton.enabled = TRUE;
        return nil;
    }

}

4

1 に答える 1

0

場所がユーザーからxマイル以内にあるかどうかをテストするループを作成する必要があります。これは私が自分のコードで使用したことの例です

for (CLLocation *prox in locations) {
    NSLog(@"prox %@", prox);
    float distanceFromLocation = [mapView.userLocation.location distanceFromLocation:prox]/1609.344;
    NSLog(@"distance %f", distanceFromLocation);
    if (distanceFromLocation <= 10) {
        NearbyLocation *nearbyLocation = [[NearbyLocation alloc]init];
        NSString *key = [NSString stringWithFormat:@"%d", index];
        nearbyLocation.title = [storedTitles objectForKey:key];
        nearbyLocation.loction = prox;
        nearbyLocation.subtitle = [NSString stringWithFormat:@"%.1f miles away", distanceFromLocation];
        nearbyLocation.lat = prox.coordinate.latitude;
        nearbyLocation.lon = prox.coordinate.longitude;
        [newArray addObject:nearbyLocation];
        [nearbyLocation release];
    }
    index++;
}
NSLog(@"new array %d prox %d", [newArray count], [proximityOutlet.nearbyLocations count]);
if ([newArray count] > [proximityOutlet.nearbyLocations count]) {
    NSLog(@"set array");
    // if the new array has actually added any objects, set the array and switch views;
    proximityOutlet.nearbyLocations = newArray;
    //[self.navigationController pushViewController:proximityOutlet animated:YES];
    proximityOutlet.modalPresentationStyle = UIModalPresentationPageSheet;
    proximityOutlet.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:proximityOutlet animated:YES];
}
[newArray release];

}

基本的に、場所の配列を検索するループを作成します。[mapView.userLocation.location distanceFrom:prox] /1609.334//ユーザーのxマイル以内かどうかを確認するためにテストされる場所のインスタンスを表すprox。

次に言う

if ([mapview.userLocation.location distanceFrom:prox]/1609.334 < (however many miles)){

        annotationView = x;
}
于 2011-06-16T04:51:41.567 に答える