关于A按钮Onclick的谷歌地图问题

时间:2011-11-28 11:53:35

标签: php jquery ajax codeigniter

我有一个codeigniter应用程序来加载谷歌地图。这是我的视图页面代码,当我点击按钮view_map_box()函数被调用。

function view_map_box()
{
    var cnt=$("#search_agency_res").serialize();
        var region = $("#agency_region").val();
        var country = $("#agency_country").val();
        var agency = $("#agency_agency").val();
        if(region == "")
        {

            $("#lsitAlert").show();
            $("#lsitAlert").html("<img src='<?php echo base_url();?>Images/exclamationR.png' style='float:left;'/>&nbsp;&nbsp;<div style='float:left;margin-top:8px;'>Select A region</div>");
            $("#lsitAlert").css("background-color","#a90b05");
            $("#addAlert").css("padding","0px");
            $("#agency_region").css("border","1px solid #f9612c");
            $("#agency_region").css("border-radius","5px");
            return false;
        }
        else
        {
        $.ajax({  
                type: "POST",  
                url: "<?php echo base_url();?>index.php/addAgency/agency_map_result/"+region+"/"+country+"/"+agency,
                data: cnt,
                success: function(valmsgnew){
                $("#map").css("display","block");
                $("#map").html(valmsgnew).show();
                }
            });
        }
}

这是控制器代码

function agency_map_result($region,$country = "",$agency = "")
    {
        $data['values'] = $this->pms_agency_model->search_map($region,$country,$agency);
        echo $this->load->view('admin/markers',$data);
    }

这是型号代码

function search_map($region,$country = "",$agency = "")
    {
        extract($_POST);
        $aregion = $_POST['agency_region'];
        $acountry = $_POST['agency_country'];
        $aagency = $_POST['agency_agency'];

        if($aregion != "" && $acountry == "" && $aagency == "")
        { 
            $this->db->where('tblagencies.varregion',$aregion);
            return $val = $this->db->get('tblagencies')->result();
        }
        else if($aregion != "" && $acountry != "" && $aagency == "")
        {
            $this->db->where('tblagencies.varregion',$aregion);
            $this->db->where_in('varcountry', $acountry);
            return $val = $this->db->get('tblagencies')->result();
        }
        else if($aregion != "" && $acountry != "" && $aagency != "")
        {
            $this->db->where('tblagencies.varregion',$aregion);
            $this->db->where_in('varcountry', $acountry);
            $this->db->where('tblagencies.intaid',$aagency);
            return $val = $this->db->get('tblagencies')->result();
        }
    }

这是正在加载的地图页面。

<?php 
function get_latlng($cityname)
{
$city = urlencode($cityname);
$Url='http://maps.googleapis.com/maps/api/geocode/json?address='.$city.'&sensor=false';
if (!function_exists('curl_init')){
    die('Sorry cURL is not installed!');
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$search_data = json_decode($output);
if($search_data->results)
{
$new = array("lat"=>$search_data->results[0]->geometry->location->lat,"lng"=>$search_data->results[0]->geometry->location->lng);
return $new;
}
}
?>
    <style>
      .gmap3{
        margin: 20px auto;
        border: 1px dashed #C0C0C0;
        width: 500px;
        height: 500px;
      }
    </style>

    <script type="text/javascript">

      $(function(){

        $('#test1')
          .gmap3(
          { action:'init',
            options:{
            center:[53.067627,-1.625977],
            <?php 
            if($values)
            {?>
              zoom: 5
              <?php
              }
              else{
              ?>
               zoom: 2
              <?php
              }
              ?>
            }
          },
          { action: 'addMarkers',
            markers:[<?php 
            if($values)
            {
            foreach($values as $rows)
            {
                $varmetro = $rows->varmetro;
                $newvar = explode(',',$varmetro);
                $count = count($newvar);
                for($i=0;$i<$count;$i++)
                {
                $sql='SELECT * FROM `tblcity` WHERE `intcityid`='.$newvar[$i].'';
                $res=mysql_query($sql);
                $sql_res=mysql_fetch_row($res);
                $cityname =$sql_res[2];
                $new = get_latlng($cityname);
                //print_r($new);

            ?>
              {lat:<?php echo $new["lat"];?>, lng:<?php echo $new["lng"];?>,data:'<?php echo $cityname;?><br><br><?php echo "Agency : ".$rows->varagencyname;?> '},
            <?php }}}?>],
            marker:{
              options:{
                draggable: false
              },
              events:{
                mouseover: function(marker, event, data){
                  var map = $(this).gmap3('get'),
                      infowindow = $(this).gmap3({action:'get', name:'infowindow'});
                  if (infowindow){
                    infowindow.open(map, marker);
                    infowindow.setContent(data);
                  } else {
                    $(this).gmap3({action:'addinfowindow', anchor:marker, options:{content: data}});
                  }
                },
                mouseout: function(){
                  var infowindow = $(this).gmap3({action:'get', name:'infowindow'});
                  if (infowindow){
                    infowindow.close();
                  }
                }
              }
            }
          }
        );
      });
    </script>
    <div id="test1" class="gmap3" style="width:700px;"></div>

但是当调用此函数时,view_map_box()地图未加载,它会加载其他一些视图页面。并且还会在firebug中生成警告

A call to document.write() from an asynchronously-loaded external script was ignored.

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用此library。它会为您生成带有自定义标记的地图。这是非常直接的使用。