MapKit doesn't show Blue Dot for Current Location - iphone

I am a newbie in MapKit for iPhone and tried to implement this, for some reason I can't see the current location blue dot, anyone else had this issue????
#import "DetailMapViewController.h"
#import "mapAnnotations.h"
#implementation DetailMapViewController
#synthesize inStock;
-(void)getlocation:(CLLocationCoordinate2D)loc
{
location = loc;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = #"Street View";
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
mapView.delegate=self;
//MKCoordinateRegion region;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 5000, 5000);
mapAnnotations *ann = [[mapAnnotations alloc] init];
ann.title = #"";
ann.subtitle = #"";
ann.coordinate = region.center;
mapView.showsUserLocation = YES;
[mapView addAnnotation:ann];
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
[self.view insertSubview:mapView atIndex:0];
}
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"currentloc"];
if (annotation == mapView.userLocation)
{
annView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"blueDot"];
if (annView != nil)
{
annView.annotation = annotation;
}
else
{
annView = [[[NSClassFromString(#"MKUserLocationView") alloc] initWithAnnotation:annotation reuseIdentifier:#"blueDot"] autorelease];
}
}
if([inStock isEqual:#"yes"]){
annView.pinColor = MKPinAnnotationColorGreen;
}
if([inStock isEqual:#"no"]){
annView.pinColor = MKPinAnnotationColorRed;
}
if([inStock isEqual:#"unknown"]){
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"greyPin.png"]];
[annView addSubview:imageView];
}
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
}
- (void)dealloc {
[super dealloc];
}
#end

The way the viewForAnnotation is currently written, it should actually crash when trying to show the current location because the "blue dot" annotation view doesn't have the pinColor or animatesDrop properties.
Try changing it to the following:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
if ([annotation isKindOfClass:MKUserLocation.class]) {
//user location view is being requested,
//return nil so it uses the default which is a blue dot...
return nil;
}
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"currentloc"];
if([inStock isEqual:#"yes"]){
annView.pinColor = MKPinAnnotationColorGreen;
}
if([inStock isEqual:#"no"]){
annView.pinColor = MKPinAnnotationColorRed;
}
if([inStock isEqual:#"unknown"]){
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"greyPin.png"]];
[annView addSubview:imageView];
}
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
}
On the simulator, the user's location will be Cupertino, CA, USA (a little south of San Francisco). If your own annotation is not within 5000 meters of that, you won't see the blue dot. You'll have to zoom out to see it.

self.mapView.showsUserLocation = YES;

Related

MapView MKAnnotation not showing callout on MKMapView

I'm displaying a few map pins on a map view. I can't figure out why the callout won't show when an annotation is touched. Here's my code. I have some conditional code in the viewDidLoad method to display a different pin image depending on a category id. The custom pins display correctly, but when you touch them nothing happens.
thanks for any help with this.
- (void)viewDidLoad {
[super viewDidLoad];
[self requestData];
CLLocationCoordinate2D centerCoord = CLLocationCoordinate2DMake(44.2632, 11.4403);
MKCoordinateSpan span;
span.latitudeDelta=5;
span.longitudeDelta=5;
MKCoordinateRegion ITRegion = MKCoordinateRegionMake(centerCoord, span);
ITRegion.span=span;
[mapView setRegion: ITRegion animated: YES];
mapView.delegate=self;
[self.mapView setShowsUserLocation:YES];
for (int i=0; i<[publicDataArray count]; i++) {
MKPointAnnotation* annotation= [MKPointAnnotation new];
CLLocationCoordinate2D coordinate;
coordinate.latitude = [[[[publicDataArray objectAtIndex:i] objectForKey:#"address"] objectForKey:#"lat"] doubleValue];
coordinate.longitude = [[[[publicDataArray objectAtIndex:i] objectForKey:#"address"] objectForKey:#"lng"] doubleValue];
NSString *catId = [NSString stringWithFormat:#"%#",[[publicDataArray objectAtIndex:i] objectForKey:#"category_id"]];
if ([catId isEqual: #"213"]) {
annView.image = [UIImage imageNamed:#"comprareMap.png"];//sets image for pin
} else if ([catId isEqual:#"217"]) {
annView.image = [UIImage imageNamed:#"vedereMap.png"];//sets image for pin
} else if ([catId isEqual:#"221"]) {
annView.image = [UIImage imageNamed:#"mangiareMap.png"];//sets image for pin
}
annotation.coordinate = coordinate;
[mapView addAnnotation: annotation];
}
}
- (MKAnnotationView *) mapView:(MKMapView *)mapingView viewForAnnotation:(id <MKAnnotation>) annotation {
annView = nil;
if(annotation != mapingView.userLocation)
{
static NSString *defaultPinID = #"";
annView = (MKAnnotationView *)[mapingView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( annView == nil )
annView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] ;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = rightButton;
annView.canShowCallout = YES;
}
return annView;
}

My viewForAnnotation function is never called?

I have made these pieces of code for my custom annotation:
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.delegate=self;
[self AddLocations];
[self initiateMap];
......
this is my addLocation function:
-(id<MKAnnotation>)addAnnotationWithTitle: (NSString*) title coordinate:(CLLocationCoordinate2D)
Location imageName:(NSString*) imageName{
CustomAnnotation *annotation = [[CustomAnnotation alloc]init];
annotation.title = title;
annotation.subtitle = #"This is a subtitle";
[annotation setCoordinate:Location];
annotation.imageName = imageName;
return annotation;
}
and my CustomAnnotation Class:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
NSLog(#"Test");
if([annotation isKindOfClass:[MKUserLocation class]]){
return nil;
}
if([annotation isKindOfClass:[CustomAnnotation class]]){
CustomAnnotation *customAnnotation = annotation;
static NSString* annotationid = #"Annotation";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationid];
if(!annotationView){
annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationid];
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}else{
annotationView.annotation = annotation;
}
annotationView.image = [UIImage imageNamed:customAnnotation.imageName];
return annotationView;
}
return nil;
}
For some reason the viewForAnnotation function is never used. Can anyone help me out here?
Thanks

How do I change the pin color of an annotation in map kit view

I would like to know how to change the pin color of an annotation in map kit. Currently Im using the following code.
//King Solomon's Lodge No. 194
CLLocationCoordinate2D kingsolomons;
kingsolomons.latitude = 38.052041;
kingsolomons.longitude = -78.683218;
Annotation *kingsolomonslodge = [[Annotation alloc] init];
kingsolomonslodge.coordinate = kingsolomons;
kingsolomonslodge.title = #"King Solomon's Lodge No. 194";
kingsolomonslodge.subtitle = #"Charlottesville, VA";
[self.myMapView addAnnotation:kingsolomonslodge];
I have googled this question a few times but Im unsure on how to implement this into the current code that I have.
- (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;
// NSLog(#"%#",annotation.title);
NSString *titlename=#"xyz";
if ([annotation.title isEqualToString:titlename]) {
pin.pinColor = MKPinAnnotationColorGreen;
// pin.image=[UIImage imageNamed:#"arrest.png"] ;
}
else{
pin.pinColor= MKPinAnnotationColorPurple;
}
pin.userInteractionEnabled = YES;
UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeCustom];
// //pin.image=[UIImage imageNamed:#"arrest.png"] ;
pin.rightCalloutAccessoryView = disclosureButton;
//pin.pinColor = MKPinAnnotationColorRed;
pin.animatesDrop = YES;
[pin setEnabled:YES];
[pin setCanShowCallout:YES];
return pin;
}
You can use pinColor property if MKAnnotation class.
kingsolomonslodge.pinColor = MKPinAnnotationColorGreen;
Another way is you can use image instead of the default pin using viewForAnnotation: delegate:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *identifier = #"MyLocation";
if ([annotation isKindOfClass:[yourAnnotationLocation class]])
{
MKAnnotationView *annotationView = (MKAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:#"yourImage.png"];//here we use a nice image instead of the default pins
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
else
{
annotationView.annotation = annotation;
}
return annotationView;
}
return nil;
}
try this man...
kingsolomonslodge.pinColor = AnnotationColorRed;
let me know whether it is workihg or not
happy coding!!!!
If you want to apply custom color you can add image also.
MKAnnotationView *pinView = nil;
pinView.image = [UIImage imageNamed:#"pinks.jpg"];
Since iOS 9:
kingsolomonslodge.pinTintColor = UIColor.greenColor;

why it show last pin's type of call out annotation in my mapview

I am designing a map view in which i am dropping no. of pins. I have also add a custom view on every pin. Now problem is that when my map view add all pins then on map it don't show my last pin. Instead of last pin it will show only custom view which is not hidden when i tap on that. And on some pins it not add custom view. I am showing my code below...
class.h
#interface MappingBusinesses : UIViewController {
MKMapView *_mapView;
CalloutMapAnnotation *_calloutAnnotation;
MKAnnotationView *_selectedAnnotationView;
Places *Place_Object;
DisplayMap *dm;
NSMutableArray *routepoints;
CSMapRouteLayerView *routeView;
UICRouteOverlayMapView *routeOverlayView;
UICGDirections *diretions;
Yelp_OnTheWayAppDelegate *appDelegate;
NSArray *wayPoints;
UICGTravelModes travelMode;
DetailView *DV_Object;
UIActivityIndicatorView *aiv;
UILabel *label;
UIView *prosView;
BusinessData *bd_object;
}
class.m
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (Yelp_OnTheWayAppDelegate *)[[UIApplication sharedApplication] delegate];
self.title=#"Map";
//[NSThread detachNewThreadSelector:#selector(start) toTarget:self withObject:nil];
label.text=[NSString stringWithFormat:#"%d places along your route",[appDelegate.busines_Aray count]];
[self.mapView setMapType:MKMapTypeStandard];
[self.mapView setZoomEnabled:YES];
[self.mapView setScrollEnabled:YES];
[self.mapView setDelegate:self];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = appDelegate.start_lat;
region.center.longitude = appDelegate.start_long;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[self.mapView setRegion:region animated:YES];
dm=[[DisplayMap alloc]init];
for(int i=0;i<[appDelegate.mapPin_aray count];i++)
{
CLLocationCoordinate2D coord;
coord.latitude=[[appDelegate.mapPin_lat objectAtIndex:i] floatValue];
coord.longitude=[[appDelegate.mapPin_long objectAtIndex:i] floatValue];
self.calloutAnnotation = [[CalloutMapAnnotation alloc] initWithLatitude:coord.latitude andLongitude:coord.longitude] ;
[self.mapView addAnnotation:self.calloutAnnotation];
NSLog(#"coordinate of number%i is %f and %f",i,coord.latitude,coord.longitude);
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
static NSString *identifier = #"RoutePinAnnotation";
if ([annotation isKindOfClass:[UICRouteAnnotation class]]) {
MKPinAnnotationView *pinAnnotation = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(!pinAnnotation) {
pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
}
if ([(UICRouteAnnotation *)annotation annotationType] == UICRouteAnnotationTypeStart) {
pinAnnotation.pinColor = MKPinAnnotationColorGreen;
} else if ([(UICRouteAnnotation *)annotation annotationType] == UICRouteAnnotationTypeEnd) {
pinAnnotation.pinColor = MKPinAnnotationColorPurple;
} else {
pinAnnotation.pinColor = MKPinAnnotationColorPurple;
}
pinAnnotation.animatesDrop = YES;
pinAnnotation.enabled = YES;
pinAnnotation.canShowCallout = YES;
return pinAnnotation;
}
static NSString * const kPinAnnotationIdentifier = #"PinIdentifier";
if (annotation == self.calloutAnnotation) {
CalloutMapAnnotationView *calloutMapAnnotationView = (CalloutMapAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:kPinAnnotationIdentifier];
if (!calloutMapAnnotationView) {
calloutMapAnnotationView = [[[CalloutMapAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:#"CalloutAnnotation"] autorelease];
calloutMapAnnotationView.contentHeight = 70.0f;
bd_object=[appDelegate.busines_Aray objectAtIndex:self.calloutAnnotation.pinID];
UITextField *txtname=[[UITextField alloc] initWithFrame:CGRectMake(0, 5, 150, 30)];
txtname.text=[NSString stringWithFormat:#"%#",bd_object.name_business ];
txtname.userInteractionEnabled=NO;
txtname.font = [UIFont boldSystemFontOfSize:18];
txtname.font=[UIFont fontWithName:#"Arial" size:18];
txtname.textColor = [UIColor whiteColor];
txtname.opaque = false;
txtname.backgroundColor = [UIColor clearColor];
[calloutMapAnnotationView.contentView addSubview:txtname];
[txtname release];
UILabel *gpsCoordinatesLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, 70, 20)];
gpsCoordinatesLabel.text = #"Categories:";
gpsCoordinatesLabel.font = [UIFont boldSystemFontOfSize:14];
gpsCoordinatesLabel.font=[UIFont fontWithName:#"Arial" size:14];
gpsCoordinatesLabel.textColor = [UIColor whiteColor];
gpsCoordinatesLabel.opaque = false;
gpsCoordinatesLabel.backgroundColor = [UIColor clearColor];
gpsCoordinatesLabel.textAlignment = UITextAlignmentRight;
[calloutMapAnnotationView.contentView addSubview:gpsCoordinatesLabel];
[gpsCoordinatesLabel release];
UITextField *txtcate=[[UITextField alloc] initWithFrame:CGRectMake(70, 40, 100, 30)];
txtcate.text=[NSString stringWithFormat:#"%#", bd_object.cat_business];
txtcate.userInteractionEnabled=NO;
txtcate.font = [UIFont boldSystemFontOfSize:18];
txtcate.font=[UIFont fontWithName:#"Arial" size:18];
txtcate.textColor = [UIColor whiteColor];
txtcate.opaque = false;
txtcate.backgroundColor = [UIColor clearColor];
[calloutMapAnnotationView.contentView addSubview:txtcate];
[txtcate release];
UITextField *txtReview=[[UITextField alloc] initWithFrame:CGRectMake(190, 40, 40, 30)];
txtReview.text=[NSString stringWithFormat:#"%d", bd_object.noofreview];
txtReview.userInteractionEnabled=NO;
txtReview.font = [UIFont boldSystemFontOfSize:18];
txtReview.textColor = [UIColor whiteColor];
txtReview.opaque = false;
txtReview.backgroundColor = [UIColor clearColor];
[calloutMapAnnotationView.contentView addSubview:txtReview];
[txtReview release];
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(210, 40, 31, 23)];
[imgView setImage:[UIImage imageNamed:#"reviewwhite.png"]];
[calloutMapAnnotationView.contentView addSubview:imgView];
[imgView release];
}
calloutMapAnnotationView.parentAnnotationView = self.selectedAnnotationView;
calloutMapAnnotationView.mapView = self.mapView;
return calloutMapAnnotationView;
}
else {
MKPinAnnotationView *annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:#"CustomAnnotation"] autorelease];
annotationView.canShowCallout = NO;
annotationView.pinColor = MKPinAnnotationColorRed;
return annotationView;
}
MKAnnotationView *av;
return av;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
if (self.calloutAnnotation != nil) {
self.calloutAnnotation = [[CalloutMapAnnotation alloc] initWithLatitude:view.annotation.coordinate.latitude andLongitude:view.annotation.coordinate.longitude];
} else {
self.calloutAnnotation.latitude = view.annotation.coordinate.latitude;
self.calloutAnnotation.longitude = view.annotation.coordinate.longitude;
}
[self.mapView addAnnotation:self.calloutAnnotation];
self.selectedAnnotationView = view;
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {
if (self.calloutAnnotation != nil) {
[self.mapView removeAnnotation: self.calloutAnnotation];
}
}
Not sure, but I think the issue might be that during your initial loop in viewDidLoad, you're remembering the annotations in self.calloutAnnotation and then checking for it in didSelect and didDeselect.
The problem is that self.calloutAnnotation will be replaced every time round the init loop, so at the end, it will only be referencing the last annotation added. For example, when user selects a different annotation, you change self.calloutAnnotation's lat/long and add it again, but that will change the lat/long of the last annotation added in the array.

How to switch to another view on click of annotation pin in MKMapView in iPhone SDK?

1 i am using multiple pin in mkmapview and i want to display detail in next view of tapped pin
#import "mapViewController.h"
#import "displaymap.h"
#import "second.h"
#implementation mapViewController
#synthesize mapview;
#synthesize selectedAnnotationsl;
- (void)viewDidLoad {
[super viewDidLoad];
[mapview setMapType:MKMapTypeStandard];
[mapview setZoomEnabled:YES];
[mapview setScrollEnabled:YES];
MKCoordinateRegion region={{0.0,0.0},{0.0,0.0}};
region.center.latitude=22.292189;
region.center.longitude=70.792207;
region.span.longitudeDelta=0.01f;
region.span.latitudeDelta=0.01f;
[mapview setRegion:region animated:YES];
//[mapview setDelegate:self];
displaymap *ann=[[displaymap alloc]init];
ann.title=#"alpesh";
ann.subtitle=#"Mahatma Gandhi";
ann.coordinate = region.center;
[mapview addAnnotation:ann];
region.center.latitude=22.295031;
region.center.longitude=70.790837;
region.span.longitudeDelta=0.01f;
region.span.latitudeDelta=0.01f;
displaymap *bnn=[[displaymap alloc]init];
bnn.title=#"samir";
bnn.subtitle=#"Mahatma Gandhi";
bnn.coordinate = region.center;
[mapview addAnnotation:bnn];
//selectedAnnotationsl =[[NSArray alloc]initWithObjects:bnn.title,ann.title,nil];
}
-(MKAnnotationView *)mapview:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinView=nil;
if(annotation != mapview.userLocation)
{
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView=(MKPinAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if(pinView==nil)pinView=[[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinID]autorelease];
pinView.pinColor=MKPinAnnotationColorRed;
pinView.canShowCallout=YES;
pinView.animatesDrop=YES;
pinView.calloutOffset= CGPointMake(-5, 5);
}
else
{
[mapview.userLocation setTitle:#"I am here"];
}
return pinView;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"currentloc"];
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[addButton addTarget:self action:#selector(changepage:) forControlEvents:UIControlEventTouchUpInside];
[annView setPinColor:MKPinAnnotationColorRed];
annView.rightCalloutAccessoryView = addButton;
annView.animatesDrop = TRUE;
annView.canShowCallout = YES;
return annView;
}
-(IBAction)changepage:(UIView*)sender
{
second *s = [[second alloc]initWithNibName:#"second" bundle:nil];
s.title = #"samir";
[[self navigationController] pushViewController:s animated:YES];
}
MKMapViewDelegate's calloutAccessoryControlTapped: method is what you need. You have no need to add target and action for addButton. This delegate method gets called if the callout accessory control is tapped(both left and right accessory controls).
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
// Your code here
}