Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-11-2011, 11:33 AM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 3
Generalmonkey is on a distinguished road
Default “iphone app” mapview kit with bubble, selected button and choose action.

hi,guys.
I have the project with flowing code
Code:
import "MapView.h"
import "DisplayMap.h"

@implementation MapView

@synthesize myMapView;

// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"MapView"; } return self; }

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. (void)viewDidLoad { [super viewDidLoad];

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];
backButton.title = @"BACK";
self.navigationItem.backBarButtonItem = backButton;
[backButton release];

CLLocation *userLoc = myMapView.userLocation.location;
CLLocationCoordinate2D userCoodinate = userLoc.coordinate;

NSLog(@"user latitude = %f",userCoodinate.latitude);
NSLog(@"user longitude = %f",userCoodinate.longitude);

myMapView.delegate=self;

NSMutableArray* annotations=[[NSMutableArray alloc] init];

CLLocationCoordinate2D theCoordinate1;
theCoordinate1.latitude = 40.909361;
theCoordinate1.longitude = -74.520264;

CLLocationCoordinate2D theCoordinate2;
theCoordinate2.latitude = 38.895041;
theCoordinate2.longitude = -77.036476;

CLLocationCoordinate2D theCoordinate3;
theCoordinate3.latitude = 34.045832;
theCoordinate3.longitude = -118.243103;

CLLocationCoordinate2D theCoordinate4;
theCoordinate4.latitude = 36.114581;
theCoordinate4.longitude = -115.434723;

CLLocationCoordinate2D theCoordinate5;
theCoordinate5.latitude = 25.789072;
theCoordinate5.longitude = -80.226288;


DisplayMap* myAnnotation1=[[DisplayMap alloc] init];

myAnnotation1.coordinate=theCoordinate1;
myAnnotation1.title=@"New York";
myAnnotation1.subtitle=@"USA";

DisplayMap* myAnnotation2=[[DisplayMap alloc] init];

myAnnotation2.coordinate=theCoordinate2;
myAnnotation2.title=@"WashingtonDc";
myAnnotation2.subtitle=@"USA";

DisplayMap* myAnnotation3=[[DisplayMap alloc] init];

myAnnotation3.coordinate=theCoordinate3;
myAnnotation3.title=@"Los Angels";
myAnnotation3.subtitle=@"USA";

DisplayMap* myAnnotation4=[[DisplayMap alloc] init];

myAnnotation4.coordinate=theCoordinate4;
myAnnotation4.title=@"Las Vegas";
myAnnotation4.subtitle=@"USA";

DisplayMap* myAnnotation5=[[DisplayMap alloc] init];

myAnnotation5.coordinate=theCoordinate5;
myAnnotation5.title=@"Miami";
myAnnotation5.subtitle=@"USA";


[myMapView addAnnotation:myAnnotation1];
[myMapView addAnnotation:myAnnotation2];
[myMapView addAnnotation:myAnnotation3];
[myMapView addAnnotation:myAnnotation4];
[myMapView addAnnotation:myAnnotation5];

[annotations addObject:myAnnotation1];
[annotations addObject:myAnnotation2];
[annotations addObject:myAnnotation3];
[annotations addObject:myAnnotation4];
[annotations addObject:myAnnotation5];


NSLog(@"%d",[annotations count]);
//[self gotoLocation];//to catch perticular area on screen
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;

// Walk the list of overlays and annotations and create a MKMapRect that
// bounds all of them and store it into flyTo.
MKMapRect flyTo = MKMapRectNull;
for (id <MKAnnotation> annotation in annotations) {
    NSLog(@"fly to on");
    MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
    MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
    if (MKMapRectIsNull(flyTo)) {
        flyTo = pointRect;
    } else {
        flyTo = MKMapRectUnion(flyTo, pointRect);
        //NSLog(@"else-%@",annotationPoint.x);
    }
}

// Position the map so that all overlays and annotations are visible on screen.
myMapView.visibleMapRect = flyTo;

[self.navigationController setNavigationBarHidden:NO animated:YES];
}

(void)viewWillDisappear:(BOOL)animated {

[self.navigationController setNavigationBarHidden:YES animated:YES]; }

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation {

NSLog(@"welcome into the map view annotation");

// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

// try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
                                 initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
pinView.pinColor=MKPinAnnotationColorRed;

UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
leftButton.frame = CGRectMake(0, 0, 30, 30);
[leftButton setImage:[UIImage imageNamed:@"one.png"] forState:UIControlStateNormal];
[leftButton addTarget:self
               action:@selector(leftClick:)
     forControlEvents:UIControlEventTouchUpInside];
pinView.leftCalloutAccessoryView = leftButton;

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0, 0, 30, 30);
[rightButton setImage:[UIImage imageNamed:@"two.png"] forState:UIControlStateNormal];
[rightButton addTarget:self
                action:@selector(rightClick:)
      forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;

return pinView;
}

