MKPinAnnotationView title not displayed - iphone

I want to show the title of the pin(MKAnnotation)every time when I added the annotation.I added the following code for that.But it is not working.Please help me to find out.
I just added the pin like this.
ann = [[DisplayMap alloc] init];
ann.title = #"aaa";
ann.subtitle = #"bbbn";
[mapView addAnnotation:ann];
after that i added the following code in this method
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
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.selected = YES;
[mapView selectAnnotation:annotation animated:YES];
//[pinView setImage:[UIImage imageNamed:#"user.png"]];
}
}
what mistake I have done?

Related

Want to show custom images on the map with user update location

I want to show bike/car image on google maps with rotation that user follows.
How it can be possible. I have added didUpdateToLocation method to update the location of user and added viewForAnnotation to display the current location of the bike. But not able to show the bike on the map which user follows.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
if(currentLocation != nil)
{
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
MyAnnotation *annotation = [[MyAnnotation alloc] initWithCoordinates:location title:#"India" subTitle:#"Sarvopari Mall"];
[self.myMapView addAnnotation:annotation];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>)annotation{
MKAnnotationView *result = nil;
if ([annotation isKindOfClass:[MyAnnotation class]] == NO)
{
return result;
}
if ([mapView isEqual:self.myMapView] == NO)
{
return result;
}
MyAnnotation *senderAnnotation = (MyAnnotation *)annotation;
NSString *pinReusableIdentifier =
[MyAnnotation
reusableIdentifierforPinColor:senderAnnotation.pinColor];
MKAnnotationView *annotationView = (MKAnnotationView *)
[mapView
dequeueReusableAnnotationViewWithIdentifier:
pinReusableIdentifier];
if (annotationView == nil){
annotationView =
[[MKAnnotationView alloc] initWithAnnotation:senderAnnotation
reuseIdentifier:pinReusableIdentifier];
annotationView.canShowCallout = YES;
}
UIImage *pinImage = [UIImage imageNamed:#"Bike.png"];
if (pinImage != nil){
annotationView.image = pinImage;
annotationView.canShowCallout = YES;
}
result = annotationView;
return result;
}
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = #"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
annotationView.image = [UIImage imageNamed:#"Bike.png"];
annotationView.annotation = annotation;
return annotationView;
}

MKAnnotationView custom image is not shown

I am trying to add a custom image instead of the regular pin on the map. But it remains a red pin... What am I missing?
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
annView.animatesDrop = TRUE;
annView.image = [UIImage imageNamed:#"CustomPin.png"];
return annView;
}
MKMapView: Instead of Annotation Pin, a custom view
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID];
//pinView.pinColor = MKPinAnnotationColorGreen;
pinView.canShowCallout = YES;
//pinView.animatesDrop = YES;
pinView.image = [UIImage imageNamed:#"pinks.jpg"]; //as suggested by Squatch
}
else {
[mapView.userLocation setTitle:#"I am here"];
}
return pinView;
}
Hi just remove one line from your code... annView.animatesDrop = TRUE;
Remain Code-
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
annView.image = [UIImage imageNamed:#"CustomPin.png"];
return annView;
}
I found it helpful to look at Apples Documentation and download the sample code.
http://developer.apple.com/library/ios/#samplecode/MapCallouts/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009746
They are implementing a custom annotation for their map.

impossible to show 2 colors in mapkit

ok desperation. This is killing me.. I totally do not understand MapKit at all.. Despite reading so many tutorial.. =(
For more information, I have 2 annotation classes - MyAnnotation and MyAnnotation2, it doesnt seems to work too.. Somebody plz help.. I am afraid I have no hair left soon =(
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
_annotations = [[NSMutableArray alloc] init];
_annotation2 = [[NSMutableArray alloc] init];
CLLocation *userLoc = _mapView.userLocation.location;
CLLocationCoordinate2D userCoordinate = userLoc.coordinate;
NSLog(#"user latitude = %f",userCoordinate.latitude);
NSLog(#"user longitude = %f",userCoordinate.longitude);
_listOfPolyClinics = [[NSMutableArray alloc] init];
_listOfPatients = [[NSMutableArray alloc] init ];
for (PolyClinics *polyclinics in [[PatientDatabase database]
polyClinics]){
[_listOfPolyClinics addObject:polyclinics];
}
NSLog(#"%i", [_listOfPolyClinics count]);
for (PatientDetails *patientDetails in [[PatientDatabase database]
patientCategoryList:_category]){
[_listOfPatients addObject:patientDetails];
}
NSLog(#"%i", [_listOfPatients count]);
for (PolyClinics *polyclinics in _listOfPolyClinics){
MyAnnotation * myAnnotation =[[MyAnnotation alloc] init];
CLLocationCoordinate2D theCoordinate;
theCoordinate.longitude = polyclinics.longtitude;
theCoordinate.latitude = polyclinics.latitude;
myAnnotation.pinColor = MKPinAnnotationColorPurple;
myAnnotation.coordinate = theCoordinate;
myAnnotation.title = polyclinics.name;
myAnnotation.subtitle = [NSString stringWithFormat:#"%i",polyclinics.telephone];
//myAnnotation.annotationsPatients =
[_mapView addAnnotation:myAnnotation];
[_annotation2 addObject:myAnnotation];
}
for(PatientDetails *patientDetails in _listOfPatients){
MyAnnotation2 * myAnnotation =[[MyAnnotation2 alloc] init];
CLLocationCoordinate2D theCoordinate;
theCoordinate.longitude = patientDetails.longitude;
theCoordinate.latitude = patientDetails.latitude;
myAnnotation.pinColor = MKPinAnnotationColorGreen;
myAnnotation.coordinate = theCoordinate;
myAnnotation.title = patientDetails.nric;
myAnnotation.subtitle = [NSString stringWithFormat:#"%i",patientDetails.category];
[_mapView addAnnotation:myAnnotation];
[_annotation2 addObject:myAnnotation];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(#"%i", [_annotation2 count]);
MKPinAnnotationView *pinView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:[annotation title]];
for(id <MKAnnotation> a in _annotation2){
if([a isKindOfClass:[MyAnnotation class]]){
pinView.pinColor = MKPinAnnotationColorPurple;
}
else{
pinView.pinColor = MKPinAnnotationColorGreen;
}
pinView.animatesDrop=NO;
pinView.canShowCallout=YES;
return [pinView autorelease];
}
return pinView;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
is called every time the MKMapView needs to show a view for an annotation, it passes you that annotation for a reason. So instead of iterating through them and returning only the color for the first one in your array (you need to understand that returning from a function/method doesn't let the loop iterate more), use the parameter instead.
Also a good practice is to use
- (MKAnnotationView *)dequeueReusableAnnotationViewWithIdentifier:(NSString *)identifier;
so the AnnotationViews don't get created again from scratch, especially memory-wise!
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
BOOL isGreen = YES;
if([annotation isKindOfClass:[MyAnnotation class]])
isGreen = NO;
MKPinAnnotationView *pinView = nil;
if (isGreen) {
static NSString *greenPin = #"greenPin";
pinView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:greenPin];
if (!pinView) {
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:greenPin] autorelease];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.animatesDrop = NO;
pinView.canShowCallout = YES;
}
else
pinView.annotation = annotation;
}
else {
static NSString *purplePin = #"purplePin";
pinView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:purplePin];
if (!pinView) {
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:purplePin] autorelease];
pinView.pinColor = MKPinAnnotationColorPurple;
pinView.animatesDrop = NO;
pinView.canShowCallout = YES;
}
else
pinView.annotation = annotation;
}
return pinView;
}

IPhone SDK MapKIt Multiple Points and Annotation

I am new to IPhone SDK Development i am trying to make an application with MapKit i have done the first bit i want to add multiple pins and annotation to the application but i am lost here.
Following is the code how can i add more pins to this code
-(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 = 26.438047;
region.center.longitude = 50.116422;
region.span.latitudeDelta=0.01f;
region.span.longitudeDelta=0.01f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = #"Corporate Office";
ann.subtitle =#"King Khalid Street";
ann.coordinate=region.center;
[mapView addAnnotation:ann];
}
-(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;
}
you are on the right track already, just reuse your code to make multiple points.
for example :
DisplayMap *ann = [[DisplayMap alloc] init];
for( int i =1;i<=5;i++ ){
region.center.latitude = 26.438047+i;
region.center.longitude = 50.116422+i;
ann.title = [NSString stringWithFormat:#"title %d",i)];
ann.subtitle =[NSString stringWithFormat:#"subtitle %d",i)];
ann.image = [NSString stringWithFormat#"image_%d.png",i];
ann.coordinate=region.center;
[mapView addAnnotation:ann];
}
[ann release];
in result, will display 5 points in different coordinate. (with same name and subtitle).
Edited: show different pin image. you have to add new field as NSString *image to DisplayMap. and add your path image inside for loop.
- (MKAnnotationView *) mapView:(MKMapView *)amapView viewForAnnotation:(id <MKAnnotation>) annotation
{
NSLog(#"pinnview before release %d",[pinView retainCount]);
if (pinView !=nil) {
pinView =nil;
[pinView release];
}
NSLog(#"pinnview after release %d",[pinView retainCount]);
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if(annotation != map.userLocation)
{
static NSString *defaultPinID = #"your-pin";
pinView = (MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( counting < [map.annotations count])
{
counting++;
pinView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
for(DisplayMap* a in map.annotations)
{
if (annotation == a){
pinView.image =
[UIImage imageWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:a.image ofType:nil]];
}
}
pinView.centerOffset= CGPointMake(0,-10);
pinView.canShowCallout = YES;
}
}
return pinView;
}

how to add multiple annotation for iphone

Right now I can show current and destination now I want a dummy pin in midway say any location but I want to know how to add 3 pins in map
if(annotation != mapView.userLocation)
{
NSLog(#"marked");
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
}