Latitude Longitude text box

968 views
Skip to first unread message

rhaazy

unread,
Dec 16, 2008, 9:54:13 AM12/16/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I have to add a couple of controls to my form for input of
geographical cooridinates(Degrees Minutes Seconds)

I was wondering if anyone here has some useful suggestions for input
controls or data validation.

I've been searching the google for a while but can't find anything
very useful.

Thanks.

rhaazy

unread,
Dec 16, 2008, 10:23:31 AM12/16/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Forgot to add that this is asp.net
Data input will be like 123.4523 -> 123°45'23"

Cerebrus

unread,
Dec 16, 2008, 11:03:34 AM12/16/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
You should be able to do it easily using RegexValidators or
CustomValidators. Or write your own textbox controls that encapsulate
the behaviour required.

If you need help with the Regex, please provide more details/examples
on the type of data that is valid and invalid.
> > Thanks.- Hide quoted text -
>
> - Show quoted text -

rhaazy

unread,
Dec 16, 2008, 11:36:36 AM12/16/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Thanks for your reply.

The data entered will no larger than 7 numbers with 4 always to the
right of the decimal.
A valid Latitude value is -90 to 90

A valid Longitude value is -180 to 180

the minutes and seconds must be no larger than 59.


A few more examples

45.0003 -> 45°0'3"
-145.3453 -> -145°34'53"
123.7592" -> 124°16'32" - this example rounds to get rid of invalid
minutes/seconds
87 -> 87°0'0"
-23.59 -> 23°59'0" (this number will be saved to database as -23.5900

Besides the validation I am trying to figure out how the text will get
formated.
My thoughts are that I would have to create some kind of "on leave"
logic for the textbox.
Is there a way to mask the input of the textbox in asp.net?
> > - Show quoted text -- Hide quoted text -

Sharp ArunKumar

unread,
Dec 16, 2008, 3:05:05 PM12/16/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Your validation easly made by various string functions,First you valid
the data then you can format the data as you like.
First input data Split into three value , Assume user give 45.0003
in the LLtextBox

Then you can split the value into two look the following C# code

string[] s ;
s = LLtextBox.Text.Split('.');
int Longitude= Convert.ToInt16(s[0]); //45
int Latitude= Convert.ToInt16(s[1]); //0003
//then again latitude value into two using substring functions
string v1= s[1].Substring(0, 2); //00
string v2= s[1].Substring(2); //03
if user give less then three digit value eg 87
call above split function
now s[0] contain 87
but s[1] contain Null value ,then you can assign default value
Then you can format the data using string functions like insertchar
-----------------------
Arun Kumar

rhaazy

unread,
Dec 17, 2008, 4:19:48 PM12/17/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I added a couple of regular expression validators to my asp fields but
I am having difficulty coming up with a regular expression for this,
anyone have any suggestions?
My RegEx-Fu is very poor.

Any kind of latitude / longitude regular expression would be nice,
preferably one that uses the signed and unsigned decimal as opposed to
cardinal direction.
I have googled this for a while but can't find any example to use as a
good starting point.

(\d{1,3} \d{1,2}\.\d{1,2} [NSEW])
latitude or longitude coordinates in the form 46 37.73 N

but I would rather have this be something closer to:
46°37'73" (46 37.73 S would be better as -46°37'73")

Again, any feedback is appreciated.

starmister

unread,
Dec 17, 2008, 5:37:45 PM12/17/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Given the complexity of dealing with the conversions between degree/
hour minutes seconds notation and decimal form, you may want to create
a custom control that is comprised of several text boxes and a drop
down or option control, in order to ensure valid entries.

Do the internal conversion processing (instead of using split()
function) on a button click to 'submit' it or after your control loses
focus (user tabs out, clicks somewhere else):

1) set up three text boxes aligned in a row, for collecting numeric
values comprising one total entry. The first box would allow numbers
0 to 180, the next two would hold values 0 to 59. In the first box,
use a simple RegEx allowing 1 to 3 digits and value range of 0 to 180.
For the second and third box, use a RegEx to check for two digits,
and values 0 to 59. After validation, or after tabbing out, you can
prepend a '0' zero to the entered value, if the value entered is less
than 10. (If you need decimal portion of second, you could add
another text box.)

2) to handle negatives, set up a drop down box or option control for
+/- or, if you want to see cardinality indicators, you'll need one for
N/S and one for E/W depending on whether collecting a longitude or
latitude entry.

Hope this idea is helpful.

Cerebrus

unread,
Dec 18, 2008, 3:14:38 PM12/18/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hey Rhaazy,

I was working on a suitable Regex for you last night, but I couldn't
finish the minutes-seconds part and it was too late anyway. So here's
what I've managed to come up with:

Latitude (-90° to 90°)
------------------------------
^(?<Degrees>(?:-?[1-8][\d]?)|(?:-?[9][0]))(?:\.(?<Minutes>[0-5][\d])(?
<Seconds>[0-5][\d]))?$

Longitude (-180° to 180°)
------------------------------------
^(?<Degrees>(?:(-?[1][0-7]\d?)|-?[1-9]\d?)|([1][8][0]))(?:\.(?<Minutes>
[0-5][\d])(?<Seconds>[0-5][\d]))?$

RegexOptions - Multiline.

I validated these expressions against the following samples and they
seem to work fine :
---
123.4523
1.0004
45.0003
-145.3453
123.7592
87
-23.59
-90
90
91
181
180.0000
---

Of course, I'm too lazy to try with a wider sample so there may be
something I've overlooked. IOW, there are no guarantees and you should
test them with your own data. I'd also be very interested to know if
you can optimize them or find a better way to do it.

You say that you could not find ANY Regex for this kind of validation
online ? I'm surprised and I didn't want to spoil my challenge (I love
Regex) so I haven't googled this. If you are right, it might be
helpful to others if we post this on RegexLib.com. What say ? ;-)

Cerebrus

unread,
Dec 18, 2008, 3:14:46 PM12/18/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
That's a good suggestion and it occurred to me earlier. But I rejected
it based on my experiences with obstinate clients (assuming that the
project requirement would *insist* on a single textbox. Or maybe I'm
getting too paranoid, eh, Rhaazy ?

rhaazy

unread,
Dec 30, 2008, 10:10:27 AM12/30/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Cerebrus, sounds like you've been around the block once or twice ;-)
Of course my client insists that this be a single textbox for latitude
and one for longitude.

Thanks for your work on that regex, I will play around with it and see
what I can use.
We managed to get this working to our clients expectations after a
little clarification on their part.
Apparently latitude and longitudes work as a percentage of 100, and
not like a clock like I had thought...
So there is no rounding to figure out, the only thing left for me to
do was add logic to automatically add the decimal point after two
numbers were typed for latitude and after three numbers were typed for
longitude (using JavaScript). The only thing left for me to do would
be to somehow mask the display to show the degree minute and second
symbols...but our client is used to not seeing this mask now so they
are happy with it as is.

Cerebrus

unread,
Dec 30, 2008, 11:09:57 AM12/30/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
You're welcome, rhaazy! Glad to know your client is finally happy.

I'd be interested to know if the Regex works in the scenario you
outlined. Lemme know if you get a chance to try it out in your leisure
time.
Reply all
Reply to author
Forward
0 new messages