[英]Can't show more than one geolocations using google map api in one page
I use googleapi to show geolocation of multiple places in one page but it's not possible.我使用googleapi在一页中显示多个地方的地理位置,但这是不可能的。
I initialize a variable to count the maps:我初始化一个变量来计算地图:
var countmaps=1;
I do the following in a loop(for each map i want to show):我在循环中执行以下操作(对于我要显示的每张地图):
First of all i get longitude and latitude from an xml file.首先,我从 xml 文件中获取经度和纬度。 This is working:这是有效的:
var lat= latitude[0].childNodes[0].nodeValue;
var lon= longitude[0].childNodes[0].nodeValue;
After this i create a new paragraph element for holding each map:在此之后,我创建了一个新的段落元素来保存每个地图:
var newmap=document.createElement('p');
Id of the element is map1 for the first map, map2 for the second etc.元素的 ID 是第一张地图的 map1,第二张地图的 map2 等等。
newmap.id="maps" + countmaps;
Then, i save the id in a variable:然后,我将 id 保存在一个变量中:
var getid= "maps" + countmaps;
I append the new element to a div called section:我将新元素附加到名为 section 的 div 中:
section.appendChild(newmap);
So far everything is working fine.到目前为止,一切正常。 Then i define the functions for showing the map.然后我定义了显示地图的函数。 I took them from w3schools :我从w3schools拿走了它们:
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition,showError);
}
else{x.innerHTML="Geolocation is not supported by this browser.";//not important
}
}
function showPosition()
{
var latlon=lat+","+lon;
var img_url="http://maps.googleapis.com/maps/api/staticmap?center="
+latlon+"&zoom=14&size=400x300&sensor=false";
//i get element with id map1 in the first loop, map2 in the second etc in the below
document.getElementById(getid).innerHTML="<img src='"+img_url+"'>";
}
function showError(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED:
document.getElementById(getid).innerHTML="MAP Not Available"
break;
case error.POSITION_UNAVAILABLE:
document.getElementById(getid).innerHTML="MAP Not Available"
break;
case error.TIMEOUT:
document.getElementById(getid).innerHTML="MAP Not Available"
break;
case error.UNKNOWN_ERROR:
document.getElementById(getid).innerHTML="MAP Not Available"
break;
}
}
Finally, i call getlocation function and increase the counter for my element ids.最后,我调用 getlocation 函数并增加元素 ID 的计数器。
getLocation();
countmaps++;
In most of the cases i only got the last map.在大多数情况下,我只拿到了最后一张地图。 Can you explain this behavior?你能解释一下这种行为吗?
您不需要多次调用地理定位服务,每次都会返回相同的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.