Javascriptグローバル変数の使い方

12309 ワード

1.デモの例説明
<script type="text/javascript">

    var gDivId;  //js    



function geocoder(lastLon,lastLat,result) {

    alert("lastLon:"+lastLon);

    alert("lastLat:"+lastLat);

    alert("result:"+result);

    gDivId = result;  //       

    

}



function testff(){

    alert("gDivId: " + gDivId);  //      

}

</script>
<input type="button" value="     " onclick="geocoder(1212,3434,'test')"/>



<input type="button" value="    " onclick="testff()"/>
 
2.高徳地図に適用し、経緯度によって住所情報を調べる.
<!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=gb2312" />

<title>     </title>

<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=d94035ac264f0cc5b293199360ca0e1e"></script>





</head>



<body>

<div id="iCenter" style="display:none;"></div>

            121.432921, 31.196159

              <div id="result"> </div>

              <input type="button" value="     " onclick="geocoder('121.432921', '31.196159','result')"/>



<script language="javascript">



var mapObj;

var gDivId;



function geocoder(lastLon,lastLat,result) {    

    gDivId = result;  //       

    

    //     

    var lnglatXY = new AMap.LngLat(lastLon,lastLat);

    

    mapObj = new AMap.Map("iCenter", {

        view: new AMap.View2D({

        center:new AMap.LngLat(lastLon,lastLat),//     

        zoom:13 //         

        })

    });

    

    var MGeocoder;

    //        

    mapObj.plugin(["AMap.Geocoder"], function() {       

        MGeocoder = new AMap.Geocoder({

            radius: 1000,

            extensions: "all"

        });

        //        

        AMap.event.addListener(MGeocoder, "complete", geocoder_CallBack);

        //     

        MGeocoder.getAddress(lnglatXY);

    });

    

    //mapObj.setFitView();

}



//    

function geocoder_CallBack(data) {

    //      

    address = data.regeocode.formattedAddress;



    //        ,  Jquery   。

    //$("#"+gDivId).html(address);

    document.getElementById(gDivId).innerHTML = address;

} 

</script>

            

</body>

</html>