實現點擊百度地圖上的覆蓋物,然后獲取覆蓋上的屬性,進而實現數據傳送。
var pointArray=new Array();//創建一個數組存儲坐標
/*在地圖上標點*/
function ShowAllPoint(){
$.ajax({
type: "GET",
url: "http://localhost:27731/api/Welcome/ShowAllPoint",
data: {},
dateType: "json",
params:{"contentType": "application/json;charset=utf-8"},
success: function (data) {
for(var i=0;i < data.length;i++){
//創建點
marker = new BMap.Marker(new BMap.Point(data[i].Longitude,data[i].Latitude));
//增加點
map.addOverlay(marker);
pointArray[i]=new BMap.Point(data[i].Longitude,data[i].Latitude);
var _id=data[i].Id;
marker.customData={id:_id};//自定義參數id
marker.addEventListener("click",attribute);
}
},
error:function(data) {
layer.msg(data.msg,{icon:2,time:3000});
},
});
//讓所有點在視野范圍內
map.setViewport(pointArray);
//獲取覆蓋物位置
function attribute(e){
var p = e.target;
var id= e.target.customData.id;
alert("id="+id+"marker的位置是" + p.getPosition().lng + "," + p.getPosition().lat);
}
