为什么我的脚本不起作用?谷歌地图,地理编码器

时间:2015-12-07 04:09:58

标签: javascript google-maps

我有这样的数组:" country,city,street",我想得到一个包含点坐标的数组。 到达界限:

locations.push(results[0].geometry.location);

脚本停止。有谁能告诉我如何解决这个问题?

var addresses = ["Russia, Moscow, Arbet", "Estonia, Tallinn, Uus",...];
function initialize() 
{ 
    geocoder = new google.maps.Geocoder();
    for (var i = 0; i < 10; i++) {
        geocodeAddress(geocoder, map, addresses[i]);
    }
}
var locations = [];
function geocodeAddress(geocoder, resultsMap, address) 
{   
    geocoder.geocode({'address': address}, function(results, status) 
    {
        if (status === google.maps.GeocoderStatus.OK) 
        {
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
        }
        else 
        {
            alert('Geocode was not successful for the following reason: ' +     status);
        }
        locations.push(results[0].geometry.location);
    });
}

谢谢, 伊琳娜

2 个答案:

答案 0 :(得分:0)

您的代码应为:

function geocodeAddress(geocoder, resultsMap, address) 
{   
    geocoder.geocode({'address': address}, function(results, status) 
    {
        if (status === google.maps.GeocoderStatus.OK) 
        {
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
        }
        else 
        {
            alert('Geocode was not successful for the following reason: ' +     status);
        }
        locations.push(results[0].geometry.location);
    });
}

results作为参数传递给您的回调方法,但您尝试从方法外部访问它,但未定义该方法。

答案 1 :(得分:0)

如果google.maps.Geocoder.geocode函数返回状态,那么这些值与"OK"不同,则响应不包含已解析的位置,因此一旦状态为{{1,您可能需要初始化locations数组}}:

google.maps.GeocoderStatus.OK

修改示例:

&#13;
&#13;
if (status === google.maps.GeocoderStatus.OK) 
{
     var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
     });
     locations.push(results[0].geometry.location);
 }   
&#13;
var addresses = ["Russia, Moscow, Arbet", "Estonia, Tallinn, Uus"];
var locations = [];

function initialize() 
{
    var map = new google.maps.Map(document.getElementById('map-canvas'), {
        center: {
            lat: 55.6574391,
            lng: 26.4077234
        },
        zoom: 5,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var geocoder = new google.maps.Geocoder();

    addresses.forEach(function(address) {
        geocodeAddress(geocoder, address,
            function (location) {
                var marker = new google.maps.Marker({
                    map: map,
                    position: location
                });
                locations.push(location);
            },
            function (status) {
                console.log('Geocode error: ' + status);
            });
    });
       
}

function geocodeAddress(geocoder,address, success, error) 
{   
    geocoder.geocode({'address': address}, function(results, status) 
    {
        if (status === google.maps.GeocoderStatus.OK) 
        {
            success(results[0].geometry.location);
        }
        else 
        {
            error(status);
        }
    });
}

google.maps.event.addDomListener(window, 'load', initialize);
&#13;
body {
  margin: 0;
  padding: 0;
}

#map-canvas {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
    height: 600px;
}
&#13;
&#13;
&#13;

更新:示例2

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<link href="style.css" rel="stylesheet" />
<div id="map-canvas"></div>
&#13;
var addresses = ["Russia, Moscow, Arbet", "Estonia, Tallinn, Uus"];
var locations = [];


function initialize() 
{
    var map = new google.maps.Map(document.getElementById('map-canvas'), {
        center: {
            lat: 57.6574391,
            lng: 19.4077234
        },
        zoom: 5,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var geocoder = new google.maps.Geocoder();


    var promises = getGeocodeAddressDeferred(geocoder, addresses);
    $.when.apply($, promises).done(function (location) {

        locations = arguments; // get all locations

        alert(JSON.stringify(locations)); //1.print all locations

        //display markers
        $.each(locations, function (i, location) {
             var marker = new google.maps.Marker({
                    map: map,
                    position: location
              });
        });
    });       
}


function getGeocodeAddressDeferred(geocoder, addreses) {
    var promises = [];
    $.each(addreses, function (i, address) {
        promises.push(geocodeAddress(geocoder, address));
    });
    return promises;
}

function geocodeAddress(geocoder, address) {
    var deferred = $.Deferred();
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status === google.maps.GeocoderStatus.OK) {
            deferred.resolve(results[0].geometry.location);
        }
    });
    return deferred.promise();
}

google.maps.event.addDomListener(window, 'load', initialize);
&#13;
body {
  margin: 0;
  padding: 0;
}

#map-canvas {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
    height: 600px;
}
&#13;
&#13;
&#13;