数组的旧值替换为数组的新值 [英] old value of array replace by new value of array

查看:255
本文介绍了数组的旧值替换为数组的新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是Google地图的onclick.试图创建包含地址的数组.问题是当我警告数组时,它仅显示当前值.
我也尝试了waypts.length,它仅警告1.这意味着当我单击Google Map时,数组值被替换了. 那么,似乎是什么问题呢?

I am trying to do is onclick of google map. Attempting to create array which contains address. The problem is when I alert the array it only displays the current value.
I also tried waypts.length, it alerts only 1. It means array value is replaced when I click on google map. So what seems to be the problem?

代码:

  function initialize() {
  var geocoder = new google.maps.Geocoder();
  var waypts=[];
  var mapOptions = {
   zoom: 11,
   center: new google.maps.LatLng(23.0171240, 72.5330533),
   mapTypeId: google.maps.MapTypeId.ROADMAP
   };
   var map = new google.maps.Map(document.getElementById('map_canvas'),mapOptions);
  google.maps.event.addListener(map, 'click', function(e) {
  placeMarker(e.latLng, map);
  var input=e.latLng;
  var lat = parseFloat(input.lat());
  var lng = parseFloat(input.lng());
  var latlng = new google.maps.LatLng(lat, lng);
  geocoder.geocode({'latLng': latlng}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
       var add=results[0].formatted_address;
       waypts.push({
        location:add,
        stopover:true
       });
       var lngth=waypts.length;
       alert(JSON.stringify(waypts));
    }
  });
          <!--  **************    for route between markers   *******************  -->
  var directionsDisplay;
  var directionsService = new google.maps.DirectionsService();
  //directionsDisplay = new google.maps.DirectionsRenderer();
  directionsDisplay = new google.maps.DirectionsRenderer({
  suppressMarkers: true, //false it if you want a marker from the direction service 
  polylineOptions: {
      strokeColor: 'green', //"black",
      strokeOpacity: 1.0,
      strokeWeight: 3
    }
  });

    var lngth=waypts.length;
    var start = waypts[0][0];//"Bopal, Ahmedabad, Gujarat, India";
    var end = waypts[waypts.length-1][0];//"Nikol, Ahmedabad, Gujarat, India";

    //remove start destination from array
    var a=waypts.shift();
    //remove end destination from array
    waypts.pop();

    var request = {
          origin:start,
          destination:end,
          waypoints:waypts,
          travelMode: google.maps.DirectionsTravelMode.DRIVING
    };

    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
        }
    });
    directionsDisplay.setMap(map);

 });


}
   function placeMarker(position, map) {
  var marker = new google.maps.Marker({
    position: position,
    map: map
  });
  map.panTo(position);
}

google.maps.event.addDomListener(window, 'load', initialize);

谢谢.

推荐答案

我假设它存在于函数中.

I'm assuming this is present inside a function.

该代码的问题可能是将waypts设置为局部变量.

The problem with the code might be that waypts is made a local variable.

如果在其他任何地方引用它,则无法获取该值.

If it is referred anywhere else, the value cannot be obtained.

每次调用函数时都会重新初始化变量,因此存储在数组中的所有先前值都将丢失.

The variable is reinitialized each time the function is called, so any previous values stored in the array will be lost.

waypts设置为全局,然后尝试在函数外部定义变量.

Make waypts global and try by defining the variable outside the function.

这篇关于数组的旧值替换为数组的新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