如何在jquery mobile中同时放置两个位于纬度和经度的位置

时间:2015-02-11 06:53:50

标签: javascript google-maps cordova jquery-mobile geolocation

我们需要一次显示两个位置纬度和经度。我们有两组经度和纬度

[{"Longitude":"17.4954","Lattitude":"78.2960","UserID":3},{"Longitude":"17.3616","Lattitude":"78.4747","UserID":3}]

我的问题是它的图钉仅显示经度和纬度的最后一次我们编码

$(all).each(function () {
    alert(this.Longitude);
    alert(this.Lattitude);


    debugger;
     directionsDisplay = new google.maps.DirectionsRenderer();
    var mapCenter = new google.maps.LatLng(this.Longitude, this.Lattitude);

    debugger;
    var myOptions = {
        zoom:7,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: mapCenter


    }

    debugger;
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
  //  directionsDisplay.setPanel(document.getElementById("directions"));  

// marker will be displayed on the lat long position
var marker = new google.maps.Marker({

position:mapCenter,
map: map
});
});

所有变量都使数据动态化,这意味着它也有一段时间来到10位置 任何人都告诉我我的代码有什么问题。请指导我。

2 个答案:

答案 0 :(得分:1)

让我们说你有这个数组

var markersArray = [{"Longitude":"17.4954","Lattitude":"78.2960","UserID":3},{"Longitude":"17.3616","Lattitude":"78.4747","UserID":3}]

因此,如果您运行console.log(markersArray.length),您将获得此输出

2

此时我们还可以吗?

现在我们需要一个functions for loop来创建基于markersArray的标记

这样的功能。

function createMarkers(){
  for(var i = 0 ; i <markersArray.length ; i++) {
      lat = markersArray[i].Lattitude;
      long = markersArray[i].longitude;
      var marker = new google.maps.Marker({
        position: new google.maps.LatLng(lat, long),
        map: map,
        icon: 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png'
      });
    }
}

说明我需要在代码中放置代码

您需要将此代码放在initializeMap()函数(或用于初始化地图的任何函数名称)中。

createMarkers()&lt; - 喜欢这个

答案 1 :(得分:0)

<强> HTML

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<div id="map_canvas" style="width:100%;height:100%; position:absolute;"></div> 

<强> JAVASCRIPT

var all=[{"Longitude":"17.3616","Lattitude":"78.4747","UserID":3},{"Longitude":"17.4954","Lattitude":"78.2960","UserID":3}];
var map;
 var infowindow = new google.maps.InfoWindow();
$(all).each(function (key, value) {

    if(key == 0){
        map = new google.maps.Map(document.getElementById('map_canvas'), {
          zoom: 6,
          center: new google.maps.LatLng(this.Lattitude, this.Longitude),
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });
    }       

   var marker = new google.maps.Marker({
        position: new google.maps.LatLng(this.Lattitude, this.Longitude),
        map: map
   });

   var content = this.Lattitude+", "+this.Longitude;
   google.maps.event.addListener(marker, 'click', (function(marker, key) {
        return function() {
          infowindow.setContent(content);
          infowindow.open(map, marker);
        }
   })(marker, key));
});