问题
I have some problem regarding Annotations in mapView. Lets have one glance on my requirement.
I want to give choice to user to choose location for meeting.
There are two options.
1) I should give list of near by data
Or
2) He can drag and drop pin anywhere he wants !
For that I have created one segment. First index for near by data and Second index for dropping a pin.
For First option ("near by") I need to fetch near by data from location of Seller, location of Buyer and midpoint between seller and buyer. So I call google api and get data by passing latitude and longitude three times. There is no issue when I get data first time. My array fill up with all data (included 3 responses) and pin color also changes as per requirement.
Buyer (Red Color) Seller ( Purple) Mid Point (Green)
Now when I click on drop pin all data are removed from array and one pin is dropped on map.
Till now it works fine !
But when you again click on "near by", Problem starts ! No doubt it gives me data as I want but pin colors don't maintained.
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([segmentND selectedSegmentIndex]==0) {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString* BridgeAnnotationIdentifier = @"bridgeAnnotationIdentifier";
MKPinAnnotationView* pinView = (MKPinAnnotationView *)
[myMapView dequeueReusableAnnotationViewWithIdentifier:BridgeAnnotationIdentifier];
if (!pinView)
{
MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:BridgeAnnotationIdentifier] autorelease];
switch (self.pinColor) {
case 0:
{
customPinView.pinColor = MKPinAnnotationColorPurple;
}
break;
case 1:
{
customPinView.pinColor = MKPinAnnotationColorRed;
}
break;
case 2:
{
customPinView.pinColor = MKPinAnnotationColorGreen;
}
break;
default:
break;
}
customPinView.canShowCallout = YES;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = rightButton;
return customPinView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
else {
// Code of dragging dropping pin. It works Fine.s
}
}
I am attaching image for more idea.
Please give me solution or any another way to implement it. Remember Pin color is compulsory to differentiate Seller Buyer and Midpoint !
回答1:
The trouble with your current approach is that self.pinColor doesn't change according to which annotation the map is needing a view for. It can and will call viewForAnnotation when ever it feels like it. Maybe the map has been scrolled around and one pin has just come back into view. Maybe the app was put into the background and is just being brought back into view by the user. What ever the reason you need to analyze the annotation is is passing in to determine which pin colour to use in the view. What object are you using for your annotation? If it was HSAnno and it had a property called pinColor you'd do something like this instead of your switch statement.
HSAnno* currentAnno = (HSAnno *)annotation;
pinView.pinColor = currentAnno.pinColor;
That way no matter what annotation needed to be redrawn viewForAnnotation would always return the right coloured pin.
回答2:
You set your pincolor in that code part when no reusable pin exists.
if (!pinView)
....
customPinView.pinColor = MKPinAnnotationColorPurple;
....
}
When viewForAnnotation
is called and reusable pins are found they are used. There is where the wrong color pin is taken.
Set your pincolor in the
else
{
pinView.annotation = annotation;
}
part and it should work fine.
回答3:
Think its got mistaken here
else
{
pinView.annotation = annotation;
}
return pinView;
Correct it to
else
{
pinView.annotation = annotation;
return pinView;
}
回答4:
Here's I little modified your code & MKMapView
delegate. Previously you've changing pin
color inside if
that make it to call only for once when first time MKMapView
loads.
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString* BridgeAnnotationIdentifier = @"bridgeAnnotationIdentifier";
MKPinAnnotationView* pinView = (MKPinAnnotationView *)
[mapView dequeueReusableAnnotationViewWithIdentifier:BridgeAnnotationIdentifier];
if (!pinView)
{
pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:BridgeAnnotationIdentifier] autorelease];
}
else
{
pinView.annotation = annotation;
}
switch (self.pinColor) {
case 0:
{
pinView.pinColor = MKPinAnnotationColorPurple;
}
break;
case 1:
{
pinView.pinColor = MKPinAnnotationColorRed;
}
break;
case 2:
{
pinView.pinColor = MKPinAnnotationColorGreen;
}
break;
default:
break;
pinView.canShowCallout = YES;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
return pinView;
}
return pinView;
}
P.S. I've removed your first UISegment
condition to check at my side, please add it as is, when you implement.
来源:https://stackoverflow.com/questions/12210860/cant-be-changed-pin-color-when-i-change-segement-in-mkmapview