高德地圖 – 1.問題集錦

image

 

1.自動加載文本框的座標,並在地圖標註點。html

2.點擊地圖時,而且「逆地理編碼」解析出地址方在文本框git

 

jsweb

  1. var lnglatXY;  
  2. var marker;  
  3.   
  4. //初始化地圖對象,加載地圖  
  5. var map = new AMap.Map('mapContainer', {  
  6.     resizeEnable: true,  
  7.     view: new AMap.View2D({  
  8.         center: new AMap.LngLat(106.46198329.516409),  
  9.         zoom: 10,  
  10.     })  
  11. });  
  12.   
  13.   
  14. //加點  
  15. marker = new AMap.Marker({  
  16.     icon: "http://webapi.amap.com/images/marker_sprite.png",  
  17.     position: new AMap.LngLat($("#EditFormMap input[name='longitude']").val(), $("#EditFormMap input[name='latitude']").val())  
  18. });  
  19.   
  20. //uncaught exception: Invalid Object: Pixel(NaN, NaN)  
  21. //這裏報這個錯誤,是由於不能讀取到$("#EditFormMap input[name='longitude']").val(), $("#EditFormMap input[name='latitude']").val() 的值  
  22. //由於要在本頁點【地圖】按鈕,纔會賦值。因此該註銷掉  
  23. //marker.setMap(map);  //在地圖上添加點  
  24.   
  25.   
  26.   
  27. AMap.event.addListener(map, 'click', getLnglat);  
  28.   
  29.   
  30. //鼠標在地圖上點擊,獲取經緯度座標  
  31. function getLnglat(e) {  
  32.     map.clearMap(); //刪除地圖上的全部覆蓋物  
  33.   
  34.     //經度賦值  
  35.     document.getElementsByName('longitude').value = e.lnglat.getLng();  
  36.     $("#EditFormMap input[name='longitude']").attr("value", e.lnglat.getLng())  
  37.     //緯度賦值  
  38.     document.getElementsByName('latitude').value = e.lnglat.getLat();  
  39.     $("#EditFormMap input[name='latitude']").attr("value", e.lnglat.getLat());  
  40.   
  41.     //逆地址編碼須要的 X,Y  
  42.     lnglatXY = new AMap.LngLat(e.lnglat.getLng(), e.lnglat.getLat());  
  43.     geocoder();  
  44. }  
  45.   
  46. function geocoder() {  
  47.     var MGeocoder;  
  48.     //加載地理編碼插件  
  49.     map.plugin(["AMap.Geocoder"], function () {  
  50.         MGeocoder = new AMap.Geocoder({  
  51.             radius: 1000,  
  52.             extensions: "all"  
  53.         });  
  54.         //返回地理編碼結果   
  55.         AMap.event.addListener(MGeocoder, "complete", geocoder_CallBack);  
  56.         //逆地理編碼  
  57.         MGeocoder.getAddress(lnglatXY);  
  58.     });  
  59.   
  60.     //加點  
  61.     marker = new AMap.Marker({  
  62.         icon: "http://webapi.amap.com/images/marker_sprite.png",  
  63.         position: lnglatXY,  
  64.   
  65.     });  
  66.   
  67.     marker.setMap(map);  //在地圖上添加點  
  68.   
  69.     //map.setFitView(); //縮放平移地圖到合適的視野級別,  
  70.   
  71. }  
  72.   
  73. //回調函數  
  74. function geocoder_CallBack(data) {  
  75.   
  76.     var address;  
  77.     //返回地址描述  
  78.     address = data.regeocode.formattedAddress;  
  79.     //console.info($("#EditFormMap input[name='address']"))  
  80.     //返回結果賦值(地址欄)  
  81.     //$("#EditFormMap input[name='address']").attr("value", address);  
  82.     $("#EditFormMap input[name='address']").attr("value", address);  
  83. }  

 

htmlapi

  1. <tr>  
  2.     <th align="right">經度:</th>  
  3.     <td>  
  4.         <input id="longitude" name="longitude" class="easyui-validatebox" data-options="required:true">  
  5.         <font color="red">*.(經緯度直接鼠標在地圖選取)</font>  
  6.     </td>  
  7. </tr>  
  8. <tr>  
  9.     <th align="right">緯度:</th>  
  10.     <td>  
  11.         <input id="latitude" name="latitude" class="easyui-validatebox" data-options="required:true">  
  12.         <font color="red"></font>  
  13.     </td>  
  14. </tr>  
  15. <tr>  
  16.     <th align="right">地址:</th>  
  17.     <td>  
  18.         <input id="address" name="address" class="easyui-validatebox" style="width: 300px" data-options="required:true,validType:'maxlength[21]'">  
  19.     </td>  
  20. </tr>   

 

問題一:函數

  情況1.點擊地圖,光改變文本框的值,地圖上不加載圖標ui

  情況2.常常出現「AMap.Geocoder is not a constructor編碼

緣由:spa

image

 

屢次加載,easyui的緣由。去掉就能夠了。插件

相關文章
相關標籤/搜索