如何处理多个引脚将在一个经度和下降处下降在iphone sdk中的纬度?

时间:2011-08-10 10:39:25

标签: iphone mapkit mkpinannotationview

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

另一个问题是我在彼此之间有一些别针,因为有纬度&经度有一些细微的差别。因此,在那个引脚将要彼此掉落的特定点上,我想给出一个按钮(在引脚弹出窗口上),它可以处理lat和amp;的微小差异。长引脚意味着按钮告诉下一个引脚&一旦按下按钮,弹出窗口应该转到另一个尚未选择的按钮上。打开它的弹出窗口,它也会告诉下一个引脚。我怎么能这样做?

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

获取用户loacation 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)
}

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

1 个答案:

答案 0 :(得分:1)

回答第一个问题:

首先找到您的当前位置,请参阅THIS ANSWER所有评论但请使用以下代码执行此操作。

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

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