iPad Mapkit – Change the title of “Current Location”

In the map view, I display the current user location. Click the pushpin to display “Current Location”. I want to change it to “My Current Location”. How can I change it. < br>Also, I want to change the pin color of the current user position in the timer. Some things are like every second it should change the color between green, purple and red. Is it possible?

I am using the map toolkit to display the default location, and then manipulate the annotation pin color as follows:

- (MKAnnotationView *)mapView:( MKMapView *)map viewForAnnotation:(id )annotation{
static NSString *AnnotationViewID = @"annotationViewID";
SolarAnnotationView* annotationView = (SolarAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID] >if(annotationView == nil)
{
if([self CLLocationCoordinate2DEquals:mapView.userLocation.location.coordinate withSecondCoordinate:[annotation coordinate]]) //Show current location with green pin
{
annotationView = [[SolarAnnotationView alloc] initWithAnnotation:annotation];
annotationView.delegate = self;
[annotationView setPinColor:MKPinAnnotationColorGreen];
}
else
{
annotationView = [[SolarAnnotationView alloc] initWithAnnotation:annotation];
annotationView.delegate = self;
}
}

return annotati onView;

)

- (BOOL) CLLocationCoordinate2DEquals:(const CLLocationCoordinate2D)lhs withSecondCoordinate:(const CLLocationCoordinate2D) rhs{
const CLLocationDegrees DELTA = 0.001;
return fabs(lhs.latitude-rhs.latitude) <= DELTA && fabs(lhs.longitude-rhs.longitude) <= DELTA;

}

If you let the map view display the default annotation view of the user’s location (blue dot), it’s easier to implement (and you can get Nice blue dots with cool animated zoom circles).

If you have to use pushpin images instead of blue dots to show the user's location, more work is needed.

First, the simple way to use blue dots:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id )annotation{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
((MKUserLocation *)annotation).title = @"My Current Location";
return nil; //return nil to use default blue dot view
}

//Your existing code for viewForAnnotation here (with some corrections)...
static NSString *AnnotationViewID = @"annotationViewID";
SolarAnnotationView* annotationView = (SolarAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if(annotationView == nil)
{
{
annotationView = [[[SolarAnnotationView alloc] initWithAnnotation:annotation] autorelease];
//added autorelease above to avoid memory leak
annotationView.delegate = self;
}
}

//update annotation in view in case we are re-using a view
annotationView.annotation = annotation;

return annotationView;
}

If you want to use a custom annotation view for the user location, you should put the pin color change code in the custom view. One way to change the color regularly is to use performSelector:withObject:afterDelay:. in SolarAnnotationView. In m, add the following two methods:

-(void)startChangingPinColor
{
switch (self.pinColor) {
case MKPinAnnotationColorRed :
self.pinColor = MKPinAnnotationColorGreen;
break;
case MKPinAnnotationColorGreen:
self.pinColor = MKPinAnnotationColorPurple;
break;
default:
self.pinColor = MKPinAnnotationColorRed;
break;
}
[self performSelector:@selector(startChangingPinColor) withObject:nil afterDelay:1.0];
}

-(void)stopChangingPinColor
{
[NSObject cancelPreviousPerformRequestsWithTarget:self] ;
}

Also add the method title to the SolarAnnotationView.h file.

