Centralized the code - iphone

- (void) setTheMap
{
myMap.mapType = MKMapTypeStandard;
[myMap setDelegate:self];
CLLocationCoordinate2D coord = {latitude:myValue , longitude:myValue };
MKCoordinateSpan span = {latitudeDelta:0.05f , longitudeDelta: 0.05f};
MKCoordinateRegion region = {coord , span};
[myMap setRegion:region];
[self.view addSubview:myMap];
PantryAnnotation *dis = [[PantryAnnotation alloc] init];
dis.coordinate = region.center;
dis.title = row.panName;
dis.subTitle = row.geo_lat;
[myMap addAnnotation:dis];
}
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id<MKAnnotation>)annotation
{ MKPinAnnotationView *pview = nil;
if (annotation != myMap.userLocation)
{
static NSString *defalt = #"Volunteer";
pview = (MKPinAnnotationView *)[myMap dequeueReusableAnnotationViewWithIdentifier:defalt];
if ( pview == nil )
pview = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defalt] autorelease];
pview.pinColor = MKPinAnnotationColorRed;
pview.canShowCallout = YES;
pview.animatesDrop = YES;
}
else
{
[myMap.userLocation setTitle:#"I am Here"];
}
return pview;
}
The above gives me map view with annotation pin. I have to write this code in more than 8 files. Can any one tell me how can I centralized this code ? I mean to create only one class and by object of that class I can add the map in any other view.
Thanks..

Since the rest of your code is not clear, it is hard to give advice, but you may try to analyze it first;
what is common in these 8 code parts, and what is different?
Then you should figure out if you need to create and hold references to any local variables, lets say your map objects, or something like that.
If so, then subclassing would be the first choice to look at,
if not then an Obj-C category would probably do (http://macdevelopertips.com/objective-c/objective-c-categories.html). P.S: you cannot add member fields with this, only behavior (methods) are allowed.
If you need only objects with little to none behavior other than these, then you may make them plain objects holding the parameters only.
Or even you may use static methods like ( +(void) blahblah; ) just to be short, but dont do it unless you are sure that the behavior is generic in nature...
Well, a soup of advice here, if you provide more info, i can write in one of these directions in more details
good luck!

Related

User location image pin disappears most of time

i Am using following code
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([[annotation title] isEqualToString:#"Current Location"] )
{
MKAnnotationView *anView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"currentPin"];
anView.image = [UIImage imageNamed:#"pin_green.png"];
anView.canShowCallout = true;
anView.enabled = true;
return anView;
}
The issue is, it randomly disappears and appears again. Giving a very bad user experience. Any way to fix this?
There are several suspect things about this code:
You're not using dequeue, as someone has pointed out. In particular, the problem here is that you are making a new view every single time, rather than checking to see whether a new view needs making.
You are forgetting the key step, namely, to associate the view with the annotation.
Here is the canonical structure of a simple viewForAnnotation: implementation where we supply our own view:
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>)annotation {
MKAnnotationView* v = nil;
if ([annotation.title isEqualToString:#"Current Location"]) {
static NSString* ident = #"greenPin";
v = [mapView dequeueReusableAnnotationViewWithIdentifier:ident];
if (v == nil) {
v = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:ident];
v.image = [UIImage imageNamed:#"pin_green.png"];
v.canShowCallout = YES;
}
v.annotation = annotation;
}
return v;
}
Since that code works for me, I'd suggest you start with it and tweak it as necessary.
By the way, you do NOT need this method just to get a green pin! You do know that, right? iOS will give you a green pin (MKPinAnnotationColorGreen).
You should use MKMapView's dequeueReusableAnnotationViewWithIdentifier: and see if you get a view back before creating a new one with initWithAnnotation:reuseIdentifier::
MKAnnotationView *anView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"currentPin"];
if (!anView) {
anView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"currentPin"];
anView.image = [UIImage imageNamed:#"pin_green.png"];
anView.canShowCallout = true;
anView.enabled = true;
}
return anView;
That said, I'm not entirely sure this is the cause of your problem.

iPhone - set MKPolylineView lineDashPattern to be animated

I have been trying to work on a application using the MapKit and MKPolyline.
I have gotten the annotations and paths drawn on the map, however I am trying to change the lineDashPattern to be a set of animated dashes in the direction of the course. I know that the lineDashPhase and lineDashPattern together give you a dashed line, and the more you increase lineDashPhase, the more the dashes move, however is there a way to increase the values in such a way that it appears that it is moving, sort of like ants along a line I guess is a good analogy.
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay
{
MKOverlayView* overlayView = nil;
if(overlay == self.routeLine)
{
//if we have not yet created an overlay view for this overlay, create it now.
if(self.routeLineView == nil)
{
self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];
self.routeLineView.lineWidth = 5;
self.routeLineView.lineDashPhase = 15;
NSArray* array = [NSArray arrayWithObjects:[NSNumber numberWithInt:20], [NSNumber numberWithInt:20], nil];
self.routeLineView.lineDashPattern = array;
}
overlayView = self.routeLineView;
}
return overlayView;
}
This right now give me dashed lines. I know there is a similar topic here, however I am not sure how to go about this. Any input would be greatly appreciated!
I know this is a pretty old question, but I have just implemented a similar solution and posted it here.

Trying to Show User Location on Mapkit, the most annoying thing ever. Anyone lend a hand?

I'm following this tutorial (http://icodeblog.com/2009/12/21/introduction-to-mapkit-in-iphone-os-3-0/) on adding mapkit and annotations to an application. However, i'm seriously struggling with the User Location. I'm new to xcode so not quite sure what to do next. I have tried Tony's option:
step one: add the CoreLocation framework to the project.
Step two: add this function to the iCodeMapViewController.m:
- (void)setCurrentLocation:(CLLocation *)location {
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center = location.coordinate;
region.span.longitudeDelta = 0.15f;
region.span.latitudeDelta = 0.15f;
[self.mapView setRegion:region animated:YES];
}
step three: add this code to the ViewForAnnotation Method:
if (annotation != mapView.userLocation) {
//the rest of the ViewForAnnotation code goes here
}else{
CLLocation *location = [[CLLocation alloc]
initWithLatitude:annotation.coordinate.latitude
longitude:annotation.coordinate.longitude];
[self setCurrentLocation:location];
}
But when i go to build, it doesn't like it.
I've also tried this option:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation
{
if ([annotation isKindOfClass:MKUserLocation.class]) return nil;
//rest of code
}
The blue dot shows, my custom annotations show but the app crashes when i try and scroll through the table. The debugger gives no help but does stop on this statement.
Can someone please help? With code examples too? i think the answer to this post might be useful to a number of people also struggling with the mapkit.
Cheers
I had the same problem, but I managed to solve it.
In cellForRowAtIndexPath I did this:
NSMutableArray *annotations = [[NSMutableArray alloc] init];
if(indexPath.section == 0)
{
for(MinuAsukohad *annotation in [mapView annotations])
{
if(![annotation isKindOfClass:[MKUserLocation class]])
{
if([annotation annotationType] == MinuAsukohadTypeInterest)
{
[annotations addObject:annotation];
}
}
}
cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];
}
You just have to repeat it for all the sections.
Sounds like you are trying to include your current location as one of the cells in the table ... look at your console and give us the output when the crash happens.

