javascript - 在 Google Maps API 中显示多个 map 标记

标签 javascript perl google-maps-api-3

在 .cgi 文件中处理一些 perl 代码,循环遍历 IP 地址的哈希列表,然后通过返回 IP 地址的纬度和经度的 Web 服务运行每个 IP 地址,然后它需要显示每个 IP地址作为谷歌地图上的标记。截至目前,我让它遍历哈希列表并打印每个 IP 地址的坐标。我想不通的是如何在谷歌地图上显示多个 map 标记。我目前只是通过将值硬编码到 $latitude 和 $longitude 变量来测试它。我想在 javascript 中需要有某种循环来运行并分配每个坐标,但我不知道如何处理它。

更新:我添加了第一个答案中的代码,并在循环外成功打印了列表。我现在遇到的问题是谷歌地图将不再加载。我已将问题缩小到为纬度和经度变量赋值的 javascript。

unless ($result->fault) {

# Print out the results one by one
my $latitude = "LATITUDE = " . $result->valueof('//LATITUDE') . "\n";
my $longitude = "LONGITUDE = " . $result->valueof('//LONGITUDE') . "\n";

#print "MESSAGE = " . $result->valueof('//MESSAGE') . "\n";


$lats .= $latitude . ',';
$lons .= $longitude . ',';


} else {

print "IP2Location Web Service Failed!\n";
print join ', ',
$result->faultcode,
$result->faultstring,
$result->faultdetail;

}
}

chop $lats;
chop $lons;

#printing here to test if the variable is making it out of the loop
print $lats ;
print $lons ;

print <<ENDHTML;
<html>
  <head>
<title>IP Lookup Map</title>
<meta name="viewport"
    content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta charset="UTF-8">
<style type="text/css">
  html, body, #map_canvas {
    margin: 0;
    padding: 0;
    height: 90%;
    width: 90%;
  }
</style>
<script type="text/javascript"
    src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
//If I comment out the following variables the map will load, not sure what the problem is
    var latitudes = "$lats".split(",");
    var longitudes = "$lons".split(",");

  var map;
  function initialize() {
    var myOptions = {
      zoom: 7,
      center: new google.maps.LatLng(44, -90),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById('map_canvas'),
        myOptions);

    // Creating a marker and positioning it on the map
    for(var i = 0; i < latitudes.length; i++){
        var latitude = latitudes[i];
        var longitude = longitudes[i];
    // Creating a marker and positioning it on the map
        var marker = new google.maps.Marker({
        position: new google.maps.LatLng(latitude,longitude),
        map: map
    });
    }
  }

  google.maps.event.addDomListener(window, 'load', initialize);
</script>

最佳答案

显然您需要保存所有的纬度和经度,而不是仅仅打印它们。我会在你的 unless($result){

之外使用 2 个全局变量
my $lats = '';
my $lons = '';
....
$lats .= $latitude . ',';
$lons .= $longitude . ',';

(您可能需要在循环后使用 chop($lats); chop($lons) 来删除最后一个逗号)然后在您的 javascript 部分:

// create two arrays from that lats and lons string using split()
var latitudes = "$lats".split(',');
var longitudes = "$lons".split(',');
.....
// create map code
....
for(var i = 0; i < latitudes.lenght; i++){
    var latitude = latitudes[i];
    var longitude = longitudes[i];
   // Creating a marker and positioning it on the map
   var marker = new google.maps.Marker({
       position: new google.maps.LatLng(latitude,longitude),
       map: map
   });
}

关于javascript - 在 Google Maps API 中显示多个 map 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9651137/

相关文章:

javascript - 使用cloudflare缓存动态页面

javascript - 是否可以(在用户许可的情况下)以与在扩展程序中相同的方式在网页中使用 JavaScript 执行跨站点请求?

windows - 在 Windows 上动态重定向 child 的标准输出

javascript - 如何欺骗浏览器中的不同位置进行测试?

javascript - 即使没有选择任何单选按钮,Radiobutton .val()也不返回空值

c# - 查找aspx页面中存在的所有控件和子控件id以及相应的ascx用户控件

regex - 正则表达式小写

perl - 查找文件中所有相同的字符串,然后在 perl 中打印值

jquery - Google Map API v3,使用 AJAX 通过 setContent 加载 infoWindow 内容

javascript - 如何仅更改谷歌地图中的某些多边形样式