Then change the viewForAnnotation method like this:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id )annotation{
static NSString *AnnotationViewID = @"annotationViewID";
SolarAnnotationView* annotationView = (SolarAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if(annotationView == nil)
{
{
annotationView = [[[SolarAnnotationView alloc] initWithAnnotation:a nnotation] autorelease];
annotationView.delegate = self;
}
}

//Update annotation in view in case we are re-using a view.. .
annotationView.annotation = annotation;

//Stop pin color changing in case we are re-using a view that has it on
//and this annotation is not user location...
[annotationView stopChangingPinColor];

if([self CLLocationCoordinate2DEquals:mapView.userLocation.location.coordinate withSecondCoordinate:[annotation coordinate]]) //Show current location with green pin
{
[annotationView setPinColor:MKPinAnnotationColorGreen];
annotationView.canShowCallout = YES;
((MKPointAnnotation *)annotation).title = @"My Current Location";
[annotationView startChangingPinColor];
}

return annotationView;
}

In the map view, I display the current User location. Click the pushpin to display "Current Location". I want to change it to "My Current Location". How can I change it.
Also, I want to count Change the pin color of the current user position in the timer. Some things are like every second it should change the color between green, purple and red. Can it?

I am using the map toolkit to display the default location, and then manipulate the annotation pin color as follows:

- (MKAnnotationView *)mapView:( MKMapView *)map viewForAnnotation:(id )annotation{
static NSString *AnnotationViewID = @"annotationViewID";
SolarAnnotationView* annotationView = (SolarAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID] >if(annotationView == nil)
{
if([self CLLocationCoordinate2DEquals:mapView.userLocation.location.coordinate withSecondCoordinate:[annotation coordinate]]) //Show current location with green pin
{
annotationView = [[SolarAnnotationView alloc] initWithAnnotation:annotation];
annotationView.delegate = self;
[annotationView setPinColor:MKPinAnnotationColorGreen];
}
else
{
annotationView = [[SolarAnnotationView alloc] initWithAnnotation:annotation];
annotationView.delegate = self;
}
}

return annotationV iew;

)

- (BOOL) CLLocationCoordinate2DEquals:(const CLLocationCoordinate2D)lhs withSecondCoordinate:(const CLLocationCoordinate2D) rhs{
const CLLocationDegrees DELTA = 0.001;
return fabs(lhs.latitude-rhs.latitude) <= DELTA && fabs(lhs.longitude-rhs.longitude) <= DELTA;

}

If you let the map view display the default annotation view of the user's location (blue dot), it is easier to implement (and you can get a beautiful blue dot with a cool animated zoom circle)

If you have to use a pushpin image instead of a blue dot to show the user's location, more work is needed.

First, a simple method to use the blue dot:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id )annotation{
if ([annotation isKindOfClass:[MKUserLocation class]])< br /> {
((MKUserLocation *)annotation).title = @"My Current Location";
return nil; //return nil to use default blue dot view
}

//Your existing code for viewForAnnotation here (with some corrections)...
static NSString *AnnotationViewID = @"annotationViewID";
SolarAnnotationView* annotatio nView = (SolarAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if(annotationView == nil)
{
{
annotationView = [[[SolarAnnotationView alloc] initWithAnnotation:annotation] autorelease];
//added autorelease above to avoid memory leak
annotationView.delegate = self;
}
}

//update annotation in view in case we are re-using a view
annotationView.annotation = annotation;

return annotationView;
}

If you want to use a custom annotation view In the user’s position, the pin color change code should be placed in the custom view. One way to change the color regularly is to use performSelector: withObject: afterDelay:. In SolarAnnotationView.m, add the following two methods:

-(void)startChangingPinColor
{
switch (self.pinColor) {
case MKPinAnnotationColorRed:
self.pinColor = MKPinAnnotationColorGreen;< br /> break;
case MKPinAnnotationColorGreen:
self.pinColor = MKPinAnnotationColorPurple;
break;
default:
self.pinColor = MKPinAnnotationColorRed;
break;
}
[self performSelector:@selector(startChangingPinColor) withObject: nil afterDelay:1.0];
}

-(void)stopChangingPinColor
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
}

< p>Also add the method title to the SolarAnnotationView.h file.

Then change the viewForAnnotation method like this:

- (MKAnnotationView *)mapView: (MKMapView *)map viewForAnnotation:(id )annotation{
static NSString *AnnotationViewID = @"annotationViewID";
SolarAnnotationView* annotationView = (SolarAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:
/> if(annotationView == nil)
{
{
annotationView = [[[SolarAnnotationView alloc] initWithAnnotation:annotation] autorelease];
annotationView.delegate = s elf;
}
}

//Update annotation in view in case we are re-using a view...
annotationView.annotation = annotation;

//Stop pin color changing in case we are re-using a view that has it on
//and this annotation is not user location...
[annotationView stopChangingPinColor];

if([self CLLocationCoordinate2DEquals:mapView.userLocation.location.coordinate withSecondCoordinate:[annotation coordinate]]) //Show current location with green pin
{
[annotationView setPinColor: MKPinAnnotationColorGreen];
annotationView.canShowCallout = YES;
((MKPointAnnotation *)annotation).title = @"My Current Location";
[annotationView startChangingPinColor];
}

return annotationView;
}

Leave a Comment

Your email address will not be published.