1

我有一项任务是使用 map-kit 开发应用程序。
我从服务器获得了多个引脚,而我必须从用户当前位置删除一个引脚。这个用户当前位置的 pin 应该是绿色的,其他来自服务器的 pin 应该是另一种颜色。我该怎么做?

另一个问题是我得到了一些针脚,因为纬度和经度有一些细微的差异。因此,在引脚将要相互掉落的那个特定点上,我想给一个按钮(在引脚弹出窗口上)来处理经纬度引脚的微小差异,这意味着该按钮会告诉下一个引脚&尽快当按下按钮时,弹出窗口应该在另一个尚未选择的引脚上打开并打开它的弹出窗口,该窗口也告诉下一个引脚。我怎样才能做到这一点??

我正在使用这样的代码来创建引脚

用于获取用户定位销

        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;}

这个处理那里事件的代码意味着 pin 正在调用按钮操作。

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

我的所有代码都正常工作。我只想回答上面给出的问题..请帮助我

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;
}

如果您的第二个问题,您最好手动检查纬度和经度之间的差异,如果差异足够大(根据您的要求),则通过它。

于 2011-08-10T11:15:56.430 回答