2011-11-28 94 views
1

我有一個codeigniter應用程序來加載谷歌地圖。這是我的視圖頁面代碼,當我點擊按鈕view_map_box()函數被調用。谷歌地圖問題OnClick的按鈕

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 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()地圖未加載它加載一些其他觀點page.And也產生警告的螢火

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

你可以給我實時的網址,我儘量找到解決方案! – sathishkumar

回答

0

您是否嘗試過使用這種library。它會爲您生成帶有自定義標記的地圖。這是非常直接的使用。