2015-02-11 15 views
1

我們需要顯示在時間。我們兩個定位經度和緯度有兩個組緯度和經度這樣如何放置兩個位於經度和緯度的地方同時在jQuery Mobile的

[{"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的位置也 任何一個可以告訴我在我的code.and什麼錯誤請指引我。

+0

? – Ethaan 2015-02-11 06:55:39

+0

@Ethaan是的,我有10分 – 2015-02-11 07:02:13

回答

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)); 
}); 
要放置2個標記
1

比方說你有這個數組

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

所以如果你運行console.log(markersArray.length),你會得到這樣的輸出

2 

在這一點上,我們好嗎?

現在好了,我們需要有一個for loopfunctions創建基於該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()函數(或任何功能名稱使用給init地圖)這裏面的代碼。

createMarkers() < - 這樣

+0

感謝reply.I是新這個你能解釋我在哪裏需要將你的代碼在我的代碼 – 2015-02-11 07:05:44

+0

你可以請上傳您的jsfiddle裏面的代碼,但在看簡歷,你有一個嵌套的對象'{ 「經度」: 「17.4954」, 「Lattitude」: 「78.2960」, 「用戶ID」:3},{ 「經度」: 「17.3616」, 「Lattitude」: 「78.4747」, 「用戶ID」:3} ' 所以只需創建一個函數來創建基於該嵌套的對象上的標記,但就像我說的,請上傳代碼裏面[的jsfiddle(jsfiddle.net) – Ethaan 2015-02-11 07:07:00

+0

檢查答案更新我試圖解釋它 – Ethaan 2015-02-11 07:14:23

相關問題