1

map-kitを使用してアプリケーションを開発するタスクが1つあります。
サーバーから複数のピンを取得していますが、ユーザーの現在の場所から1つのピンをドロップする必要があります。このユーザーの現在地のピンは緑色で、サーバーからのその他のピンは別の色である必要があります。これを行うにはどうすればよいですか?

もう1つの質問は、緯度と経度に若干の違いがあるため、互いにいくつかのピンを取得していることです。したがって、ピンが互いにドロップされる特定のポイントで、緯度と経度のピンのわずかな違いを処理する1つのボタン(ピンポップアップウィンドウ上)を指定したいので、ボタンは次のピンとすぐに通知しますボタンが押されると、ポップアップウィンドウはまだ選択されていない別のピンに移動し、次のピンも通知するポップアップウィンドウを開きます。これどうやってするの??

私はピンを作成するためにこのようなコードを使用しています

ユーザー位置ピンを取得するため

        Place* current = [[[Place alloc] init] autorelease];
    current.name = @"You are here.";
    current.latitude = coordinate.latitude;
    current.longitude = coordinate.longitude;
    PlaceMark *from = [[[PlaceMark alloc] initWithPlace:current] autorelease];              
    [mapView addAnnotation:from];

サーバーロケーションピンを取得するため。

    int s=1;
    for (j=0 ; j < i - 1   ; j++) 
    {
        //points = [[NSString alloc] initWithFormat:@"point%d",s];
        reports = [NSArray arrayWithArray:lat];
        NSString *titles = [[NSString alloc] initWithFormat:@"%@",[tit objectAtIndex:j]];
        NSString *description = [[NSString alloc] initWithFormat:@"%@",[des objectAtIndex:j]];
        float latitude=[[lat objectAtIndex:j] floatValue];
        float longitude=[[lon objectAtIndex:j] floatValue];
        Place* home = [[[Place alloc] init] autorelease];
        home.name = [NSString stringWithFormat:@"%@",titles];
        home.description = [NSString stringWithFormat:@"%@",description];
        home.latitude = latitude;
        home.longitude = longitude;
        PlaceMark *from = [[[PlaceMark alloc] initWithPlace:home] autorelease];             [mapView addAnnotation:from];
        s++;
        [self centerMap];
    }

そこにポップアップウィンドウを作成するためのこのコード。

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation{
if (annotation == mapView.userLocation)
return nil;
MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: @"asdf"];
    if (pin == nil)
    pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: @"asdf"] autorelease];
else
pin.annotation = annotation;
pin.userInteractionEnabled = YES;
UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
[disclosureButton setFrame:CGRectMake(0, 0, 30, 30)];
pin.rightCalloutAccessoryView = disclosureButton;
pin.pinColor = MKPinAnnotationColorRed;
pin.animatesDrop = YES;
[pin setEnabled:YES];
[pin setCanShowCallout:YES];
return pin;}

イベントを処理するためのこのコードは、ピンがボタンアクションを呼び出していることを意味します。

 - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{ nslog (do something)
}

私のすべてのコードは正しく機能しています。私は上記の質問に答えたいだけです..plsは私を助けます

4

1 に答える 1

1

回答最初の質問について:

最初にあなたの現在の場所を見つけて、すべてのコメントでこの答えを参照してくださいしかし、これを行うには次のコードを使用してください。

#pragma mark -
#pragma mark Location Manager functions

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{   NSLog(@"Inside Location Delegate"); 
/*NSString *val=[[NSString alloc] initWithFormat:@"Previous Location is:\n Lattitude : %f\n Longitude : %f \n\n Current location is :\n Lattitude : %f\n Longitude : %f",oldLocation.coordinate.latitude, oldLocation.coordinate.longitude,newLocation.coordinate.latitude, newLocation.coordinate.longitude];
NSLog(@"%@",val);*/
[self setMapCenter:newLocation.coordinate];

PlaceMark *addAnnotation = [[[PlaceMark alloc] initWithCoordinate:newLocation.coordinate] retain];
[addAnnotation setTitle:@"Your Location"];
[addAnnotation setSubTitle:@""];        
[self._mapView addAnnotation:addAnnotation];
userAnnoFlag = TRUE;

[self._mapView selectAnnotation:[[self._mapView annotations] lastObject] animated:YES];

[self.locationManager stopUpdatingLocation];
}

次に、次の方法を使用してピンを設定します。

#pragma mark -
#pragma mark Annotations Functions

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
@try {
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MyPin"];  
    annView.animatesDrop=FALSE;
    annView.canShowCallout = YES;
    [annView setSelected:YES];
    if (userAnnoFlag == TRUE){
        annView.pinColor = MKPinAnnotationColorRed;
        userAnnoFlag = FALSE;
    }
    if ([mapView userLocation]) {
        annView.pinColor = MKPinAnnotationColorRed;
    }
    else{
        annView.pinColor = MKPinAnnotationColorGreen;
    }   
    annView.calloutOffset = CGPointMake(-5, 5); 
    return annView;
}
@catch (NSException * e) {      
}
@finally {      
}
return 0;
}

また、2番目の質問の場合は、緯度と経度の違いを手動で確認し、十分な違いがある場合はそれを渡します(要件に応じて)。

于 2011-08-10T11:15:56.430 に答える