English 中文(简体)
Google s GeoCode Convert City, STATE into Latitude Longitude
原标题:

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);
}
最佳回答

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.

问题回答

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.

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;
                }
            }
        }
    }
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签