How to save MKMapRect in a plist
Asked Answered
B

4

5

I am creating a MKMapView application and in need to save a couple MKMapRect type variables in a plist so as to refer them when need.

I know that MKMapRect has MKMapPoint origin and MKMapSize size. And they each have 2 double values that can be saved as nsnumber but saving all of them seems to be a lot of work and top of that i have to read the values back and convert them into a MKMapRect variable.

So my question is that, is there any easy way to store a MKMapRect and retrive it back from a plist.

Thanks,
Robin.

Bellinger answered 3/8, 2011 at 8:16 Comment(0)
D
7

Use MKStringFromMapRect to turn it into a string.

Diaeresis answered 3/8, 2011 at 8:27 Comment(5)
And how to I get the MKMapRect from that string.Bellinger
Yeah there doesn't seem to be an opposite function, don't know why I would raise a bug with Apple, you could probable use CGRectFromString and create a MKMapRect from it, but to be safe you are probable better of doing what andreamazz suggested.Diaeresis
U have raised a bug issue with Apple Problem ID: 9887906Diaeresis
'U have raised a bug' is suppose to be 'I have raised a bug'Diaeresis
Hey thanks, just let me know if they reply to you with any feedback. Also you can report the same bug on openradar so that people can see the progress of your bug report.Thanks anyway.Bellinger
R
5

There:

- (NSString *)save:(MKMapRect)rect
{
     return MKStringFromMapRect(rect);
}

- (MKMapRect)load:(NSString *)str
{
    MKMapRect mapRect;
    CGRect rect = CGRectFromString(str);
    mapRect.origin.x = rect.origin.x;   
    mapRect.origin.y = rect.origin.y;
    mapRect.size.width = rect.size.width;
    mapRect.size.height = rect.size.height;
    return mapRect;
}
Rieth answered 3/8, 2011 at 13:22 Comment(2)
nice but would be nicer as a category on NSString.Pyrex
Keep in mind that CGRect value might be base on the float datatype and you could loose precision because CLLocationDegree is based on double.Alimony
M
2

I made a category to save the map rect to the user defaults:

NSUserDefaults+MKMapRect.h

@interface NSUserDefaults (MKMapRect)

//stores a map rect in user defaults
-(void)setMapRect:(MKMapRect)mapRect forKey:(NSString*)key;
//retrieves the stored map rect or returns the world rect if one wasn't previously set.
-(MKMapRect)mapRectForKey:(NSString*)key;

@end

NSUserDefaults+MKMapRect.m

@implementation NSUserDefaults (MKMapRect)

-(void)setMapRect:(MKMapRect)mapRect forKey:(NSString*)key{
    NSMutableDictionary *d = [NSMutableDictionary dictionary];
    [d setObject:[NSNumber numberWithDouble:mapRect.origin.x] forKey:@"x"];
    [d setObject:[NSNumber numberWithDouble:mapRect.origin.y] forKey:@"y"];
    [d setObject:[NSNumber numberWithDouble:mapRect.size.width] forKey:@"width"];
    [d setObject:[NSNumber numberWithDouble:mapRect.size.height] forKey:@"height"];

    [self setObject:d forKey:key];
}

-(MKMapRect)mapRectForKey:(NSString*)key{
    NSDictionary *d = [self dictionaryForKey:key];
    if(!d){
        return MKMapRectWorld;
    }
    return MKMapRectMake([[d objectForKey:@"x"] doubleValue],
                         [[d objectForKey:@"y"] doubleValue],
                         [[d objectForKey:@"width"] doubleValue],
                         [[d objectForKey:@"height"] doubleValue]);
}

@end
Montpelier answered 11/11, 2013 at 5:45 Comment(0)
R
0

You could probably copy the MKMapRect data in a CGRect, then store the CGRect as a string with NSStringFromgCGRect() and CGRectFromString()

Rieth answered 3/8, 2011 at 8:25 Comment(1)
4 lines of code to save, 4 to load. I'll past it in a new answerRieth

© 2022 - 2024 — McMap. All rights reserved.