Calculating the latidude and longitude of an area
-
I’m trying to get the lowlatitude, highlatidude and lowlongitude, highlongitude of an area given the zip code and the radius in miles. This is what I’ve done but I think it is incorrect. Anyone has a better calculation? Thanks, Martin Sub SetRadius(ByVal iRadius, ByVal iZip) Dim rs, SQL, iStartlat, iStartlong, LatRange, LongRange SQL = "SELECT LATITUDE, LONGITUDE FROM ZIP_CODES WHERE ZIP = '" & iZip & "'" Dim strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\PROJECT\zipbase.mdb" Dim Conn = New OleDbConnection(strConn) Conn.open() Dim cmdSelect As New OleDbCommand(SQL, Conn) Dim dr As OleDbDataReader dr = cmdSelect.ExecuteReader() While dr.Read() iStartlat = dr(0) iStartlong = dr(1) End While Conn.Close() LatRange = iRadius / ((6076 / 5280) * 60) LongRange = iRadius / (((Cos(CDbl(iStartlat * 3.141592653589 / 180)) * 6076) / 5280) * 60) LowLatitude = iStartlat - LatRange HighLatitude = iStartlat + LatRange LowLongitude = iStartlong - LongRange HighLongitude = iStartlong + LongRange end sub
-
I’m trying to get the lowlatitude, highlatidude and lowlongitude, highlongitude of an area given the zip code and the radius in miles. This is what I’ve done but I think it is incorrect. Anyone has a better calculation? Thanks, Martin Sub SetRadius(ByVal iRadius, ByVal iZip) Dim rs, SQL, iStartlat, iStartlong, LatRange, LongRange SQL = "SELECT LATITUDE, LONGITUDE FROM ZIP_CODES WHERE ZIP = '" & iZip & "'" Dim strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\PROJECT\zipbase.mdb" Dim Conn = New OleDbConnection(strConn) Conn.open() Dim cmdSelect As New OleDbCommand(SQL, Conn) Dim dr As OleDbDataReader dr = cmdSelect.ExecuteReader() While dr.Read() iStartlat = dr(0) iStartlong = dr(1) End While Conn.Close() LatRange = iRadius / ((6076 / 5280) * 60) LongRange = iRadius / (((Cos(CDbl(iStartlat * 3.141592653589 / 180)) * 6076) / 5280) * 60) LowLatitude = iStartlat - LatRange HighLatitude = iStartlat + LatRange LowLongitude = iStartlong - LongRange HighLongitude = iStartlong + LongRange end sub
Ok, I’m not the seasoned programmer but you are at least talking in terms that I am very familiar with. If I read everything correctly, you have center latitude, center longitude, and a radius in miles. I am assuming the following; your Latitude and Longitude are in degree format, i.e. +/- 30.5 and your radius is in statute miles. Since I am use to doing all my calculation in Nautical Miles, I would convert the radius first: RadiusNM = iRadius / 1.15077945 (1 NM = 1.15077945 statute mile) Since there are 60 nautical miles in a degree, I would convert the RadiusNM into degree format so it is in the same format as my other data. RadiusD = RadiusNM / 60 Now that the radius is in nautical terms in degree format, you can simple add and subtract it from your center latitude to get the High / Low latitudes. HighLatitude = CenterLatitude + RadiusD LowLatitude = Centerlatitude – RadiusD For longitude you will need to multiply the distance of your radius in nautical miles by the COS of the center latitude. HighLongitude = CenterLongitude + (RadiusD * COS(CenterLatitude)) LowLongitude = CenterLongitude - (RadiusD * COS(CenterLatitude))
-
Ok, I’m not the seasoned programmer but you are at least talking in terms that I am very familiar with. If I read everything correctly, you have center latitude, center longitude, and a radius in miles. I am assuming the following; your Latitude and Longitude are in degree format, i.e. +/- 30.5 and your radius is in statute miles. Since I am use to doing all my calculation in Nautical Miles, I would convert the radius first: RadiusNM = iRadius / 1.15077945 (1 NM = 1.15077945 statute mile) Since there are 60 nautical miles in a degree, I would convert the RadiusNM into degree format so it is in the same format as my other data. RadiusD = RadiusNM / 60 Now that the radius is in nautical terms in degree format, you can simple add and subtract it from your center latitude to get the High / Low latitudes. HighLatitude = CenterLatitude + RadiusD LowLatitude = Centerlatitude – RadiusD For longitude you will need to multiply the distance of your radius in nautical miles by the COS of the center latitude. HighLongitude = CenterLongitude + (RadiusD * COS(CenterLatitude)) LowLongitude = CenterLongitude - (RadiusD * COS(CenterLatitude))
Thank you for your reply. I tried your suggestion and I got the same result. I think that my zip code database is incorrect. Here is where I got the DB from : http://www.cfdynamics.com/zipbase/ If I try zip = 33182 and miles = 1 I get 71 zip codes. And if I try zip = 33182 and miles = 12 I also get 71 zip codes. This is how I get the zip codes: Private Sub getZip(ByVal LowLatitude As String, ByVal LowLongitude As String, ByVal HighLatitude As String, ByVal HighLongitude As String) If (LowLongitude.Substring(3, 1) = ".") Then LowLongitude = "-0" + LowLongitude.Substring(1, LowLongitude.Length - 1) If (HighLongitude.Substring(3, 1) = ".") Then HighLongitude = "-0" + HighLongitude.Substring(1, HighLongitude.Length - 1) Dim sql = "Select ZIP FROM ZIP_CODES WHERE latitude > '+" + LowLatitude + "' and latitude < '+" + HighLatitude + "' and longitude < '" + LowLongitude + "' and longitude > '" + HighLongitude + "';" Dim strConn As String Dim strSQL As String Dim Conn As OleDbConnection Dim objDA As OleDbDataAdapter Dim objDS As New DataSet() strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\PROJECT\zipbase.mdb" Conn = New OleDbConnection(strConn) Try Conn.Open() objDA = New OleDbDataAdapter(sql, Conn) objDA.Fill(objDS, "ZIP_CODES") Catch ex As Exception MsgBox(ex.Message) End Try Conn.Close() ComboBox1.DisplayMember = "ZIP" ComboBox1.ValueMember = "ZIP" ComboBox1.DataSource = objDS.Tables("ZIP_CODES") MsgBox(ComboBox1.Items.Count.ToString) ‘display the # of zip found End Sub THANKS AGAIN, MARTIN
-
Thank you for your reply. I tried your suggestion and I got the same result. I think that my zip code database is incorrect. Here is where I got the DB from : http://www.cfdynamics.com/zipbase/ If I try zip = 33182 and miles = 1 I get 71 zip codes. And if I try zip = 33182 and miles = 12 I also get 71 zip codes. This is how I get the zip codes: Private Sub getZip(ByVal LowLatitude As String, ByVal LowLongitude As String, ByVal HighLatitude As String, ByVal HighLongitude As String) If (LowLongitude.Substring(3, 1) = ".") Then LowLongitude = "-0" + LowLongitude.Substring(1, LowLongitude.Length - 1) If (HighLongitude.Substring(3, 1) = ".") Then HighLongitude = "-0" + HighLongitude.Substring(1, HighLongitude.Length - 1) Dim sql = "Select ZIP FROM ZIP_CODES WHERE latitude > '+" + LowLatitude + "' and latitude < '+" + HighLatitude + "' and longitude < '" + LowLongitude + "' and longitude > '" + HighLongitude + "';" Dim strConn As String Dim strSQL As String Dim Conn As OleDbConnection Dim objDA As OleDbDataAdapter Dim objDS As New DataSet() strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\PROJECT\zipbase.mdb" Conn = New OleDbConnection(strConn) Try Conn.Open() objDA = New OleDbDataAdapter(sql, Conn) objDA.Fill(objDS, "ZIP_CODES") Catch ex As Exception MsgBox(ex.Message) End Try Conn.Close() ComboBox1.DisplayMember = "ZIP" ComboBox1.ValueMember = "ZIP" ComboBox1.DataSource = objDS.Tables("ZIP_CODES") MsgBox(ComboBox1.Items.Count.ToString) ‘display the # of zip found End Sub THANKS AGAIN, MARTIN
Do you know where I can get another DB just like the one I'm using?
-
Thank you for your reply. I tried your suggestion and I got the same result. I think that my zip code database is incorrect. Here is where I got the DB from : http://www.cfdynamics.com/zipbase/ If I try zip = 33182 and miles = 1 I get 71 zip codes. And if I try zip = 33182 and miles = 12 I also get 71 zip codes. This is how I get the zip codes: Private Sub getZip(ByVal LowLatitude As String, ByVal LowLongitude As String, ByVal HighLatitude As String, ByVal HighLongitude As String) If (LowLongitude.Substring(3, 1) = ".") Then LowLongitude = "-0" + LowLongitude.Substring(1, LowLongitude.Length - 1) If (HighLongitude.Substring(3, 1) = ".") Then HighLongitude = "-0" + HighLongitude.Substring(1, HighLongitude.Length - 1) Dim sql = "Select ZIP FROM ZIP_CODES WHERE latitude > '+" + LowLatitude + "' and latitude < '+" + HighLatitude + "' and longitude < '" + LowLongitude + "' and longitude > '" + HighLongitude + "';" Dim strConn As String Dim strSQL As String Dim Conn As OleDbConnection Dim objDA As OleDbDataAdapter Dim objDS As New DataSet() strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\PROJECT\zipbase.mdb" Conn = New OleDbConnection(strConn) Try Conn.Open() objDA = New OleDbDataAdapter(sql, Conn) objDA.Fill(objDS, "ZIP_CODES") Catch ex As Exception MsgBox(ex.Message) End Try Conn.Close() ComboBox1.DisplayMember = "ZIP" ComboBox1.ValueMember = "ZIP" ComboBox1.DataSource = objDS.Tables("ZIP_CODES") MsgBox(ComboBox1.Items.Count.ToString) ‘display the # of zip found End Sub THANKS AGAIN, MARTIN
Have you stoped in the middle of this and checked to see what High / low Latitude and Longitude you are sending in the SQL statement. Since you know what the Latitude and Longitude of the zip is (33182 Lat 25.7802 & Long -80.4182), and you know what the distance is, you should know what the high and lows should be (High lat 25.7946 and low lat 25.76571). Seems pretty simple... BTW... Miami has 134 records that come up if you check for zip codes at zip-codes.com.
-
Have you stoped in the middle of this and checked to see what High / low Latitude and Longitude you are sending in the SQL statement. Since you know what the Latitude and Longitude of the zip is (33182 Lat 25.7802 & Long -80.4182), and you know what the distance is, you should know what the high and lows should be (High lat 25.7946 and low lat 25.76571). Seems pretty simple... BTW... Miami has 134 records that come up if you check for zip codes at zip-codes.com.
I did stop and checked the high and low Latitude/Longitude and all 4 look fine, but I get the same result with a radius of 1 mile and a radius of 12 miles. I think the DB I'm using is wrong. Do you know where I can find another zip code DB? Thanks again, Martin