unable to display custom Annotations on the map in iPhone - iphone

I am trying to add annotations on my mapview but I am not able to add them. Can someone suggest me where I am doing wrong? Code: Code to add the map:
mapview = [[MKMapView alloc] initWithFrame:CGRectMake(10, 175, 300, 268)];
mapview.delegate = self;
mapview.userInteractionEnabled = TRUE;
[mapview setZoomEnabled:TRUE];
[mapview setScrollEnabled:TRUE];
mapview.showsUserLocation = TRUE;
[self.view addSubview:mapview];
//adding annotations in another method:
for (id annotation in mapview.annotations) {
[mapview removeAnnotation:annotation];
}
for(int i =0;i<arrEventList.count;i++){
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = [[NSString stringWithFormat:#"%#",[(NSDictionary*)[arrEventList objectAtIndex:i] objectForKey:#"lat"]] floatValue];
theCoordinate.longitude = [[NSString stringWithFormat:#"%#",[(NSDictionary*)[arrEventList objectAtIndex:i] objectForKey:#"lng"]] floatValue];
MyLocation *annotation = [[[MyLocation alloc] initWithName:[(NSDictionary*)[arrEventList objectAtIndex:i] objectForKey:#"event"] address:[(NSDictionary*)[arrEventList objectAtIndex:i] objectForKey:#"place"] coordinate:theCoordinate] autorelease];
[mapview addAnnotation:annotation];
//[annotations addObject:myAnno.annotation];
}
// delegate method:
- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation {
static NSString *identifier = #"MyLocation";
if ([annotation isKindOfClass:[MyLocation class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image=[UIImage imageNamed:#"annotation.png"];//here we use a nice image instead of the default pins
return annotationView;
}
return nil;
}
Above delegate method is only called for self location but it is not called for other annotations that I want to add.
Can someone point me to my mistakes
#interface MyLocation : NSObject <MKAnnotation> {
NSString *_name;
NSString *_address;
CLLocationCoordinate2D _coordinate;
}
#property (copy) NSString *name;
#property (copy) NSString *address;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate;
#end
//Mylocation class:
#implementation MyLocation
#synthesize name = _name;
#synthesize address = _address;
#synthesize coordinate = _coordinate;
- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate {
if ((self = [super init])) {
_name = [name copy];
_address = [address copy];
_coordinate = coordinate;
}
return self;
}
- (NSString *)title {
if ([_name isKindOfClass:[NSNull class]])
return #"Unknown charge";
else
return _name;
}
- (NSString *)subtitle {
return _address;
}

I found the problem. My code is correct. The values of latittude and longitudes were some how interchanged. My mistake but I have corrected it now

Related

how to display multiple locations in a single map view in iphone application

i have NSMutableArray that contains multiple places- name,latitude and longitude. those location values are within the range of 5000 meter from current location. i need to display the annotation pins for those all location in a single map. is it possible?
thanks in advance..
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MyAnnotation : NSObject <MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString* title;
NSString* subtitle;
}
#property (nonatomic, assign)CLLocationCoordinate2D coordinate;
#property (nonatomic, copy)NSString *title;
#property (nonatomic, copy)NSString *subtitle;
#end
#import "MyAnnotation.h"
#implementation MyAnnotation
#synthesize title;
#synthesize subtitle;
#synthesize coordinate;
- (void)dealloc
{
[super dealloc];
self.title = nil;
self.subtitle = nil;
}
#end
//in your controller in .h
MyAnnotation* myAnnotation;
NSMutableArray *annotations;
//im .m file
- (void)viewDidLoad
{
[super viewDidLoad];
mapp.showsUserLocation = YES;
mapp.delegate = self;
annotations = [[NSMutableArray alloc] init];
for (int i=0; i<[arrayLatitude count]; i++)
{
CLLocationCoordinate2D theCoordinate1;
theCoordinate1.latitude = [[arrayLatitude objectAtIndex:i] floatValue];
theCoordinate1.longitude = [[arrayLongitude objectAtIndex:i] floatValue];
myAnnotation = [[MyAnnotation alloc] init];
myAnnotation.coordinate = theCoordinate1;
myAnnotation.title = [arrayName objectAtIndex:i];
[mapp addAnnotation:myAnnotation];
[annotations addObject:myAnnotation]
}
NSLog(#"%d",[annotations count]);
MKMapRect flyTo = MKMapRectNull;
for (id <MKAnnotation> annotation in annotations)
{
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
if (MKMapRectIsNull(flyTo))
{
flyTo = pointRect;
}
else
{
flyTo = MKMapRectUnion(flyTo, pointRect);
}
}
mapp.visibleMapRect = flyTo;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
pinView.animatesDrop = NO;
pinView.canShowCallout = YES;
pinView.pinColor = MKPinAnnotationColorRed;
UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"%i.jpg",rand()%3908+1]]];
pinView.leftCalloutAccessoryView = icon;
[icon release];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:nil forState:UIControlStateNormal];
[rightButton addTarget:self
action:#selector(myMethod:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
return pinView;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
NSLog(#"Title : %#",view.annotation.title);
NSLog(#"Latitude : %f", view.annotation.coordinate.latitude);
NSLog(#"Longitude : %f", view.annotation.coordinate.longitude);
}
you can ad infinite number of MKAnnotationPoint s

iPhone map view custom annotation

I have some map view controller and I have a custom annotation.
Custom annotation code:
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
#interface DisplayMapAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
NSString *detailID;
}
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, retain) NSString *title;
#property (nonatomic, retain) NSString *subtitle;
#property (nonatomic, retain) NSString *detailID;
#end
#import "DisplayMapAnnotation.h"
#implementation DisplayMapAnnotation
#synthesize coordinate, title, subtitle, detailID;
- (NSString *) title
{
return title;
}
- (NSString *) subtitle
{
return subtitle;
}
- (NSString *) detailID
{
return detailID;
}
- (id)initWithCoordinate:(CLLocationCoordinate2D) c
{
coordinate=c;
return self;
}
- (void) dealloc
{
[title release];
[subtitle release];
[detailID release];
[super dealloc];
}
A here is my map view code where I create the annotation:
In viewDidLoad method I make something like this:
for (FeedItems *aItem in geoDataList) {
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = [aItem.geoLat doubleValue];
region.center.longitude = [aItem.geoLng doubleValue];
region.span.longitudeDelta = kLatitudeDelta;
region.span.latitudeDelta = kLongitudeDelta;
[mapView setRegion:region animated:YES];
ann = [[DisplayMapAnnotation alloc] init];
ann.title = aItem.job;
ann.subtitle = aItem.jobCompany;
ann.detailID = aItem.jobID;
ann.coordinate = region.center;
[mapView addAnnotation:ann];
[aItem release];
}
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
MKPinAnnotationView *pinView = nil;
if (annotation != self.mapView.userLocation) {
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if (pinView == nil)
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
if (mapIdentifier == 0) {
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//NSInteger annotationValue = [annView indexOfObject:annotation];
//rightButton.tag = annotationValue;
[rightButton addTarget:self action:#selector(detailButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
}
} else {
[self.mapView.userLocation setTitle:#"your location"];
}
return pinView;
}
And here is my problem (this method):
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"Detail id: %#", view.annotation.detailID);
}
When I will log detail id Xcode say: "Property 'detailID' not found on object of type 'id'" What is wrong ?
It does not know that the annotation is your custom annotation:
NSLog(#"Detail id: %#", view.annotation.detailID);
You could cast it like this to silence the warning
NSLog(#"Detail id: %#", [(DisplayMapAnnotation*)view.annotation detailID]);
after having checked that it is a DisplayMapAnnotation*
if ([view.annotation isKindOfClass:[DisplayMapAnnotation class]])

How to show multiple pin annotation in map?

I m showing a pin annotation statically, but i want to do it dynamically to show a lot of pins on map with title and subtitle.
Thx in advance.
I have an array of dictionaries with have two objects lat, long and i am doing this -
if ([resultArray count])
{
for (int i =0; i < [resultArray count]; i++)
{
NSDictionary *dict = [resultArray objectAtIndex:i];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = [[dict objectForKey:#"lat"] floatValue];
region.center.longitude = [[dict objectForKey:#"long"] floatValue];
region.span.longitudeDelta = 70.0f;
region.span.latitudeDelta = 70.0f;
MyAnnotations *ann = [[MyAnnotations alloc] init];
ann.title = #"title";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
}
}
//
// MyAnnotation.h
//
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MyAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#end
//
// MyAnnotation.m
//
#import "MyAnnotation.h"
#implementation MyAnnotation
#synthesize coordinate,title,subtitle;
-(void)dealloc
{
[title release];
[subtitle release];
[super dealloc];
}
#end
use this delegate for callout -
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id )annotation
{
MKPinAnnotationView *pinView = nil;
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorPurple;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton addTarget:self action:#selector(infoButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = infoButton;
[defaultPinID release];
return pinView;
}

Customize map annotations from plist

I found a short guide on how to load "annotation data" from a plist and add those annotations (pins) to a MapView.
My question is if anyone can tell me how I would customize the pins the most easily? As an example, I would like to define a color and an image in the plist and then have it set when adding the pins.
The pins are added like this:
MyAnnotation.m:
#implementation MyAnnotation
#synthesize title, subtitle, coordinate;
-(void) dealloc {
[super dealloc];
self.title = nil;
self.subtitle = nil;
}
MyAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MyAnnotation : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString* title;
#property (nonatomic, copy) NSString* subtitle;
Running through the plist in ViewDidLoad in MapViewController.m (array containg dictionaries for each pin) and adding annotations.
NSString *path = [[NSBundle mainBundle] pathForResource:#"Annotations" ofType:#"plist"];
NSMutableArray* anns = [[NSMutableArray alloc] initWithContentsOfFile:path];
for(int i = 0; i < [anns count]; i++) {
float realLatitude = [[[anns objectAtIndex:i] objectForKey:#"latitude"] floatValue];
float realLongitude = [[[anns objectAtIndex:i] objectForKey:#"longitude"] floatValue];
MyAnnotation* myAnnotation = [[MyAnnotation alloc] init];
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = realLatitude;
theCoordinate.longitude = realLongitude;
myAnnotation.coordinate = theCoordinate;
myAnnotation.title = [[anns objectAtIndex:i] objectForKey:#"title"];
myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:#"subtitle"];
[mapView addAnnotation:myAnnotation];
[annotations addObject:myAnnotation];
[myAnnotation release];
}
EDIT:
I have added the viewForAnnotation method which sets the color but how can I set the color of each pin individually?
-(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 = MKPinAnnotationColorPurple;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
}
else {
[mapView.userLocation setTitle:#"I am here"];
}
return pinView;
}
Solved! It can be done like this in the viewForAnnotation method:
for(MyAnnotation* a in mapView.annotations) {
if(annotation == a && annotation != mapView.userLocation) {
pinView.image = [UIImage imageNamed:a.annImage];
}
}

I am not able to give an MKAnnotation an image!

Hey guys! I am having some trouble with giving an MKAnnotationView an image instead of a pin view. In other words, I am having trouble displaying a target image (target.png) instead of the normal pin view. Here is my code---
// .h file
#import //Here it says to import mapkit & UIKit. The code blockquote doesn't let me
#import //show you that
#interface AddressAnnotation : NSObject {
CLLocationCoordinate2D coordinate;
NSString *mTitle;
NSString *mSubTitle;
}
#end
#interface ChosenLocationMap : UIViewController {
IBOutlet MKMapView *mapView;
AddressAnnotation *addAnnotation;
}
-(CLLocationCoordinate2D) addressLocation;
// .m file
#implementation AddressAnnotation
#synthesize coordinate;
- (NSString *)subtitle{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *stitle = [prefs objectForKey:#"addressKey"];
return #"%#",stitle;
}
- (NSString *)title{
return #"TARGET";
}
-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
coordinate=c;
NSLog(#"%f,%f",c.latitude,c.longitude);
return self;
}
#end
#implementation ChosenLocationMap
#synthesize destinationLabel, startbutton, accelloop, aimview, bombblowupview, bombleftview1, bombleftview2, bombleftview3, firebutton;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (void)viewDidLoad {
mapView.mapType = MKMapTypeSatellite;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
CLLocationCoordinate2D location = [self addressLocation];
region.span=span;
region.center=location;
if(addAnnotation != nil) {
[mapView removeAnnotation:addAnnotation];
[addAnnotation release];
addAnnotation = nil;
}
addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];
[mapView addAnnotation:addAnnotation];
[super viewDidLoad];
}
-(CLLocationCoordinate2D) addressLocation {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *destinationstring = [prefs objectForKey:#"addressKey"];
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv",
[destinationstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:#","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:#"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
//Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}
- (MKAnnotationView *)map:(MKMapView *)map viewForAnnotation:(id )annotation{
MKAnnotationView *annView;
annView = (MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:annotation.title];
if(annView == nil)
annView = [[[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:annotation.title] autorelease];
else
annView.annotation = annotation;
[annView setImage:[UIImage imageNamed:#"target.png"]];
annView.canShowCallout = TRUE;
return annView;
}
Please note that I only included the code that actually involves the mapview. Thanks in advance!
EDIT: I Changed the code in my Xcode document for the changes in answer 1. I am too lazy to transfer everything to the code block above, and still, the picture still doesn't work.
SHOOP DA EDIT: Thank you for replying! My solution was that I forgot to say mapView.delegate = self. Bye!
Wow so much wrong with this code :-)
First you are missing a #property declaration in your AddressAnnotation
#property (nonatomic,assign) CLLocationCoordinate2D coordinate;
In the subtitle method you do this:
return #"%#",stitle;
But this is Objective-C and not Python, so you might want to change this to:
return stitle;
Then your initWithCoordinate is completely wrong. You do not initialize super. This is better:
-(id) initWithCoordinate: (CLLocationCoordinate2D) c
{
if ((self = [super init]) != nil) {
coordinate=c;
NSLog(#"%f,%f",c.latitude,c.longitude);
}
return self;
}
Try fixing that stuff first to see if it helps :-)
I'm new to Objective C but I think the prototype for your delegate function should be :
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id )annotation{
MKAnnotationView *annView;
The delegate name is mapView:viewForAnnotation:, not map:viewForAnnotation:
Also you AddressAnnotation doesn't implement the MKAnnotation protocol