-
Nov 15th, 2013, 10:31 AM
#1
Thread Starter
PowerPoster
calculate distance from zip code vs zip code
I need to calculate distance in Km from zip code to zip code...?
Suggest me a method iin vb6.
Tkx
-
Nov 15th, 2013, 10:39 AM
#2
Re: calculate distance from zip code vs zip code
I've used third party software in the past. I've seen mechanisms on SQL forum sites where people created there own tables from information that can be bought. One issue always is the distance as "the crow flies" or actual driving distance? From what part of the starting zip code to the other. For example if your are across the street from the next zip code it is just a few feet. If you are across town it can be many miles. Same with going as the crow flies or up and down hills and curves, there can be many miles difference.
I guess what I'm saying is I would look at third pary software. I think MapQuest has a VB interface and subscriptions to updates for calculating distance. I used it for an app to schedule doctor appointments.
-
Nov 15th, 2013, 11:25 AM
#3
Re: calculate distance from zip code vs zip code
If you know Lat/Long for both geo locations then here is what you can do:
(note: original code was written in C# - I just converted it to VB6 and tested).
Code:
Option Explicit
Private Const PI As Double = 3.14159265358979
Private Function CalculateDistance(dLat1 As Double, dLong1 As Double, _
dLat2 As Double, dLong2 As Double) As Long
'returns distance in feet between 2 geo points
Dim dLat As Single
Dim dLong As Single
Dim Lat1 As Single
Dim Lat2 As Single
Dim a As Double
Dim c As Double
Dim d As Long
Const R As Double = 6371 * 3280.84
dLat = DegreesToRadians(dLat2 - dLat1)
dLong = DegreesToRadians(dLong2 - dLong1)
Lat1 = DegreesToRadians(dLat1)
Lat2 = DegreesToRadians(dLat2)
a = VBA.Sin(dLat / 2) * VBA.Sin(dLat / 2) + VBA.Sin(dLong / 2) * VBA.Sin(dLong / 2) * VBA.Cos(Lat1) * VBA.Cos(Lat2)
c = 2 * Atan2(VBA.Sqr(a), VBA.Sqr(1 - a))
d = (R * c)
CalculateDistance = d
End Function
Private Function DegreesToRadians(ByVal degrees As Single) As Single
DegreesToRadians = degrees / 57.29578
End Function
Private Function Atan2(y As Double, x As Double) As Double
If x > 0 Then
Atan2 = VBA.Math.Atn(y / x)
ElseIf x < 0 Then
Atan2 = VBA.Math.Sgn(y) * (PI - VBA.Math.Atn(VBA.Math.Abs(y / x)))
ElseIf y = 0 Then
Atan2 = 0
Else
Atan2 = VBA.Math.Sgn(y) * PI / 2
End If
End Function
'usage:
Dim lDistance As Long
lDistance = CalculateDistance(43.0780216666667,-79.0790033333333,43.0783,-79.079515)
For what I need this works pretty accurate - results were compared to various sources and they are consistent.
Last edited by RhinoBull; Nov 15th, 2013 at 11:28 AM.
-
Nov 16th, 2013, 04:37 AM
#4
Thread Starter
PowerPoster
Re: calculate distance from zip code vs zip code
Originally Posted by TysonLPrice
I've used third party software in the past. I've seen mechanisms on SQL forum sites where people created there own tables from information that can be bought. One issue always is the distance as "the crow flies" or actual driving distance? From what part of the starting zip code to the other. For example if your are across the street from the next zip code it is just a few feet. If you are across town it can be many miles. Same with going as the crow flies or up and down hills and curves, there can be many miles difference.
I guess what I'm saying is I would look at third pary software. I think MapQuest has a VB interface and subscriptions to updates for calculating distance. I used it for an app to schedule doctor appointments.
ok tkx!!!!!!!!!!!!!!!
but the lDistance result is in miles, meters, km... or?
Sorry Tyson... is for Rhinobull:-)
Last edited by luca90; Nov 16th, 2013 at 04:40 AM.
Reason: Sorry Tyson... is for Rhinobull:-)
-
Nov 16th, 2013, 07:52 AM
#5
Re: calculate distance from zip code vs zip code
Originally Posted by luca90
ok tkx!!!!!!!!!!!!!!!
but the lDistance result is in miles, meters, km... or?
Sorry Tyson... is for Rhinobull:-)
Unless I'm missing something your still going to have the "as he crow flies" versus actual miles issue. If you are acting under some governmental restriction, like a Doctor has to be within nnn miles, you could get into trouble. Also, why not use what is already available.
http://www.freemaptools.com/how-far-is-it-between.htm
I don't have any skin in the game either way. Just putting it out there.
-
Nov 16th, 2013, 01:58 PM
#6
Re: calculate distance from zip code vs zip code
Probably not legal for a commercial application but if it's for personal use try something like;
Code:
Private Sub CbMap_Click()
Dim qText$
qText$ = Trim$(TbPostCode.Text) 'TbPostCode is a TextBox containing a UK postcode
qText$ = Replace(UCase$(Trim$(qText$)), " ", "%20")
If Len(qText$) > 2 Then
On Error Resume Next
ShellExecute HWND, vbNullString, "http://maps.google.co.uk/maps?sourceid=navclient&ie=UTF-8&rls=RNWG,RNWG:2004-15,RNWG:en&q=" & _
qText$ & _
"&um=1&sa=N&tab=wl", vbNullString, vbNullString, vbNormalFocus
End If
End Sub
The code above will show a Google map for a given Postcode in IE, with a little work I expect it could show one for a route between two postcodes. It may be possible to recover the distance from the webpage too.
-
Nov 16th, 2013, 03:13 PM
#7
Thread Starter
PowerPoster
Re: calculate distance from zip code vs zip code
First tkx to the all!
But all my requet are only for Italy zip code and city. I dont need other country.;-)
-
Nov 16th, 2013, 05:28 PM
#8
Re: calculate distance from zip code vs zip code
Originally Posted by luca90
but the lDistance result is in miles, meters, km... or?
If you pay attention then you should see comments right before variable declaration.
'returns distance in feet between 2 geo points
-
Nov 16th, 2013, 07:24 PM
#9
Re: calculate distance from zip code vs zip code
google maps will work OK for italy
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Nov 17th, 2013, 06:29 AM
#10
Re: calculate distance from zip code vs zip code
Originally Posted by Magic Ink
Probably not legal for a commercial application but if it's for personal use try something like;
Violating terms of use is not ok just because one claims "personal use."
http://www.google.com/help/terms_maps.html
2. Restrictions on Use. Unless you have received prior written authorization from Google (or, as applicable, from the provider of particular Content), you must not: (a) copy, translate, modify, or make derivative works of the Content or any part thereof; (b) redistribute, sublicense, rent, publish, sell, assign, lease, market, transfer, or otherwise make the Products or Content available to third parties; (c) reverse engineer, decompile or otherwise attempt to extract the source code of the Service or any part thereof, unless this is expressly permitted or required by applicable law; (d) use the Products in a manner that gives you or any other person access to mass downloads or bulk feeds of any Content, including but not limited to numerical latitude or longitude coordinates, imagery, and visible map data; (e) delete, obscure, or in any manner alter any warning or link that appears in the Products or the Content; or (f) use the Service or Content with any products, systems, or applications for or in connection with (i) real time navigation or route guidance, including but not limited to turn-by-turn route guidance that is synchronized to the position of a user's sensor-enabled device; or (ii) any systems or functions for automatic or autonomous control of vehicle behavior; (g) use the Products to create a database of places or other local listings information.
Google offers a Maps API for legitimate automated use. However it requires a different method of access, provides advertising you must display, and can only be used in a service open to the public via the web or mobile devices.
Basically it doesn't seem to be legal to ever use Google Maps through webscraping, and not even via the Google Maps API from a private desktop program unless you register for Google Maps API for Business.
So... it would be piracy, and a prohibited topic here.
-
Nov 17th, 2013, 06:38 AM
#11
Re: calculate distance from zip code vs zip code
Besides the facts of all the rules dilettante just stated...
How about the reality of the fact that if Google allowed people to hit the service "unknown" to them how could they scale it for the paying customers?
Would you want to support an operation like that? Would you want to sell a service like that?
It's like 2 people buying a pizza and having 3 other people show up to eat...
Stealing is stealing it's just wrong!
-
Nov 17th, 2013, 06:40 AM
#12
Thread Starter
PowerPoster
Re: calculate distance from zip code vs zip code
Peraphs solved with the link:
http://www.cap-italiani.com/distanza-tra-due-cap
this site calculate the distance from 2 Zip code(CAP in Italy), in Route Km and Direct line on air.
I think now...
Is possible to insert in the two boxes the value of Zip Code and give the result from the page?
Possible via VB6 or VBa for Excel
-
Nov 17th, 2013, 08:00 AM
#13
Re: calculate distance from zip code vs zip code
If you check around you can find alternative web services that provide precisely this function. Some are completely free with registration, others are free for low volume use (25 to 50 hits per day). Most of those you can count on over a long period of time (two years is a long time in Internet Time) require at least registration if not a paid subscription.
Post #12 suggests another web scraping option. However web scraping is never legal unless terms of use or a written agreement give you permission.
-
Nov 17th, 2013, 10:50 AM
#14
Re: calculate distance from zip code vs zip code
-
Nov 18th, 2013, 08:17 AM
#15
Thread Starter
PowerPoster
Re: calculate distance from zip code vs zip code
Originally Posted by TysonLPrice
Unless I'm missing something your still going to have the "as he crow flies" versus actual miles issue. If you are acting under some governmental restriction, like a Doctor has to be within nnn miles, you could get into trouble. Also, why not use what is already available.
http://www.freemaptools.com/how-far-is-it-between.htm
I don't have any skin in the game either way. Just putting it out there.
Hi Tyson...
I have tested your link, and work perfect...
but i need to get, via vb6 code, the value in the box: Distance by Land Transport :
possible?
in effect i put, via vb6 code, in the the two box 00199,Italy and 04023,Italy and press Show i get the perfect distance between zips code.
-
Nov 19th, 2013, 02:29 AM
#16
Re: calculate distance from zip code vs zip code
You can use WebBrowser. Navigate to site and extract from HTML the data you need. Plenty of samples in Code Bank on using WebBrowser to extract data from Html code
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Nov 24th, 2013, 05:50 AM
#17
Re: calculate distance from zip code vs zip code
Originally Posted by dilettante
However web scraping is never legal unless terms of use or a written agreement give you permission.
That is not completely true.
If the terms and conditions of a website states that you shouldn't use web scrapping then yes it is not legal. If the terms and conditions do not say "anything" about "scrapping" then it is perfectly legal. I do it very frequently for my clients.
EDIT:
Interesting Read:
http://www.distilnetworks.com/is-web...he-word-is-is/
http://en.wikipedia.org/wiki/Web_scraping (See Legal issues)
Last edited by Siddharth Rout; Nov 24th, 2013 at 05:56 AM.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|