iPhone Mapkit adding custom image and pins to annotations

I am trying to change the pin colour from the default red to a custom image but whatever I am trying just isn't working.
I have downloaded the sample code from this website:
http://icodeblog.com/2009/12/21/introduction-to-mapkit-in-iphone-os-3-0/
The code works on it's own but when I import the annotation classes into my code, they do not work and I have no idea why. I have also tried a number of different methods from other sites but I can't even get the pin colour to change.
My project has 4 tabs and the MapView is on one of the tabs. When I select it, it parses a JSON string and adds the separate annotations onto the map. i have the title and subtitle showing up when I click on the pin, but cannot change the colour or image.
Here is how I add my annotations - MapAnnotation follows MKAnnotation:
MapAnnotation *ann = [[MapAnnotation alloc] initWithCoordinate:newCoord];
ann.title = [locationDictionary objectForKey:#"name"];
ann.subtitle = [locationDictionary objectForKey:#"name"];
[mapView addAnnotation:ann];
[ann release]
Here is how I try and attempt to change the colour - I have MapView.delegate=self in the view controller:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
MKPinAnnotationView *pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:[annotation title]];
if (pin == nil) {
pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:[annotation title]] autorelease];
}else {
pin.annotation = annotation;
}
pin.pinColor = MKPinAnnotationColorGreen;
pin.animatesDrop = YES;
pin.canShowCallout = TRUE;
return pin;
}
I get the annotations to appear with title and subtitle, just not green markers. They're always red wether I use colours or images. If anyone could help me out, that would be great!
Thanks
EDIT:
The mapView delegate is assigned in the viewDidLoad method. I also add an Overlay to a certain part of the map. This is working fine, I have also taken it out and tried it without that incase it was causing a problem but it still didn't fix it.
- (void)viewDidLoad {
[super viewDidLoad];
MKCoordinateRegion cordRgn;
LusuAppAppDelegate *delegate =[[UIApplication sharedApplication]delegate];
if (delegate.CurrentLocation == 0) {
cordRgn.center.latitude = (CENTRE_OF_POSITION_2_LAT);
cordRgn.center.longitude = (CENTRE_OF_POSITION_2_LON);
delegate.CurrentLocation = 1;
}else if (delegate.CurrentLocation == 1) {
cordRgn.center.latitude = (CENTRE_OF_POSITION_1_LAT);
cordRgn.center.longitude = (CENTRE_OF_POSITION_1_LON);
delegate.CurrentLocation = 0;
}
cordRgn.span.latitudeDelta = 0.009f;
cordRgn.span.longitudeDelta = 0.009f;
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[locationManager startUpdatingHeading];
//add overlay
MKRasterOverlay *overlay = [[MKRasterOverlay alloc] init];
[overlay setCoordinate:CLLocationCoordinate2DMake(54.005508, -2.780507)];
MKMapRect mkrect;
MKMapPoint mkpointa, mkpointb;
mkrect.origin = MKMapPointForCoordinate(CLLocationCoordinate2DMake(54.016964, -2.794862));
mkpointa = MKMapPointForCoordinate(CLLocationCoordinate2DMake(54.016964, 2.000000));
mkpointb = MKMapPointForCoordinate(CLLocationCoordinate2DMake(54.001447, 2.013770));
mkrect.size.width = mkpointb.x - mkpointa.x;
mkrect.size.height = mkpointb.y - mkpointa.y;
overlay.boundingMapRect = mkrect;
mapView.delegate = self;
[mapView addOverlay:overlay];
[mapView setRegion:cordRgn animated:NO];
[self.mapView setShowsUserLocation:YES];
[self doAnnotations];
}
The doAnnotations function is the code shown above but in a loop. Thanks again for your help.
Your code looks fine to me. I suspect you have an error in your delegate assignment and mapView:viewForAnimation is not actually being called. An MKMapView without a delegate providing that function will work fine, with red pins for all annotations. Try adding some NSLog statements or setting a breakpoint in the debugger to make sure you're actually executing this code.
You probably already know this, but using your own images will require you to create pins that are MKAnnotationViews rather than MKAnnotationPinViews.

