<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&region=zh-CN"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
  </head>
<script type="text/javascript">
var  massage=null;
function userip(ip){
$.getScript("http://whois.pconline.com.cn/jsFunction.jsp?callback=jsShow&ip='"+ip+"'");  
}
function jsShow(address){
var geocoder = new google.maps.Geocoder();
    if(geocoder){
    geocoder.geocode({'address': address }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
    var GeoCode = ((results[0].geometry.location).toString().replace(/[()]/g, '')).split(",",2);
var lat = parseFloat(GeoCode[0]);
var lng = parseFloat(GeoCode[1]);
var result=lat+','+lng;
massage=result;
return result;
}
})
}
}function (){
var ip=document.getElementById('info').value;
 userip(ip); 
alert('最后的结果为:'+massage);
}</script>
 <body>
输入ip地址:<input id="info" type="text" >
<input type="button" value="查询 " onclick="()">
 </body>
</html>
这是一段通过ip结果获取物理地址,又通过google定位搜索获得经纬度的代码
问题:第一次得不到结果,但是第二次点查询的时候就能得到了。不知道是时间太短导致的额?还是什么原因。有什么好的解决办法,吗?真心求教!

解决方案 »

  1.   

    那个接口有问题,我帮你换了一个。效果一样的棒
    既然用了jquery,就不要再使用:document.getElementById('info').value;.....<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
     <head>
      <title> New Document </title>
      <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
      <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&region=zh-CN"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
      </head>
    <script type="text/javascript">
    var  massage=null;
    function userip(ip){     
        $.getScript("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip="+ip,
    function(){
     var prov=remote_ip_info["province"];
     var city=remote_ip_info["city"];
     var thisAddess=prov+city
     jsShow(thisAddess)
    }
    ); 
    }function jsShow(address){
        var geocoder = new google.maps.Geocoder();
         if(geocoder){
             geocoder.geocode({'address': address }, function(results, status) {
                 if (status == google.maps.GeocoderStatus.OK) {
                     var GeoCode = ((results[0].geometry.location).toString().replace(/[()]/g, '')).split(",",2);
                    var lat = parseFloat(GeoCode[0]);
                    var lng = parseFloat(GeoCode[1]);
                    var result=lat+','+lng;                        
                   alert('最后的结果为:'+result);
                    }
                })
            }
    }
     
    function (){
    var ip=$('#info').val();
    userip(ip);  
    }
     
    </script>
     <body>
        输入ip地址:<input id="info" type="text">
            <input type="button" value="查询 " onclick="()">
     </body>
    </html>
      

  2.   


    我的到的结果要在这个函数中用。就是怎么运行下来,我在这个函数中才能得到result的值。不能在回调函数中用。
      

  3.   

    要把你使用message的东西放在回调函数里面,大家不是都已经告诉你了么?
    看3楼代码,把使用message的地方,放在jsShow函数里面
      

  4.   

    估计楼主并没有理解大家的回复,对代码还不理解透彻?<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
     <head>
      <title> New Document </title>
      <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
      <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&region=zh-CN"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
      </head>
    <script type="text/javascript">
    var  massage=null;
    function userip(ip){       
        $.getScript("http://whois.pconline.com.cn/jsFunction.jsp?callback=jsShow&ip='"+ip+"'");   
    }
    function jsShow(address){//异步回调函数
        var geocoder = new google.maps.Geocoder();
         if(geocoder){
             geocoder.geocode({'address': address }, function(results, status) {
                 if (status == google.maps.GeocoderStatus.OK) { //如果状态为ok,应该是指得到服务器的响应
                     var GeoCode = ((results[0].geometry.location).toString().replace(/[()]/g, '')).split(",",2);
                    var lat = parseFloat(GeoCode[0]);
                    var lng = parseFloat(GeoCode[1]);
                    var result=lat+','+lng;                        
                    massage=result;
                    //将结果赋值到全局变量message中,然后你的message才可以被使用。
                    alert(message);//这个时候你才可以使用message!
                    return result;
                    }
                })
            }
    }
     
    function (){
    var ip=document.getElementById('info').value;
     userip(ip); 
    //alert('最后的结果为:'+massage); //说明,查询ip请求发送后,服务器还没有给你反馈之前,你的message变量是空的,你要在这个时候用message,是肯定不对的。即时你延迟一段时间后访问message,也不能保证在什么时间肯定能得到message。异步回调函数的作用就在于此!
     
     
    }
     
    </script>
     <body>
        输入ip地址:<input id="info" type="text" >
            <input type="button" value="查询 " onclick="()">
     </body>
    </html>
      

  5.   


    我明白你的意思。就是message还没东西的时候,就要用message。肯定是没东西的。我的意思是怎么才能解决这个问题,因为获取经纬度要在地图上标注,每个标注点颜色也不同是要判断的,传进去的ip地址是一个对象中取出来的,这个对象中有2个ip地址,肯定就对应的2组经纬度。所以不能在回调函数中去操作标注地图
      

  6.   


    我试过放到jsShow里肯定能得到。但是放进去就达不到需求了
     是2组经纬度,标注到地图上是2个不同颜色的点
      

  7.   


    当时也想到用延迟了,估计是够欠。延迟一次1秒。要取1组(2个)经纬度下来就延迟了2秒。异步的时候要获取时时数据,诶。扯淡的要死。实在不行看看websocket咋样吧。
      

  8.   

    你看这样行不?<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
     <head>
      <title> New Document </title>
      <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
      <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&region=zh-CN"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
      </head>
    <script type="text/javascript">
    var message;
    function userip(ip){     
        $.getScript("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip="+ip,
            function(){
                 var prov=remote_ip_info["province"];
                 var city=remote_ip_info["city"];
                 var thisAddess=prov+city;
     jsShow(thisAddess)  
            }
        );     
    }
     
    function jsShow(address){
        var geocoder = new google.maps.Geocoder();
         if(geocoder){
             geocoder.geocode({'address': address }, function(results, status) {
                 if (status == google.maps.GeocoderStatus.OK) {
                     var GeoCode = ((results[0].geometry.location).toString().replace(/[()]/g, '')).split(",",2);
                    var lat = parseFloat(GeoCode[0]);
                    var lng = parseFloat(GeoCode[1]);
                    var result=lat+','+lng;
    (result);                   
                    }
                })
            }
    }
      
    function (re){
    if(re=="" || re==null) {
       var ip=$('#info').val();
          ccc=userip(ip); 
    }else{
    alert(re)
    }}
      
    </script>
     <body>
        输入ip地址:<input id="info" type="text" value="61.188.38.99">
            <input type="button" value="查询 " onclick="()">
     </body>
    </html>
      

  9.   

    别纠结了,试一下:<script type="text/javascript">
        function jsShow(address){
            var geocoder = new google.maps.Geocoder();
            if(geocoder){
                geocoder.geocode({'address': address }, function(results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        var GeoCode = ((results[0].geometry.location).toString().replace(/[()]/g, '')).split(",",2);
                        var lat = parseFloat(GeoCode[0]);
                        var lng = parseFloat(GeoCode[1]);
                        var result=lat+','+lng;
                        alert('最后的结果为:'+result);
                        return result;
                    }
                })
            }
        }
        function (){
            var ip=document.getElementById('info').value;
            $.getScript("http://whois.pconline.com.cn/jsFunction.jsp?callback=jsShow&ip='"+ip+"'");
        }
    </script>
      

  10.   

    如果只是2个点的话,要不就这样
    坐2个全局变量:msg1,msg2
    参照15楼的方式,在回调函数里面调用处理函数process()
    处理函数函数process如下
    var process = function() {
       if(msg1 == null || msg2 == null) {
           return;
       } else {
           //真正的处理逻辑
           ...
           //如果处理结束后不使用,就把这两个值赋值为null
           msg1 = null;msg2 = null;
           //如果这个处理结束后还要使用,就看你自己的业务逻辑了。这两个值什么时候重新赋值为null比较讲究。
       }
    }
      

  11.   

    数据库不清楚。
    但你的js结构本身有问题,上面已经帮你改过了。关键是你的$.getScript()文件调用jsShow()函数,所以没必须在没加载script文件前alert().