Custom icon and default current location icon on Map when needed in iPhone
-
02-03-2021 - |
Question
I have an application where in I give user 2 options. 1. Select your current location through GPS 2. Enter your current location manually.
What I want is When user has selected thh first option then I have to display the blue animated icon for current location And when user has selected 2nd option then I have to display my own custom icon for current location.
Is it possible? Suggestions are most welcomed.
Thanks.
Solution
First set a boolean variable for Example isInGPSMode
now apply following code
if(isInGPSMode==TRUE)
{
mapViewHome.showsUserLocation=TRUE;
}
else
{
mapViewHome.showsUserLocation=FALSE;
}
and handle the map view delegate as
- (MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(id <MKAnnotation>) annotation
{
if(annotation==mapViewHome.userLocation)
{
return nil;
}
addAnnonation *tempAnnonation=(addAnnonation *)annotation;
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.pinColor = MKPinAnnotationColorRed;
return annView;
}
for further details refer the link http://developer.apple.com/iphone/library/samplecode/MapCallouts/Introduction/Intro.html
hope this helps
OTHER TIPS
Rupesh's solution in Swift :
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
// Keep default pin for user location
if myMapView.userLocation.isEqual(annotation) {
return nil;
}
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier("pin") as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")
pinView!.canShowCallout = true
pinView!.animatesDrop = true
pinView!.pinColor = .Red
}
else {
pinView!.annotation = annotation
}
return pinView
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow