views:

6738

answers:

9

I am working on a project which requires a server side access to google map api. i want to calculate distance (actual distance, not straight line). google map api supports javascript and not asp.net. please give suggestions ...!

A: 

You can calculate the distance of two geographical coordinates (latitude, longitude) using the Great-circle distance algorithm.

Here you can find some other formulas for distance calculation.

CMS
A: 

You could try creating a HttpWebRequest "faking" what you'd do in a browser, that is assuming all the parameters for google maps can be passed in the querystring. Then you could just read the response stream.

Antony Scott
+2  A: 

The Google API allows you to Geocode via a server side call:

http://code.google.com/apis/maps/documentation/services.html#Geocoding_Direct

This would allow you to get the longitude and latitude of the locations. You can then cache these and use them to calculate distance using the techniques CMS suggests.

You will need to be careful of the Google T&C's though as you are only allowed to store the geocoding data for use on a Google map which is publicly available.

You would probably also run into limitations on the number of requests you could make from a single IP.

However I think what you mean by non-straight line distance is distance taking into account roads and one way streets etc.

If this is the case I think a commercial service is your only option. Although theoretically you could do it all via screen scraping, I'm almost certain that this would break Google's T&C's.

The simplest solution would probably be just to embed a Google map on a page of your application and let the user calculate the distance. You could pre-fill the to and from fields if required.

Again if this is for an internal app i.e. Not publicly available "my understanding" of the Google T&C's would forbid this.

Tim Saunders
+1  A: 

Use something like firebug or fiddler to look at the requests that are being sent to Google from javascript you should then beable to build the request using that information and an HTTPWebRequest in .net and retrieve the same information.

HTH

OneSHOT
A: 

Well, you've pretty much identified the key issue, the Gmaps API is a browser resident javascript API and there's not much getting around that. Most of the API is executed in the browser so there's not much network traffic to spy on.

As tsaunders mentions there is a geocoding API call that is restfully accessible, but it only does reverse/geocoding and if you have lat/lng's already you can use the calculations the rms suggested, but they are as tsaunders points out 'as the crow flies' distance.

If indeed you are looking for road taken distance, the API does do routing but you are back in the browser to get the start/end points from the user.

Perhaps you can be a little more specific about what you are trying to do and why you feel this requires you to to access the API from your server. My application for instance has features that gather information from the user and sends requests back to my server to work on, some of that data are processed by the Gmaps API first.

jottos
+2  A: 

Hi - you specified google maps in your question - but have you looked at Virtual Earth? Specifically this routing with Virtual Earth Web Service example sounds exactly like what you want:

  • server-side access (just Add Service Reference inside visual studio)
  • actual distance (not straight line) since it is using a route

The concerns raised by others about T&Cs for 'internal/intranet use' apply to VE as well as Google I think - you'll have to read up about whether your application needs licensing or not.

p.s. if you did just want to calculate straight-line distance, I have instructions using SQL Server 2008; which also links to some straight c# code that does it too.

CraigD
A: 

I have a similar problem.

@Jottos

In my case, I'm sending geo-coordinates from a mobile device, so I wanted to make all distance calculations service side rather than having the device make 10-20 slow requests over 3G.

This Virtual Earth service looks interesting, but I can't find any good information on how to properly configure it in Visual Studio.

Steve Willard
A: 

If I were to use a API platform, I certinaly would not use Google as the free one does not include advances Geocoding menaing the accuracy is poor. There is also no sla , support or rights of service.

The directions are poor, the coverage for Ireland and Geocoding is almost childlike and the privacy stinks. No professional business would use a google mapping solution.

They copy everyone else's idea, say they are there own and get loads of press (they only added tube stations in 2006) an dcyclc lanes (2010), viamichelin added these 2006 and Traffic in 2009 !

Any agency or developers looking for an API should stick to Bing or ViaMichelin for better customisation and user experience which is killer !

Johny Adams
A: 

Well the virtual Earth API seems to be intresting. But is it freely available ?

Majid