属性<position> </position>的值无效

时间:2012-03-18 01:10:38

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

我正在尝试将perl和Javascript结合使用Google Maps API v3将IP地址Lat / Long位置映射到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>
      ';
    #}

非常感谢任何协助。

1 个答案:

答案 0 :(得分:10)

问题是当你在这里指定位置时 -

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/