Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何访问GoogleMapAPI中的变量以在每个函数中使用_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 如何访问GoogleMapAPI中的变量以在每个函数中使用

Javascript 如何访问GoogleMapAPI中的变量以在每个函数中使用,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我在使用谷歌地图搜索的自动完成功能时遇到问题。这就是我正在尝试的 function initialize() { directionsDisplay = new google.maps.DirectionsRenderer(); //add map, the type of map var map = new google.maps.Map(document.getElementById('map-canvas'), { zoom: 6,

我在使用谷歌地图搜索的自动完成功能时遇到问题。这就是我正在尝试的

function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    //add map, the type of map
    var map = new google.maps.Map(document.getElementById('map-canvas'), {
        zoom: 6,
        center: haight,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    autocomplete = new google.maps.places.Autocomplete(document.getElementById('address'));

    google.maps.event.addListener(autocomplete, 'place_changed', function () {
        var place = autocomplete.getPlace();
        var lat = place.geometry.location.lat();
        var lng = place.geometry.location.lng();      
    });

    alert(lat);
}
如果我试图从addlistner函数中生成警报,它将不起作用,但它在addlistner中起作用。有没有办法从这个addlistner获取lat,lng

我还可以在运行时在initialize函数之外使用变量lat,lng吗

编辑

var markers = new google.maps.Marker({
  map: map,
  position: new google.maps.LatLng(lat, lng),
  title: 'Point of origin'
});

// Add circle overlay and bind to marker
var circle = new google.maps.Circle({
  map: map,
  radius: 1000*10,    // metres
  fillColor: '#AA0000'
});

circle.bindTo('center', markers, 'position');

我想使用lat,lng变量创建标记,以生成特定纬度和经度的圆。

尝试以下操作:

var map;

function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    //add map, the type of map
    map = new google.maps.Map(document.getElementById('map-canvas'), {
        zoom: 6,
        center: haight,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    autocomplete = new google.maps.places.Autocomplete(document.getElementById('address'));

    google.maps.event.addListener(autocomplete, 'place_changed', function() {
        var place = autocomplete.getPlace();
        var lat = place.geometry.location.lat();
        var lng = place.geometry.location.lng();

        markerAndCircle(lat, lng);
    });
}

function markerAndCircle(lat, lng) {

    var marker = new google.maps.Marker({
        map: map,
        position: new google.maps.LatLng(lat, lng),
        title: 'Point of origin'
    });

    // Add circle overlay and bind to marker
    var circle = new google.maps.Circle({
        map: map,
        center: new google.maps.LatLng(lat, lng),
        radius: 1000 * 10, // metres
        fillColor: '#AA0000'
    });
}
这是未经测试的。如果它不起作用,请观察javascript控制台中的错误并在此处报告


注意:
map
变量现在在
initialize
函数之外声明,因为我们需要它在
markerAndCircle
函数中声明。

可以。从侦听器中声明变量。但是要小心。。。这是一个异步函数。感谢您的回复,它仍然表示未定义。您想做什么?如果您需要帮助,这可能是有用的信息。用您所做的(代码)和您正在尝试做的事情更新您的问题。@MrUpsidown很抱歉给您带来不便,我已经编辑了问题。您只需在侦听器中创建标记/圆圈即可。。。