我們可以很容易地使用HTML5導(dǎo)航對(duì)象獲取當(dāng)前位置。請(qǐng)按照以下步驟來(lái)獲得城市/國(guó)家的細(xì)節(jié)。
首先包括jQuery庫(kù)
JavaScript Code復(fù)制內(nèi)容到剪貼板
<script type="text/javascript" src="jquery.js"></script> jQuery(function() { //call navigator object to get latitude and logtitute if(navigator.geolocation) { //getting current latitude and longtitude navigator.geolocation.getCurrentPosition(function(position) { //get location details based on latitude and longtitude from Google var jsonUrl = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+position.coords.latitude+","+position.coords.longitude jQuery.getJSON(jsonUrl , function( data ) { var results = data.results[0]; console.info(results); //check firebug alert(results.address_components[5].long_name); //City alert(results.address_components[6].long_name); //State alert(results.address_components[7].long_name); //Country }, function(){ alert('Failed to get Location Details...'); }); return false; }); }});
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com