0

I need to calculate a distance between two places (specified by name of the city, country and postal code) from Java or .NET application using internet maps services such as Google maps or Viamichelin. I don't want it to be a web application and all the google APIs that i've found for java expect that you use Javascript. I would prefer Java but .NET is possible too.

It should work like: I put 2 places in ma app-> application connects to the map service, which calculate distance and send it back to my application where I can work with that.

Do anybody know the easiest (and legal.... they have some crazy terms of use in that APIs) way to do this? Doesnt matter if it is in Java or .NET but it must use Google maps or Viamichelin map server.

Thank you very much for your answers.

Wolf
  • 871
  • 1
  • 7
  • 21

2 Answers2

1

Use this Class in your project to get driving distance between two places You can get Newtonsoft.Json.Linq namespace from https://json.codeplex.com/releases/view/117958

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;

namespace Management
{
    class DeliveryMapInfo
    {
    string origin = @""; //Write the address of the origin here
    public DeliveryMapInfo() { }
    public int getDistanceTo(string destination)
    {
        System.Threading.Thread.Sleep(1000);
        int distance = -1;
        string url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&sensor=false";
        string requesturl = url;string content = fileGetJSON(requesturl);
        JObject _Jobj = JObject.Parse(content);
        try
        {
            distance = (int)_Jobj.SelectToken("routes[0].legs[0].distance.value");
            return distance / 1000;// Distance in KMS
        }
        catch
        {
            return distance / 1000;
        }
    }
    protected string fileGetJSON(string fileName)
    {
        string _sData = string.Empty;
        string me = string.Empty;
        try
        {
            if (fileName.ToLower().IndexOf("http:") > -1)
            {
                System.Net.WebClient wc = new System.Net.WebClient();
                byte[] response = wc.DownloadData(fileName);
                _sData = System.Text.Encoding.ASCII.GetString(response);

            }
            else
            {
                System.IO.StreamReader sr = new System.IO.StreamReader(fileName);
                _sData = sr.ReadToEnd();
                sr.Close();
            }
        }
        catch { _sData = "unable to connect to server "; }
        return _sData;
    }
}

}

A lot of your idea depends upon what api you want to use, ie. Distance Matrix, Directions, Geocoding, etc. Notice: "/directions/" I found directions as most complete api for my use, since it can geocode, find distance, find durations... etc. However the size of directions json file is larger than other json files. Use your judgement.

string url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&sensor=false";

and

distance = (int)_Jobj.SelectToken("routes[0].legs[0].distance.value");

for eg:

The Structure inside Directions json file is:

-Routes

    - Bound
        - NorthEast
        - SouthWest
    - Copyrights
    - Legs
        - Steps
            - duration
            - distance
            - endlocation
            - maneuver
            - htmlinstructions
            - polyline
            - startlocation
            - travelmode
        - Distance
            - text
            - value
        - Duration
            - test
            - value
        - StartAddress
        - EndAddress
        - EndLocation
            - lat
            - lng
        - StartLocation
            - lat
            - lng
    - Overview Polyline
    - Summary
    - Warnings
    - Waypoint Order

- Status
Nilay Vishwakarma
  • 3,105
  • 1
  • 27
  • 48
1

See here

If you mean driving distance as a webservice, Google don't offer it. Straight-line distance is fairly easily calculated and doesn't need a webservice (although you can geocode your two addresses using one).

http://www.movable-type.co.uk/scripts/latlong.html http://code.google.com/apis/maps/documentation/geocoding/index.html

This question's solution may also be helpful

Community
  • 1
  • 1
Sebastian Zaklada
  • 2,308
  • 1
  • 14
  • 25
  • yes I mean driving distance... so there's no way how i can obtain driving distance between two locations?:( – Wolf Mar 16 '11 at 20:07
  • 1
    only if you use the Google Directions API, but then, you must display the response on map.... you can't use it in desktop applications – GoG Mar 16 '11 at 22:14