如何不顯示地圖就獲取位置數據?

使用「同步加載插件的方式」,引用各種插件,就能夠不建立地圖,直接獲取地圖數據。javascript

 

如下用IP定位作爲例子,詳細講述「如何不顯示地圖就獲取當前位置」。html

 

引入城市定位插件,更多插件與使用方法請見插件類總覽 java

<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=您的Key&plugin=AMap.CitySearch"></script>

 

實例化城市定位web

var citysearch = new AMap.CitySearch();

 

定位結果,若是成功返回城市名稱,若是失敗返回錯誤信息api

citysearch.getLocalCity(function(status, result) {
            if (status === 'complete' && result.info === 'OK') {
                if (result && result.city && result.bounds) {
                    var cityinfo = result.city;
                    var citybounds = result.bounds;
                    document.getElementById('tip').innerHTML = '您當前所在城市:'+cityinfo;
                }
            } else {
                document.getElementById('tip').innerHTML = result.info;
            }
        });

 

截圖ide

 

所有源代碼ui

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>根據ip定位</title>
    <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=0250860ccb5953fa5d655e8acf40ebb7&plugin=AMap.CitySearch"></script>
</head>
<body>
<div id="container"></div>
<div class="button-group">
    <input type="button" class="button" value="顯示當前城市" onClick="javascript:showCityInfo()"/>
</div>
<div id="tip"></div>
<script type="text/javascript">
    //獲取用戶所在城市信息
    function showCityInfo() {
        //實例化城市查詢類
        var citysearch = new AMap.CitySearch();
        //自動獲取用戶IP,返回當前城市
        citysearch.getLocalCity(function(status, result) {
            if (status === 'complete' && result.info === 'OK') {
                if (result && result.city && result.bounds) {
                    var cityinfo = result.city;
                    var citybounds = result.bounds;
                    document.getElementById('tip').innerHTML = '您當前所在城市:'+cityinfo;
                }
            } else {
                document.getElementById('tip').innerHTML = result.info;
            }
        });
    }
</script>
</body>
</html>                        

    
View Code

 

在線示例:http://zhaoziang.com/amap/nomapGetcity.htmspa

相關文章
相關標籤/搜索