MKMapView NSInvalidArgumentException Invalid Region crash in ios6 - ios6

Program crashes when set location coordinates using MKMapView.
Log:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid Region <center:+112.57075000, +37.87049600 span:+0.05165163, +0.43945312>'
span in my program is
MKCoordinateSpan span;
span.latitudeDelta = .05;
span.longitudeDelta = .02;
after coding:
self.mMKMapview.region = [self.mMKMapview regionThatFits:region];
as the log shows, span changes to :+0.05165163, +0.43945312
anyone help please, I have been standstill here for two days.
Thanks!

The problem is the center coordinate:
+112.57075000, +37.87049600
The latitude must be from -90 to +90 so +112.57075 is out of range.
Check how the center coordinate is being set or maybe the data is backwards.
Also, you don't need to explicitly call regionThatFits because the map view does it automatically when you set the region normally (ie. just call setRegion). It's normal, by the way, for the map view to adjust the span as needed to fit the map view dimensions or zoom level.

I use the following code to set the region:
if( centerLat > -89 && centerLat < 89 && centerLng > -179 && centerLng < 179 ){
[self.mapView setRegion:region animated:YES];
}

I would rather suggest to use CLLocationCoordinate2DIsValid
so something like
guard CLLocationCoordinate2DIsValid(centerLat) else {
return
}

Related

How to get current latitude and longitude values in codename one

Im trying to get my current latitude and longitude values using LocationManager in Codename One but the values i'm getting is for a different location.
Here is my code:
LocationManager locationManager = LocationManager.getLocationManager();
Location location = locationManager.getCurrentLocation();
Double loc1= location.getLatitude();
Double loc2= location.getLongitude();
System.out.println("Latitude: "+loc1);
System.out.println("Longitude: "+loc2);
Output:
Latitude: 40.715353
Longitude: -74.00497299999999
My correct current Latitude and Longitude values are 3.xxxx and 101.xxxx respectively.
Please how can I get these correct values from LocationManager?
That is default location on Simulator.
Note that you can't get your actual location on Simulator without entering it yourself.
To set your simulator to use your current location, Click Simulate on your simulator and select Location Simulation. Enter your LatLong values for Available and Click Update. The Google map will shift view to your location, So would your app.
You can keep Location Simulator open while you test your app.
On a side note, if your app needs a real time location I would suggest you use getCurrentLocationAsync(). Below is an example:
InfiniteProgress ip = new InfiniteProgress();
Dialog ipDlg = ip.showInifiniteBlocking();
Location location = LocationManager.getLocationManager().getCurrentLocationSync(30000);
ipDlg.dispose();
if (location == null) {
try {
location = LocationManager.getLocationManager().getCurrentLocation();
} catch (IOException err) {
Dialog.show("Location Error", "Unable to find your current location, please be sure that your GPS is turned on", "OK", null);
return;
}
}
Double loc1 = location.getLatitude();
Double loc2 = location.getLongitude();
Log.p("Latitude: " + loc1);
Log.p("Longitude: " + loc2);

Winforms Visibility issues on Dynamically Loaded UserControls?