iPhone MapKit - update annotations coordinates and map

I use this tutorial for integrating MapKit to my application:
http://iphonebcit.wordpress.com/iphone-map-kit-tutorial/
CLLocationCoordinate2D coordinate;
coordinate.latitude = 49.2802;
coordinate.longitude = -123.1182;
NSUInteger count = 1;
for(int i = 0; i < 10; i++) {
CGFloat latDelta = rand()*.035/RAND_MAX - .02;
CGFloat longDelta = rand()*.03/RAND_MAX - .015;
CLLocationCoordinate2D newCoord = {coordinate.latitude+latDelta, coordinate.longitude+longDelta};
MapDemoAnnotation* annotation = [[MapDemoAnnotation alloc] initWithCoordinate:newCoord andID:count++];
[mapView addAnnotation:annotation];
[annotation release];
}
and
- (MKAnnotationView *)mapView:(MKMapView *)mapViewLocal viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapViewLocal dequeueReusableAnnotationViewWithIdentifier:#"Pin"];
if(pinView == nil) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"Pin"];
pinView.pinColor = MKPinAnnotationColorPurple;
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
} else {
pinView.annotation = annotation;
}
return pinView;
}
So the pins will set on the map randomly. In my application, the coordinates will change. How can I change the coordinates of the annotations, so they will be updated on the map?
Does anyone know?
At iPhone SDK 3.x you have to remove the pin annotations and set it again. That is not very nice if you have many annotations an your map.
I try to make it better so I ony display/renew my pin annotations which are on the screen. So if an user zoom in to New York, there won't be pin annotations in San Francisco or other than the user can't see. So the performance will be much better.
Perhaps in the future this would be possible. I hope so :-)
That tutorial is only for getting a map view to show, nothing else. You're going to need a bigger tutorial. I found this one useful:
http://blog.objectgraph.com/index.php/2009/04/02/iphone-sdk-30-playing-with-map-kit/
Probably what you'll need to do is to loop through all the annotations on the map, removing them, then loop through your data array, adding the annotations back again. You could be more clever about it and loop through the data array, checking if there's already a pin with the same latitude/longitude on the map, but that way gets more complicated.
Since I'm doing annotations at the moment I just made a quick test. You do get a compiler warning, so it might not be supported. But it works.
Make a custom MKAnnotation class so you can set the coordinate property to be writable:
#property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;
Then at whatever event or interval you want, change the coordinate using something like this:
CLLocation *loc=[[CLLocation alloc] initWithLatitude:55.0 longitude:17.0];
annotation.coordinate=loc.coordinate;
[loc release];