Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
527 views
in Technique[技术] by (71.8m points)

iphone - MKMapView : setRegion doesn't work !

Aaaarg... ok, let's calm myself.

Did someone have any problem with setting the region of a MKMapView ? It never worked with me.

This code :

-(void)setUserCenteredSpan:(MKCoordinateSpan)span{ // for this example, span = {0.5, 0.5}
    // Current region (just initialised)
NSLog(@"%f, %f - %f, %f",   self.region.center.latitude,
                            self.region.center.longitude, 
                            self.region.span.latitudeDelta, 
                            self.region.span.longitudeDelta);
    // New Region
MKCoordinateRegion region = MKCoordinateRegionMake([[[self userLocation] location] coordinate],
                                                   span);
NSLog(@"%f, %f - %f, %f",   region.center.latitude,
                            region.center.longitude, 
                            region.span.latitudeDelta, 
                            region.span.longitudeDelta);
    // Region saved in MKMapView
[self setRegion:region animated:NO];
NSLog(@"%f, %f - %f, %f",   self.region.center.latitude,
                            self.region.center.longitude, 
                            self.region.span.latitudeDelta, 
                            self.region.span.longitudeDelta);
}

Returns that log :

30.145127, -40.078125 - 0.000000, 0.000000
0.000000, 0.000000 - 0.500000, 0.500000
0.000000, 0.000000 - 0.000000, 0.000000

Do you know why ??!

Thanks a lot, you can save me from killing myself X(

Mart

EDIT : Of course, I am on the device, connected to internet.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't understand exactly the previous logs, but I know where was my error.

The instancied MKMapView was not initialised with a frame, but with an autoresizingMask set to > 0.

When the setRegion method was called, my view was not framed yet. I think the region values are calculated according to the view frame, so these values couldn't be found.

Just set the frame before doing a setRegion, and it would display normally.

Bye !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...