Having Placemark/Annotation in the center of your mapView, even if you scroll - android-mapview

I've spent many hours trying to figure how to do this:
Having a placemark/annotation in the centerCoordinate of your mapView, when you scroll the map, the placemark should always stays in the center.
I've seen another app doing this too!

Found my question in How to add annotation on center of map view in iPhone?
There's the answer :
If you want to use an actual annotation instead of just a regular view positioned above the center of the map view, you can:
use an annotation class with a settable coordinate property (pre-defined MKPointAnnotation class eg). This avoids having to remove and add the annotation when the center changes.
create the annotation in viewDidLoad
keep a reference to it in a property, say centerAnnotation
update its coordinate (and title, etc) in the map view's regionDidChangeAnimated delegate method (make sure map view's delegate property is set)
Example:
#interface SomeViewController : UIViewController <MKMapViewDelegate> {
MKPointAnnotation *centerAnnotation;
}
#property (nonatomic, retain) MKPointAnnotation *centerAnnotation;
#end
#implementation SomeViewController
#synthesize centerAnnotation;
- (void)viewDidLoad {
[super viewDidLoad];
MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = mapView.centerCoordinate;
pa.title = #"Map Center";
pa.subtitle = [NSString stringWithFormat:#"%f, %f", pa.coordinate.latitude, pa.coordinate.longitude];
[mapView addAnnotation:pa];
self.centerAnnotation = pa;
[pa release];
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
centerAnnotation.coordinate = mapView.centerCoordinate;
centerAnnotation.subtitle = [NSString stringWithFormat:#"%f, %f", centerAnnotation.coordinate.latitude, centerAnnotation.coordinate.longitude];
}
- (void)dealloc {
[centerAnnotation release];
[super dealloc];
}
#end
Now this will move the annotation but not smoothly. If you need the annotation to move more smoothly, you can add a UIPanGestureRecognizer and UIPinchGestureRecognizer to the map view and also update the annotation in the gesture handler:
// (Also add UIGestureRecognizerDelegate to the interface.)
// In viewDidLoad:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
panGesture.delegate = self;
[mapView addGestureRecognizer:panGesture];
[panGesture release];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
pinchGesture.delegate = self;
[mapView addGestureRecognizer:pinchGesture];
[pinchGesture release];
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
centerAnnotation.coordinate = mapView.centerCoordinate;
centerAnnotation.subtitle = [NSString stringWithFormat:#"%f, %f", centerAnnotation.coordinate.latitude, centerAnnotation.coordinate.longitude];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
//let the map view's and our gesture recognizers work at the same time...
return YES;
}

Related

iOS4 MKMapKit - map not zooming in when more than one pin

Sure this is something simple as I'm just starting with the maps. I already have a map showing one location, but when I've added a second anotation the map stays zoomed all the way out rather than going to my locations. The pins are there when I zoom in, so I know that bit's working.
Code snippets:
- (void)viewDidLoad
{
...
...
...
// Set coordinates for our position
CLLocationCoordinate2D location;
location.latitude = [self.lat doubleValue];
location.longitude = [self.lon doubleValue];
// Add the annotation to our map view
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc]
initWithTitle:self.placename
andSubtitle:self.subtitle
andCoordinate:location];
[self.mapView addAnnotation:newAnnotation];
[newAnnotation release];
// Set coordinates for our second position
CLLocationCoordinate2D amenitylocation;
amenitylocation.latitude = self.latitude;
amenitylocation.longitude = self.longitude;
// Add the annotation to our map view
MapViewAnnotation *amenityAnnotation = [[MapViewAnnotation alloc]
initWithTitle:self.callouttitle
andSubtitle:self.calloutsubtitle
andCoordinate:amenitylocation];
[self.mapView addAnnotation:amenityAnnotation];
[amenityAnnotation release];
[super viewDidLoad];
}
#pragma mark - MKMapView Delegates
// When a map annotation point is added, zoom to it (1500 range)
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
MKAnnotationView *annotationView = [views objectAtIndex:0];
id <MKAnnotation> mp = [annotationView annotation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 1500, 1500);
[mv setRegion:region animated:YES];
[mv selectAnnotation:mp animated:YES];
}
- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id<MKAnnotation>)annotation
{
if(mapView.userLocation==annotation)
{
return nil;
}
NSString *identifier = #"IDENTIFIER";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(annotationView==nil)
{
annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]autorelease];
annotationView.pinColor=MKPinAnnotationColorPurple;
annotationView.canShowCallout=YES;
}
return annotationView;
}
I'd appreciate any pointers.
Also, am I right in thinking I'll have to make custom callouts if I want more than one to appear on the map at the same time?
Sorry, found the answer - I didn't have the MKMapView delegate linked to File's Owner in IB, although I do have in my header file. Linked that up and it's working.

