Google map on ajax success



我在上有以下 ajax request >选择下拉更改,它只是从控制器中获取记录,循环循环每个人并获得latitude |longitude将其推到数组

然后,在同一AJAX成功中,我将LAT和LNG数组传递给Google Map。

但地图没有显示。

$(document).ready(function() {
   $('.selectCity').change(function() {
    var city = $(this).val();
    $.ajax({
      type: 'GET',
      url: '/riders/location/track',
      data: {
        'city': city
      },
      success: function(data) {
        var lat = [];
        var lng = [];
        //Get Locations and push it to lat and lng
        $.each(data, function(index, value) {
          $.each(value, function(index1, value1) {
            console.log(value1.rider_location.lat);
            lat.push(value1.rider_location.lat);
            lng.push(value1.rider_location.lng);
          });
        });
        //Google Map
        google.maps.event.addDomListener(window, 'load', init);
        function init() {
          var locations = [
            ['Rider', lat, lng]
          ];
          var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 10,
            center: new google.maps.LatLng(lat, lng),
            mapTypeId: google.maps.MapTypeId.ROADMAP
          });
          var infowindow = new google.maps.InfoWindow();
          var marker, i;
          for (i = 0; i < locations.length; i++) {
            marker = new google.maps.Marker({
              position: new google.maps.LatLng(locations[i][1], locations[i][2]),
              map: map
            });
            google.maps.event.addListener(marker, 'click', (function(marker, i) {
              return function() {
                infowindow.setContent(locations[i][0]);
                infowindow.open(map, marker);
              }
            })(marker, i));
          }
        }
      }
    });
  });
});

另外,请建议最好的做法。

当然,我可以在上面发表评论作为答案。

您也可以使用callback-参数(HTML)来收听脚本-URL中的" Google-Maps-Ready" - 事件:

<script type="text/javascript"
  src="http://maps.googleapis.com/maps/api/js?libraries=geometry,places&callback=initialize">
</script>

JS:

// In this way you have to define a function called initialize which should be defined globally otherwise it can not be found by the google-library.
// unfortunately this map-variable is defined globally here but you can also wrap the whole code below by using an IIFE.
var map; 
function initialize() {
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 10,
    // you might set a center here or wait untill you have got some markers fetched via ajax, you can then use the first/last or some other marker respecetive it's position(lat,long) to set as "starting point"
    //center: {lat: LAT, lng: LONG }
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });
}
// Although I have no access to your website to test this code below, it might be done in this way:
$(document).ready(function () {
  $('.selectCity').change(function () {
    var city = $(this).val();
    $.ajax({
      type: 'GET',
      url: '/riders/location/track',
      data: {
        'city': city
      },
      success: function (data) {
        var positions = [];
        //Get Locations and push it to lat and lng
        // you can also use just one array to insert all locations and "labels" into
        $.each(data, function (index, value) {
          $.each(value, function (index1, value1) {
            console.log(value1.rider_location.lat);
            positions.push({
              lat: value1.rider_location.lat,
              lng: value1.rider_location.lng,
              content: 'Rider' // do you get some text with each location?
            });
          });
        });
        // set "starting point" afterwards
        map.setCenter({
          lat: positions[0].lat,
          lng: positions[0].lng
        });
        var infowindow = new google.maps.InfoWindow();
        var marker,
        i;
        for (i = 0; i < positions.length; i++) {
          marker = new google.maps.Marker({
            position: new google.maps.LatLng(positions[i].lat, positions[i].lng),
            map: map
          });
          google.maps.event.addListener(marker, 'click', (function (marker, i) {
            return function () {
              infowindow.setContent(positions[i].content);
              infowindow.open(map, marker);
            }
          }) (marker, i));
        }
      }
    });
  });
});

希望它有帮助!

相关内容

  • 没有找到相关文章

最新更新