如何在 jquery 移动设备中同时放置两个经纬度位置
how to place two located latitude and longitude place same time in jquery mobile
我们需要在一个地方显示两个位置的经纬度 time.We 有两组这样的经纬度
[{"Longitude":"17.4954","Lattitude":"78.2960","UserID":3},{"Longitude":"17.3616","Lattitude":"78.4747","UserID":3}]
我的问题是它的 pin 仅显示最后的经度和纬度我们对此进行编码
$(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 请指导我。
假设你有这个数组
var markersArray = [{"Longitude":"17.4954","Lattitude":"78.2960","UserID":3},{"Longitude":"17.3616","Lattitude":"78.4747","UserID":3}]
所以如果你 运行 console.log(markersArray.length)
,你将得到这个输出
2
此时我们还好吗?
现在我们需要 functions with a 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()
<-- 像这样
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));
});
我们需要在一个地方显示两个位置的经纬度 time.We 有两组这样的经纬度
[{"Longitude":"17.4954","Lattitude":"78.2960","UserID":3},{"Longitude":"17.3616","Lattitude":"78.4747","UserID":3}]
我的问题是它的 pin 仅显示最后的经度和纬度我们对此进行编码
$(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 请指导我。
假设你有这个数组
var markersArray = [{"Longitude":"17.4954","Lattitude":"78.2960","UserID":3},{"Longitude":"17.3616","Lattitude":"78.4747","UserID":3}]
所以如果你 运行 console.log(markersArray.length)
,你将得到这个输出
2
此时我们还好吗?
现在我们需要 functions with a 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()
<-- 像这样
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));
});