views:

71

answers:

3

The following code works asynchronously. I only have it set up to convert a City, State into latitude and longitude. That value is then alerted.

How can I write a function that actually returns these values?

var map;
var geocoder;

function initialize() {

  geocoder = new GClientGeocoder();

}

// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
  //map.clearOverlays();
  if (!response || response.Status.code != 200) {
    alert("Sorry, we were unable to geocode that address");
  } else {
    place = response.Placemark[0];

    latitude = place.Point.coordinates[0];
    longitude = place.Point.coordinates[1];

    alert("Latitude " + latitude + " Longitude" + longitude);

  }
}

function showLocation(address) {
  geocoder.getLocations(address, addAddressToMap);
}
+1  A: 

Return those values from the callback won't doing anything. If you want to store those values somewhere just do it in the callback. You could do it in your current code if you just declare latitude and longitude at the top where declare the map and geocoder vars.

BrennaSoft
That's exactly what I tried first, but it's being done at a form submission and the values aren't set in time. I might need to get crafty with ajax
pws5068
@pws5068: Feel free to post another question, showing the problem you are having when geocoding before form submission... We'll give a look at it :)
Daniel Vassallo
+1  A: 

No, as far as I know, the Google Maps API does not support synchronous geocoding requests. However, in general you shouldn't be using synchronous requests. This is because the browser UI would block until it receives the response, and that would make a terrible user experience.

Daniel Vassallo
Thank you, this is the answer I was afraid of. I will try to come up with an ajax approach to storing these values.
pws5068
A: 

For this solution, you will need to download Sharmil Desai's "A .NET API for the Google Maps Geocoder" from CodeProject (open license), located here: http://www.codeproject.com/KB/custom-controls/GMapGeocoder.aspx.

Implement the following code, inserting the required city, state, or street address, and the GoogleMapsAPI will return your GeoCoded results using the GMapGeocoder's utility method, 'Util.Geocode'.

Happy Coding!

using System;
using System.Collections.Generic;
using System.Linq;
using GMapGeocoder;
namespace GeoCodeAddresses
{
    class Program
    {
        static void Main(string[] args)
        {
            string city = "Carmel";
            string state = "Indiana";
            string GMapsAPIkey = 
                System.Configuration.ConfigurationSettings.AppSettings["GoogleMapsApiKey"].ToString();

                GMapGeocoder.Containers.Results results =
                    GMapGeocoder.Util.Geocode(
                    string.Format("\"{1}, {2}\"", city, state), GMapsAPIkey);

                switch (results.StatusCode)
                {
                    case StatusCodeOptions.Success:
                        GMapGeocoder.Containers.USAddress match1 = results.Addresses[0];
                        //city = match1.City;
                        //state = match1.StateCode;
                        double lat = match1.Coordinates.Latitude;
                        double lon = match1.Coordinates.Longitude;
                        Console.WriteLine("Latitude: {0}, Longitude: {1}", lat, lon);
                        break;
                    case StatusCodeOptions.BadRequest:
                        break;
                    case StatusCodeOptions.ServerError:
                        break;
                    case StatusCodeOptions.MissingQueryOrAddress:
                        break;
                    case StatusCodeOptions.UnknownAddress:
                        break;
                    case StatusCodeOptions.UnavailableAddress:
                        break;
                    case StatusCodeOptions.UnknownDirections:
                        break;
                    case StatusCodeOptions.BadKey:
                        break;
                    case StatusCodeOptions.TooManyQueries:
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
MAbraham1