Как геокодировать адрес в лат / лонг с помощью карт Google - PullRequest
3 голосов
/ 21 сентября 2011

Я хочу иметь возможность нанести несколько компаний на карту Google и понять, что мне нужно геокодировать их.

У меня также есть код под несколькими маркерами этого графика на карте.

Как я могу геокодировать несколько адресов компаний (используя следующий адрес в качестве первого примера) и включить их в текущий имеющийся у меня код?

Мне действительно нужна чья-то помощь, поскольку я не могу понять документацию Google, так кака также с тем, что у меня уже есть.

1 Ответ

8 голосов
/ 21 сентября 2011

вы можете использовать геокодирование Google для получения координат ваших почтовых индексов

РЕДАКТИРОВАТЬ: Мне не очень нравится, когда вы меняете смысл вопроса таким образом, но все в порядке.Пожалуйста, попробуйте вот так (не проверено):

// Creating an array that will contain the coordinates 
// for New York, San Francisco, and Seattle
var places = [];

// Adding a LatLng object for each city
//places.push(new google.maps.LatLng(40.756, -73.986));
//places.push(new google.maps.LatLng(37.775, -122.419));
//places.push(new google.maps.LatLng(47.620, -122.347));
//places.push(new google.maps.LatLng(-22.933, -43.184));
var result;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "http://maps.googleapis.com/maps/api/geocode/json?address=your+code&sensor=false",true);
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
       result = eval('(' + xmlhttp.responseText + ')');
if (result.status == "OK") {
var location = new google.maps.LatLng(result.results[0].geometry.location.lat, result.results[0].geometry.location.lng);
places.push(location);

это, вероятно, должно работать, несмотря на возможные незначительные ошибки.

РЕДАКТИРОВАТЬ2: Я только что нашел более простое решение :

// Creating an array that will contain the coordinates 
// for New York, San Francisco, and Seattle
var places = [];

// Adding a LatLng object for each city
//places.push(new google.maps.LatLng(40.756, -73.986));
//places.push(new google.maps.LatLng(37.775, -122.419));
//places.push(new google.maps.LatLng(47.620, -122.347));
//places.push(new google.maps.LatLng(-22.933, -43.184));
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': "your+code"}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    map.setCenter(results[0].geometry.location);
    //var marker = new google.maps.Marker({
        //map: map,
        //position: results[0].geometry.location
    //});
    places.push(results[0].geometry.location);
  } else {
    alert("Geocode was not successful for the following reason: " + status);
  }
});
...