Google Maps JavaScript API V3 根據地址 載入地圖

來源:互聯網
上載者:User

如下:
            

 

javascript代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標題文檔</title><script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script><script type="text/javascript">var geocoder;var map;var query = "香港 灣仔 維多利亞公園";var display = "<b>單位:</b> 維多利亞公園";function initialize() {geocoder = new google.maps.Geocoder();var myOptions = {zoom: 17,mapTypeId: google.maps.MapTypeId.ROADMAP}map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);codeAddress();}function codeAddress() {var address = query;geocoder.geocode({'address': address}, function(results, status) {if (status == google.maps.GeocoderStatus.OK) {map.setCenter(results[0].geometry.location);var marker = new google.maps.Marker({map: map,position: results[0].geometry.location});var infowindow = new google.maps.InfoWindow({content: "<b>地址:</b>" + address + "<br>" + display});infowindow.open(map, marker);} else {alert("未能解析該地址的原因: " + status);}});}</script></head><body onload="initialize()"><div id="map_canvas" style="width:500px; height:400px"></div></body>



新封裝版本:
var map; var geocoder = new google.maps.Geocoder(); /** *-25.363882,131.044922 *初始地圖 */
function initialize(lat1, lng1, zoom, canvas_div) {
    var myLatlng = new google.maps.LatLng(lat1, lng1);
    var myOptions = {
        zoom: zoom,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById(canvas_div), myOptions);
}
//根據座標點 mark 標記 在初始地圖時function placeMarker(latLng,html) {
    var marker = new google.maps.Marker({
        position: latLng,
        map: map,
        cursor: "1",
        flat: true,
        draggable: false,
        clickable: true,
        visible: true
    });
    map.setCenter(latLng);
    var infowindow = new google.maps.InfoWindow({ content: html });
   // infowindow.setPosition(latLng);    infowindow.open(map, marker);
}
//根據地址擷取座標function codeAddress(latitude, longitude,html, address, Fun) {
    if (0 == latitude || 0 == longitude) {
        if (geocoder) {
            geocoder.geocode({ 'address': address }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    Fun(results[0].geometry.location, html);
                } else {
                    Fun(latLng,html);
                    //.return false;                    //alert("Geocode was not successful for the following reason: " + status);                }
            });
        }
    } else {
        placeMarker(new google.maps.LatLng(latitude, longitude),html);
    }
}

調用:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script src="Scripts/gmap.js" type="text/javascript"></script>
 $(function () {
        initialize('0.000000', '0.000000', 16, 'map_canvas');
          var html = 'xxx';
        codeAddress('0.000000', '0.000000',html, addr, placeMarker);       
    });

 

百度:

請求介面中有四個參數:
其中,from和to對應的值分別是:0真實座標;2google座標;4baidu座標。
from:被轉換的座標體系
to:轉換到這個座標體系
x:經度
y:緯度
//GPS經緯度轉換為百度座標//gps:        http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=lng&y=lat//google:     http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x=lng&y=lat//例子:       http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=116.397428&y=39.90923            string s = bind("MTE2LjQxMDA0OTUwNTY2", "MzkuOTE2OTc5NTE5ODcz");                //116.41004950566,39.916979519873        protected string bind(string a,string b)        {           return Base64Decode(a) + "," + Base64Decode(b);        }        public static string Base64Encode(string AStr)        {            return Convert.ToBase64String(Encoding.UTF8.GetBytes(AStr));        }        public static string Base64Decode(string ABase64)        {            return Encoding.UTF8.GetString(Convert.FromBase64String(ABase64));        }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.