結合高機能系統家具與室內設計,專業團隊免費丈量規劃
為您打造夢想居家空間!
平價窗簾提供各大品牌冷氣維修,各型號不冷
不開機,各式問題排除,來電速洽

首頁  •  j2h 論壇 • 程式設計討論     • 

[AJAX] 取得google地圖四角座標

房東:小蛙
發表時間:2011-11-28


<?php

$id = $_GET['id'];

// ....撈出這筆 id 的地址  過程略

$address = $book['address'];

$map_api_key = 申請的 google maps API;

$smarty->assign('google_maps_js', load_google_maps($map_api_key, $address));


function load_google_maps($map_api_key, $address) {

$gmap_js = '

 <!-- Google Maps Mashup -->

 <script type="text/javascript" src="http://www.google.com/jsapi?key=' ... "></script>

 <script type="text/javascript">

  google.load("maps", "2", {"locale" : "zh_TW"}); // 引入 maps api 、設定介面語系為正體中文

  function initialize() {

   //if (GBrowserIsCompatible()) { 判斷瀏覽器是否支援 google maps

   var map = new google.maps.Map2(document.getElementById("map"));

   var geocoder = new GClientGeocoder(); // 地理編碼解譯

   var address = "'.$address.'";

   map.addControl(new GSmallMapControl());

   map.addControl(new GOverviewMapControl());

   geocoder.getLatLng(address, function(point) {

    if (!point) {

     alert("Google Maps 找不到該地址,無法顯示地圖!"); // 如果Google Maps無法顯示該地址的警示文字

    } else {

     map.setCenter(point, 16);

     /* 暫訂 */

     var marker = createMarker(point, "'.$address.'");

     map.addOverlay(marker);

     

     var bounds = map.getBounds();

     // 西南座標

     var southWest = bounds.getSouthWest();

     // 東北座標

     var northEast = bounds.getNorthEast();

     // 地圖四角座標

     var lng1 = northEast.lng();

     var lng2 = southWest.lng();

     var lat1 = northEast.lat();

     var lat2 = southWest.lat();

     // 除錯

     //document.getElementById("debug").innerHTML = "xml.php?lng1="+ lng1 +"&lng2="+ lng2 +"&lat1="+ lat1 +"&lat2="+ lat2;

     GDownloadUrl("xml.php?lng1="+ lng1 +"&lng2="+ lng2 +"&lat1="+ lat1 +"&lat2="+ lat2, function(data, responseCode) {

      if(responseCode == 200) {

       var xml = GXml.parse(data);

       var markers = xml.documentElement.getElementsByTagName("marker");

       for (var i = 0; i < markers.length; i++) {

        var lat = parseFloat(markers[i].getAttribute("lat"));

        var lng = parseFloat(markers[i].getAttribute("lng"));

        var point = new GLatLng(lat,lng);

        var title = markers[i].getAttribute("title");

        //var address = markers[i].getAttribute("title");

        //var title = markers[i].getAttribute("title");

        var marker = createMarker(point, title);

        map.addOverlay(marker);

       }

      } else if(responseCode == -1) {

       alert("Data request timed out. Please try later.");

      } else {

       alert("Request resulted in error. Check XML file is retrievable.");

      }

     });

     

    }

   });

  }

  function createMarker(point, title) {

   var marker = new GMarker(point);

   var html = "<p><small>" + title + "</small></p>";

   GEvent.addListener(marker, "click", function() {

    marker.openInfoWindowHtml(html);

   });

   return marker;

  }

  google.setOnLoadCallback(initialize);

 </script>

';

return $gmap_js;

}


$smarty->display('show_map.html');


?>


 





  • 贊助網站       

    廣利不動產-新板特區指名度最高、值得您信賴的好房仲
    您的托付,廣利用心為您服務
    廣利不動產-板橋在地生根最實在--新板特區指名度最高、值得您信賴的好房仲
    完整房訊,房屋、店面熱門精選物件,廣利不動產 優質仲介,房屋租賃、買賣資訊透明,交易真安心!



  •  共 0 人回應

    姓名:
    佈告內容:
    其他選項: