2012-03-18 88 views
6

我试图将perl和Javascript结合使用Google Maps API v3将IP地址纬度/长度位置映射到Google Map。我正在找回在标记,我得到的错误:Uncaught Error: Invalid value for property <position>: 43.073052,-89.40123属性值无效<position>

我的代码是:

#!/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> 
     '; 
    #} 

任何援助将不胜感激。

回答

10

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

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

- 你给它一个生JSON对象,但该公司预计google.maps.LatLng物件。您将要分配给它像this--

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

看到这里也为Gmaps API使用的一些很好的样本: https://developers.google.com/maps/documentation/javascript/v2/examples/

+0

谢谢。它不再给我错误,但它也不会将标记添加到地图。有任何想法吗? – Kruug 2012-03-18 02:40:03

+0

我认为这只是检查你的语法现在的问题(我怀疑这个问题只是Javascript语法)。由于我不熟练使用Perl,因此我无法确切地说出发生了什么。作为测试,我将HTML复制到一个文本文件中,并将其加载到Chrome中,并使用Firebug进行测试。在我更改了两行代码后,它使用了标记: coord_data = [{lat:44.49,lng:-91}]; position:new google.maps.LatLng(coord_data [i] .lat,coord_data [i] .lng), – McGarnagle 2012-03-18 03:18:23