javascript - 属性 <position> 的值无效

标签 javascript json perl google-maps google-maps-api-3

我正在尝试结合使用 perl 和 Javascript,使用 Google Maps API v3 将 IP 地址纬度/经度位置映射到 Google map 。我得到的是在标记中,我收到错误:Uncaught Error: Invalid value for property <position>: 43.073052,-89.40123

我的代码是:

    #!/usr/bin/perl -w
    use CGI qw(:standard);
    use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
    use CGI::Session ( '-ip_match');
    use SOAP::Lite;
    use lib '/home/schreiber/perl5/lib/perl5';
    use JSON qw(encode_json);

    $session = CGI::Session->load();
    $q = new CGI;
    my $soap = SOAP::Lite
        -> uri('http://v1.fraudlabs.com/')
        -> proxy('http://v1.fraudlabs.com/ip2locationwebservice.asmx')
        -> on_action(sub { join "/", "http://v1.fraudlabs.com", $_[1] });

    $license = "<removed>";

    #if($session->is_expired) {
    #  print $q->header(-cache_control=>"no-cache, no-store, must-revalidate");
    #  print "Your session has expired.  Please login again.";
    #  print "<br/><a href=\"../login.html\">Login</a>";
    #} elsif($session->is_empty) {
    #  print $q->header(-cache_control=>"no-cache, no-store, must-revalidate");
    #  print "You have not logged in";
    #} else {
      print $q->header(-cache_control=>"no-cache, no-store, must-revalidate");

      open (IPF, "/home/access_log");
      @incomingarray=<IPF>;

      $i = 0;
      foreach $pair(@incomingarray) {
        ($ip, $rest) = split(/ - - /, $pair);
        $incomingarray[$i] = $ip;

        chomp $ip;
        $i++;
      }

      close (IPF);

      my %hash = map { $_, 1 } @incomingarray;
      my @distinctIP = keys %hash;

      $j = 0;
      my @iplocation;
      foreach (@distinctIP) {
        my $method = SOAP::Data->name('IP2Location')->attr({xmlns => 
        'http://v1.fraudlabs.com/' });

        my @params = SOAP::Data->name('inputdata' => \SOAP::Data->value(
        SOAP::Data->name(IP=>$_),
        SOAP::Data->name(LICENSE=>$license)
        ));

        my $result = $soap->call($method => @params);
        $lat = $result->valueof('//LATITUDE');
        $long = $result->valueof('//LONGITUDE');

        push(@iplocation, "$lat,$long");
      }

      my $json = encode_json(\@iplocation);

    #  print "Content-Type: text/html\n\n";
      print '<html>
      <head>
        <script type="text/javascript">
          function initialize() {
            var myOptions = {
              zoom: 8,
              center: new google.maps.LatLng(44.49, -91.30),
              mapTypeId: google.maps.MapTypeId.ROADMAP
            };

            var map = new google.maps.Map(document.getElementById(\'map_canvas\'),
                myOptions);

            var coord_data = new Array();
            coord_data = '.$json.';

            var marker;
            for (var i = 1; i < coord_data.length; i++) {
               console.log(coord_data[i]);
      var marker = new google.maps.Marker({
        position: new google.maps.LatLng(coord_data[i]),
        map:map
      });

              marker.setMap(map);
            }
          }

          function loadScript() {
            var script = document.createElement(\'script\');
            script.type = \'text/javascript\';
            script.src = \'//maps.googleapis.com/maps/api/js?sensor=false&callback=initialize\';
            document.body.appendChild(script);
          }

          window.onload = loadScript;
        </script>
      </head>

      <body>
        <div id="map_canvas" style="width: 100%; height: 100%"></div>
      </body>
      </html>
      ';
    #}

如有任何帮助,我们将不胜感激。

最佳答案

问题是当你在这里分配位置时——

marker = new google.maps.Marker({
    position:coord_data[i],
    map:map
});

-- 您给它一个原始的 JSON 对象,但它需要一个 google.maps.LatLng 对象。你会想这样分配它——

position: new google.maps.LatLng(coord_data[i].lon,coord_data[i].lat)

另请参阅此处,了解 Gmaps API 用法的一些优秀示例: https://developers.google.com/maps/documentation/javascript/v2/examples/

关于javascript - 属性 <position> 的值无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9755203/

相关文章:

javascript - 为对象计算的 polymer

perl - 如何为文件中存在的每个字符串分配行号

javascript - 从编程检查/取消选中中选取更改的复选框

javascript - 我在 Canvas 中的 mousedown 函数在被调用时没有重绘背景

javascript - jQuery + PHP 动态创建内容

php - Json数组保存在sql中

php - 将 mysql_query 传递到 JSON 文件不起作用

database - 如何在 Perl 中更高效地进行多个数据库查询?

perl - 为什么 'Use of "shift"没有括号是由 Perl 发出的不明确的警告?

javascript - 如何从 iframe 内部调用 Javascript 函数?