So I have a program that dynamically loads in usercontrols depending on certain criteria, The problem I'm having is that my validation is only truly working on the first textbox loaded.
The following code is ran for each usercontrol loaded:
Try
TextBox = DirectCast(flpRepeatConts.Controls(Count).Controls("tbAnswerText"), Telerik.WinControls.UI.RadTextBox) ' Control(1) = TextBox Answer
LabelAnsID = DirectCast(flpRepeatConts.Controls(Count).Controls("lblQID"), RadLabel)
LabelMinHolder = DirectCast(flpRepeatConts.Controls(Count).Controls("lblMinTextHolder"), RadLabel)
lblValidationText = DirectCast(flpRepeatConts.Controls(Count).Controls("lblValidationText"), RadLabel)
If TextBox.Text.Length >= LabelMinHolder.Text And TextBox.Text.Length <= TextBox.MaxLength Then
'Good to go.
lblValidationText.Visible = False
Else
'Too little/not enough text
If LabelMinHolder.Text = TextBox.MaxLength Then
lblValidationText.Text = String.Format("Text must be exactly {0} Characters! ({1} of {0})", LabelMinHolder.Text, TextBox.Text.Length)
Else
lblValidationText.Text = String.Format("Text must be between {0} and {1} Characters!", LabelMinHolder.Text, TextBox.MaxLength)
End If
inError = True
lblValidationText.Visible = True
End If
Catch ex As Exception
'Exception Handling Here
End Try
Count += 1
The true issue here is that labels past the first one will never show (unless there's atleast one character in its respective textbox, in which case the error message does show). I'm stumped and was hoping another set of eyes would see something I don't. I've set breakpoints and stepped through this and the text and visibility are in fact being set, they just don't take effect.

How to avoid x axis changed when add marks?

I have a problem to consult,when I use the Line of the Point style and show the marks, the x axis would have a problem.When annotate the code (lineSeries5.Add(5, 1,"ghgh");),the x axis shows normally like number 1,2.....,but when I use this code (lineSeries5.Add(5, 1,"ghgh"); ), the x axis becomes 'ghgh',I wonder if how to solve this problem. My purpose is showing the contents which I want to shows in the marks,but the x axis should not be changed. Waiting for your help, thank you very much!
private Steema.TeeChart.WPF.Styles.Points lineSeries5;
this.lineSeries5 = new Steema.TeeChart.WPF.Styles.Points();
this.tChart1.Series.Add(this.lineSeries5);
lineSeries5.Marks.Visible = true;
lineSeries5.Marks.Transparent = true;
lineSeries5.Pointer.Style = Steema.TeeChart.WPF.Styles.PointerStyles.Nothing;
**lineSeries5.Add(5, 1,"ghgh");**
lineSeries5.Add(9, 3);
If you want in your axis labels are values instead of labelsText, you must change the Axis labels style as I do in next line:
tChart1.Axes.Bottom.Labels.Style = AxisLabelStyle.Value;
Could you tell us if previous code solve your problem?
Thanks,

MKMapView setRegion not exact

Starting Situation
iOS6, Apple Maps. I have two annotations on a MKMapView. The coordinates of the two annotations have the same values for the latitude component, but different longitudes.
What I want
I now want to zoom the map so that one annotation is exactly on the left edge of the mapView's frame, and the other annotation on the right edge of the frame. For that I tried MKMapView's setRegion and setVisibleMapRect methods.
The Problem
The problem is that those methods seem to snap to certain zoom levels and therefore not setting the region as exactly as I need it. I saw a lot of questions on Stack Overflow that point out, that this behavior is normal in iOS5 and below. But since Apple Maps are using vector graphics, the view is not bound to certain zoom levels to display imagery in proper resolution.
Tested in...
I tested on iPhone 4, 4S, 5, iPad 3 and iPad Mini (all with iOS6.1), and in the Simulator on iOS6.1.
My question
So why is setRegion and setVisibleMapRect snapping to certain zoom levels and does not adjust the exact region to the values I pass it?
Sample Code
in view did appear I define 4 different Locations as iVars, and set up the map view:
// define coords
_coord1 = CLLocationCoordinate2DMake(46.0, 13.0);
_coord2 = CLLocationCoordinate2DMake(46.0, 13.1);
_coord3 = CLLocationCoordinate2DMake(46.0, 13.2);
_coord4 = CLLocationCoordinate2DMake(46.0, 13.3);
// define frame for map in landscape mode
CGRect mainScreen = [[UIScreen mainScreen] bounds];
CGRect newSRect = mainScreen;
newSRect.size.width = mainScreen.size.height;
newSRect.size.height = mainScreen.size.width;
// setup map view
customMapView = [[MKMapView alloc] initWithFrame:newSRect];
[customMapView setDelegate:self];
[customMapView setShowsUserLocation:NO];
[self.view addSubview:customMapView];
Then I add 3 buttons. Those trigger all the same method addAnnotationsWithCoord1:coord2. The first button passes _coord1 and _coord2, the second button passes _coord1 and _coord3 and the third button passes _coord1 and _coord4. The method looks like so (TapAnnotation is my subclass of MKAnnotation):
-(void)addAnnotationsWithCoord1:(CLLocationCoordinate2D)coord1 coord2:(CLLocationCoordinate2D)coord2{
// Make 2 new annotations with the passed coordinates
TapAnnotation *annot1 = [[TapAnnotation alloc] initWithNumber:0 coordinate:coord1];
TapAnnotation *annot2 = [[TapAnnotation alloc] initWithNumber:0 coordinate:coord2];
// Remove all existing annotations
for(id<MKAnnotation> annotation in customMapView.annotations){
[customMapView removeAnnotation:annotation];
}
// Add annotations to map
[customMapView addAnnotation:annot1];
[customMapView addAnnotation:annot2];
}
After that I determine the SouthWest and NorthEast points that will determine the rect which is containing my 2 annotations.
// get northEast and southWest
CLLocationCoordinate2D northEast;
CLLocationCoordinate2D southWest;
northEast.latitude = MAX(coord1.latitude, coord2.latitude);
northEast.longitude = MAX(coord1.longitude, coord2.longitude);
southWest.latitude = MIN(coord1.latitude, coord2.latitude);
southWest.longitude = MIN(coord1.longitude, coord2.longitude);
Then I calculate the center point between the two coordinates and set the center coordinate of the map to it (remember, since all coordinates have same latitudes the following calculation should be correct):
// determine center coordinate and set to map
double centerLon = ((coord1.longitude + coord2.longitude) / 2.0f);
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(southWest.latitude, centerLon);
[customMapView setCenterCoordinate:center animated:NO];
Now I try to set the region of the map so that it fits like I want:
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude];
CLLocationDistance meterSpanLong = [loc1 distanceFromLocation:loc2];
CLLocationDistance meterSpanLat = 0.1;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center, meterSpanLat, meterSpanLong);
[customMapView setRegion:region animated:NO];
This does not behave as expected, so I try this:
MKCoordinateSpan span = MKCoordinateSpanMake(0.01, northEast.longitude-southWest.longitude);
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[customMapView setRegion:region animated:NO];
This still not behaves as expected, so I try it with setVisibleMapRect:
MKMapPoint westPoint = MKMapPointForCoordinate(southWest);
MKMapPoint eastPoint = MKMapPointForCoordinate(northEast);
MKMapRect mapRect = MKMapRectMake(westPoint.x, westPoint.y,eastPoint.x-westPoint.x,1);
[customMapView setVisibleMapRect:mapRect animated:NO];
And still, it does not behave like I want. As a verification, I calculate the point distance from the left annotation to the left edge of the mapView's frame:
// log the distance from the soutwest point to the left edge of the mapFrame
CGPoint tappedWestPoint = [customMapView convertCoordinate:southWest toPointToView:customMapView];
NSLog(#"distance: %f", tappedWestPoint.x);
For _coord1 and _coord2 it shows: 138
For _coord1 and _coord3 it shows: 138
For _coord1 and _coord4 it shows: 65
So why do I get these values? If anything works as expected, these should all be 0.
Thanks for any help, struggling with this problem for a week now.
Read the docs on setRegion:
When setting this property, the map may adjust the new region value so that it fits the visible area of the map precisely. This is normal and is done to ensure that the value in this property always reflects the visible portion of the map. However, it does mean that if you get the value of this property right after setting it, the returned value may not match the value you set. (You can use the regionThatFits: method to determine the region that will actually be set by the map.)
You will have to figure out the math yourself if you want a precise zoom.

VirtualEarth: determine min/max visible latitude/longitude

Is there an easy way to determine the maximum and minimum visible latitude and longitude in a VirtualEarth map?
Given that it's not a flat surface (VE uses Mercator projection it looks like) I can see the math getting fairly complicated, I figured somebody may know of a snippet to accomplish this.
Found it! VEMap.GetMapView() returns the bounding rectangle, even works for 3D mode as well (where the boundary is not even a rectangle).
var view = map.GetMapView();
latMin = view.BottomRightLatLong.Latitude;
lonMin = view.TopLeftLatLong.Longitude;
latMax = view.TopLeftLatLong.Latitude;
lonMax = view.BottomRightLatLong.Longitude;
Using the Virtual Earth Interactive SDK, you can see how to convert a pixel point to a LatLong object:
function GetMap()
{
map = new VEMap('myMap');
map.LoadMap();
}
function DoPixelToLL(x, y)
{
var ll = map.PixelToLatLong(new VEPixel(x, y)).toString()
alert("The latitude,longitude of the pixel at (" + x + "," + y + ") is: " + ll)
}
Take a further look here: http://dev.live.com/virtualearth/sdk/ in the menu go to: Get map info --> Convert pixel to LatLong
To get the Max/Min visible LatLong's, you could call the DoPixelToLL method for each corner of the map.

Resources