UITextView text does not get cleared

I have a small UIView that I hide/show in to show a message to the user. The message itself is in a UITextView that I add to the small UIView that gets shown.
The sliding-in and sliding-out works fine - but the prior messages are not cleared. I have spent enough time to fix the problem - but to no avail. Can someone lend me their eyes!!
Here is how the UITextField is created programatically:
#interface MessageVC : UIViewController {
UITextView *messageTV;
}
#property (nonatomic, retain) UITextView *messageTV;
- (id)init;
- (void)showMsg:(NSString *)title;
#end
and
- (id)init {
if (self = [super init]) {
self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, 380, 320, 100)] autorelease];
[self.view setBackgroundColor:[UIColor blackColor]];
}
return self;
}
- (void)showMsg:(NSString *)title {
[self setMessageTV : [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 315, 90 )]];
[[self messageTV] setBackgroundColor : [UIColor greenColor]];
[[self messageTV] setTextColor : [UIColor whiteColor]];
[[self messageTV] setText:#""]; <<<<<<<< - does not clear the text
[[self messageTV] setText : title];
[self.view addSubview : [self messageTV]];
[self.view setHidden:NO];
}
- (void) hideMessage {
[self.view setHidden:YES]
}
I'll go out on a limb and ask why you're using a UITextView. I honestly have never needed to use a UITextView. Try changing it to a UILabel and see if the problem is specific to the UITextView. If you absolutely need a UITextView, let me know in a comment, but I have a suspicion that a UILabel is what you're after.
It appears that you are adding it as a subview every time. So you're actually creating multiple UITextViews and adding them on top of each other. You would either need to removeFromSuperview or just set the text of the instance variable.
Take these two lines out of showMsg and put them in viewDidLoad:
[self setMessageTV : [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 315, 90 )]];
[self.view addSubview : [self messageTV]];

navigationItem not showing on popoverController

