It looks like you're new here. If you want to get involved, click one of these buttons!
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
Replies
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.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomebut I have no idea how to write right code.
If you have some sample code, please give advice.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSo, 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
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.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome