Reputation: 2970
I want to display a scale on a mapView. I found this thread, from which I added the following code to my mapViewController.m:
- (void)mapView:(MKMapView *)map regionDidChangeAnimated:(BOOL)animated {
MKCoordinateSpan span = mapView.region.span;
NSLog(@" 1 = ~111 km -> %f = ~ %f km ",span.latitudeDelta,span.latitudeDelta*111);
}
The above code logs the distance in kilometers from the top of the region of the map displayed in the mapview to the bottom and it updates whenever that distance changes. The next logical step would be to display that information (from my NSLog) into a UILabel. I found this thread on just that, but it is not working for me. To be clear, I get an error saying that "string" is not defined when I try to enter that code into either my viewDidLoad or my abovementioned regionDidChangeAnimated method. I also tried the method explained here, and I got the same result (plus something saying that "%" was unexpected).
What I want to know is how to display my NSLog output in a UILabel. Once I have that figured out, I plan to add an inch-long bar to the side of the screen (just a picture of a line that is one inch long) and divide that NSLog output by the number of inches from the top to the bottom of the mapView. Then I will display the number of km in a UILabel next to that line, so that it will look similar to scales found on traditional maps. I will also have to edit the UILabel to only say the number of kilometers, as the NSLog writes out a lot of contextual information too (e.g., "2013-07-11 15:02:20.775 Controller 2.0[1076:907] 1 = ~111 km -> 15.994178 = ~ 1775.353763 km ").
Controller 2.0 is the name of my app (to avoid confusion RE the NSLog)
Thank you!
EDIT- per KHansenSF's advice, I tried to add a string. Nothing changed. Here is what I did: in the @interface on the .h, I wrote
IBOutlet UILabel *scaleLabel;
NSString *latDistance;
Then after the } I wrote:
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic, retain) IBOutlet UILabel *scaleLabel;
@property (nonatomic, retain) NSString *latDistance;
- (IBAction) getLatDistance;
Then in the .m I synthesized everything and wrote
scaleLabel.text = latDistance;
In the viewDidLoad and
- (IBAction) getLatDistance
{
MKCoordinateSpan span = mapView.region.span;
NSString *latDistance = [NSString stringWithFormat:@" 1 = ~111 km -> %f = ~ %f km ",span.latitudeDelta,span.latitudeDelta*111];
}
after the viewDidLoad. Nothing changed. I must be declaring something incorrectly or passing the information incorrectly but regardless I am stymied. Any input is appreciated.
NOTE- I just found this extension for adding a scale to a MapView on GitHub but the page has no directions and the ReadMe is empty. Anyone know how to implement this? Alternatively, I found this thread on how to make such a scale on Android. Possibly such a file could be ported to iOS, for example with a tool like this. I will investigate this further but any input is appreciated. Thanks!
EDIT- Now that I am done, here is a screenshot of the finished product. I blurred out the contents of the map annotation and the company name in the header for privacy purposes. It works beautifully!
Upvotes: 2
Views: 2029
Reputation: 2422
Try this:
- (void)mapView:(MKMapView *)map regionDidChangeAnimated:(BOOL)animated {
MKCoordinateSpan span = mapView.region.span;
NSString * scaleString = [NSString stringWithFormat:@" 1 = ~111 km -> %f = ~ %f km", span.latitudeDelta, span.latitudeDelta*111];
NSLog(@"%@", scaleString);
self.scaleLabel.text = scaleString;
}
Upvotes: 2
Reputation: 604
I can't see how you are creating the string. But you would want something like this to get the string similar to the NSLog
NSString *string = [NSString stringWithFormat:@" 1 = ~111 km -> %f = ~ %f km ",span.latitudeDelta,span.latitudeDelta*111];
Upvotes: 0