The navigationbar is failing to appear, works fine in a UITableView, but fails inside a popoverController
Initiate a popover popoverController in UIViewController
-(IBAction) btnShowMovies:(id) sender {
if (self.popoverController == nil) {
teamAController *movies =
[[teamAController alloc]
initWithNibName:#"teamAController"
bundle:[NSBundle mainBundle]];
UIPopoverController *popover =
[[UIPopoverController alloc] initWithContentViewController:movies];
popover.delegate = self;
[movies release];
self.popoverController = popover;
[popover release];
}
CGRect popoverRect = [self.view convertRect:[btn frame]
fromView:[btn superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 100);
[self.popoverController
presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
}
teamAController.h
#interface teamAController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
UITableView *tableView;
NSArray *theArray;
}
#property (nonatomic, retain) NSArray *theArray;
#property (nonatomic, retain) IBOutlet UITableView *tableView;
-(void) createArray;
teamAController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title= #"FooBarExtreme";
self.contentSizeForViewInPopover = CGSizeMake(250.0, 300.0);
[self createArray];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
Everything works, I have lovely table with images etc, correct sized and placed popover just no title bar..... ?
I found the solution/problem by following the tutorial at http://mobiforge.com/designing/story/using-popoverview-ipad-app-development.
Worth noting that I found this the most comprehensive one on creating uiPopoverController with uiNavigationBar elements from UIButtons.
The issue is that the popover itself belongs to the view that calls it. The content is derived from the xlib/view you load into it. But not the titlebar. You call that in the parent view view.
This code is in the main view and is called from the UIButton
// BookMarksViewController is the class that contains the code/xib for the popover's content
// Of overarching importance is creating it as a UITableViewController
if (self.popoverController == nil) {
BookMarksViewController *bookMarksViewController =
[[BookMarksViewController alloc]
initWithNibName:#"BookMarksViewController"
bundle:[NSBundle mainBundle]];
// Here's the rub: because in effect this view is controlling the popover
// we have to assign nav bar stuff here. Sigh.
bookMarksViewController.navigationItem.title = #"Territories";
UINavigationController *navController =
[[UINavigationController alloc]
initWithRootViewController:bookMarksViewController];
bookMarksViewController.contentSizeForViewInPopover = CGSizeMake(320, 400);
UIPopoverController *popover =
[[UIPopoverController alloc]
initWithContentViewController:navController];
popover.delegate = self;
[bookMarksViewController release];
[navController release];
self.popoverController = popover;
[popover release];
}
CGRect sourceRect = [self.view convertRect:[btn frame] fromView:[btn superview]];
[self.popoverController presentPopoverFromRect:sourceRect
inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

How can you hide a segmented controller?

I have tried hiding a segmented controller just like a button or label can be hidden in XCode. It's intended to be hidden/shown when touching a parent segmented controller above. This Code would work with Buttons or Labels:
mySegmContr.hidden = YES;
But it just won't work for segmented controllers. Can you help me out?
I figured out that you can use a simple UIView in which you put the things you want to hide. The UIView can then be hidden with
myView.hidden = YES;
still I found no way to hide a segmented control directly.
If you create a property for the segment controller you can do more stuff with it like changing it's location, resizing it and want you want hiding it.
In your .h file do this
UISegmentedControl *mySegment;
#property (nonatomic, retain) UISegmentedControl *mySegment;
-(void) createMySegment;
In your .m file do this
#synthesize mySegment;
- (void) createMySegment {
if ([self mySegment] == nil) {
NSArray *buttons = [NSArray arrayWithObjects:#"One", #"Two", #"Three", nil];
UISegmentedControl *segName = [[UISegmentedControl alloc] buttons];
[self setMySegment:segName];
[segName release];
segName.frame = CGRectMake(110, 62, 120, 25);
segName.segmentedControlStyle = UISegmentedControlStyleBar;
segName.momentary = NO;
segName.selectedSegmentIndex = 0;
[segName addTarget:self
action:#selector(pickMethod:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segName];
}
}
NOTE: With "setMySegment" above make sure you use a capital first letter which is M in "mySegment".
Then when you want to hide it use this. Don't for get to dealloc mySegment.
[[self mySegment] setHidden:YES];

uitabbarcontroller / uitabbar in navigation based project

I have created navigation based project. and in second screen i want to add uitabbarcontroller. so can any one suggest how i do this.
i already did lot of search but no success yet. so please can you provide a simple sample of this. i already tried below discussion but i think its not a good approach.
Navigation Based Application with TabBar
Thanks
Actually this is the correct approach. The one thing that is not correct is where the controllers are allocated. This is happened in the previous controller, the one that is making the push, but should be allocated in the object that is responsible, the TabBarController.
When you implement your action to show the UITabBarController make the following code:
- (void) theAction {
SomeTabBarControllerSubClass *controller = [[SomeTabBarControllerSubClass alloc] init];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
Then when you implement the SomeTabBarControllerSubClass class:
(.h)
#interface SomeTabBarControllerSubClass : UITabBarController {
UIViewController *first;
UIViewController *second;
}
#end
(.m)
#implementation SomeTabBarControllerSubClass
- (void) viewDidLoad {
first = [[UIViewController alloc] init]; //Or initWithNib:
second = [[UIViewController alloc] init];
first.view.backgroundColor = [UIColor greenColor] //Just example
second.view.backgroundColor = [UIColor redColor] //Just example
first.tabBarItem.image = [UIImage imageNamed:#"someImage.png"];
self.viewControllers = [NSArray arrayWithObjects:first,second,nil];
}
- (void) dealloc {
[first dealloc];
[second dealloc];
[super dealloc];
}
#end

Resources