(void)leftClick:(UIButton *)sender {

ListWebView *lwv = [[ListWebView alloc] initWithNibName:@"ListWebView" bundle:[NSBundle mainBundle]]; lwv.title = @"Los Angels"; lwv.url = @"http://www.la.com/"; [self.navigationController pushViewController:lwv animated:YES]; [lwv release];

}

(void)rightClick:(UIButton *)sender {

ListWebView *lwv = [[ListWebView alloc] initWithNibName:@"ListWebView" bundle:[NSBundle mainBundle]]; lwv.title = @"LA Times"; lwv.url = @"http://www.latimes.com/"; [self.navigationController pushViewController:lwv animated:YES]; [lwv release];

}

/* // Override to allow orientations other than the default portrait orientation. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations. return (interfaceOrientation == UIInterfaceOrientationPortrait); } */

(void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning];

// Release any cached data, images, etc. that aren't in use. }

(void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; }

(void)dealloc { [myMapView release]; [super dealloc]; }

@end
I have two button in this map bubble. On the map, Los Anges pin,
When I push button #1, go to la.com
When I push button#2, go to latimes.com
This is clear.
but, go to New York pin. When I click button#1, it goes to same as Los Angles/www.la.com
I want to go to newyork.com. Also, button#2, it goes latimes.com.
I wanna go to newyorktimes.com.....

Please give me advice how to fix this problems.
I uploaded my project file on
http://bit.ly/mlru6l

Thank you for your help.
Generalmonkey is offline   Reply With Quote
Old 05-11-2011, 10:43 PM   #2 (permalink)
Registered Member
 
Join Date: Jan 2011
Location: South Florida, US
Posts: 357
lgehrig1 is on a distinguished road
Default

The part where you are creating leftButton and rightButton? Do either:

A. (LAZY) Change the selector based on the annotation being passed in. Call different functions to open different websites.

B. (FAR BETTER) Assign each button a tag based on the annotation. In the leftClick and rightClick lookup where you are supposed to go based on the tag. Heck, then you could merge leftClick and rightClick into one function. If you have more than 4,294,967,296 annotations, rethink your app.
lgehrig1 is offline   Reply With Quote
Old 05-13-2011, 03:39 AM   #3 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 3
Generalmonkey is on a distinguished road
Default

Thank you for your reply. I understand what you sayin'.
but I have no idea how to write right code.
If you have some sample code, please give advice.
Generalmonkey is offline   Reply With Quote
Old 05-13-2011, 10:17 AM   #4 (permalink)
Registered Member
 
Join Date: Jan 2011
Location: South Florida, US
Posts: 357
lgehrig1 is on a distinguished road
Default

Quote:
Originally Posted by Generalmonkey View Post
Thank you for your reply. I understand what you sayin'.
but I have no idea how to write right code.
If you have some sample code, please give advice.
Yeah ... advice is free, real code requires real cash

So, when the framework calls for its annotation view, it will pass you the map view requiring the annotation AND the annotation to create the view for. You annotations all appear to have title values set, so you could just do a

Code:
// NOTE: maintaining your city spelling
if( [ annotation.title isEqualToString : @"Los Angels" ])
{
  leftButton.tag = 1 ;
}
else if( [ annotation.title isEqualToString : @"Las Vegas" ])
{
  leftButton.tag = 2 ;
}
// ... and repeat
Then, down in your button click, just test the value of the tag and fire off the appropriate logic.

Note: your code could be substantially improved, though. For example, the code snippet I've given above is very bad for more than a handful of annotations (search time is O(N), when it could be O(1)).

Since you're using your own annotation object, add one property for URL and one property for ID. If you won't be doing many resorts of your annotation array, you can assign the ID as the array position - and that means your button handling routine has instant access to the annotation hit (see NSArray's objectAtIndex), and thus instant access to the URL.

You're going to want to start thinking more in object-oriented mode. In your code right now, there's very tight coupling between the interface logic (what happens when a button is hit) and your data (your annotations). For example, if you changed "Los Angels" to "Cambridge", you also have to go into your button logic to change the URL.
lgehrig1 is offline   Reply With Quote
Old 05-14-2011, 10:40 PM   #5 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 3
Generalmonkey is on a distinguished road
Default

Thank you!! I'll try that as much as I can.
Generalmonkey is offline   Reply With Quote
Reply

Bookmarks

Tags
button, iphone-sdk-3.2, mkannotationview, mkmapview, tap

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 358
7 members and 351 guests
doffing81, dre, iOS.Lover, Kirkout, MikaelBartlett, oztemel, PlutoPrime
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:02 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0