1. 程式人生 > >百度地圖根據輸入的位置獲取座標

百度地圖根據輸入的位置獲取座標

在操作頁面引入以下js

//百度官方js
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的金鑰"></script>
//根據輸入獲取座標的js
    <script type="text/javascript" src="http://***.com/assets/js/shop.js"></script>

//表單程式碼:
<input placeholder="請輸入商鋪地址" id="address" required="true" type="text"
/> <input placeholder="點選自動獲取座標" readonly="true" id="shopcoord" type="text" /> <input id="getPoint" type="button" value="獲取座標" />

shop.js 程式碼:

$(function(){
    /**
     * 點選獲取座標
     */
    $('#getPoint').click(function(){
        if($('#address').val() == ''){
            alert('請填寫一個地址'
); } var adds = $('#address').val(); getPoint(adds); }) }) function getPoint(adds){ // 建立地址解析器例項 var myGeo = new BMap.Geocoder(); // 將地址解析結果顯示在地圖上,並調整地圖視野 myGeo.getPoint(adds, function(point){ $('#shopcoord').val(JSON.stringify(point)); }, "北京市"); }