streetDirectory地图——SDMap 的笔记
2012-11-21 09:48 甘超波 阅读(1410) 评论(0) 编辑 收藏 举报SDMap的总结。(文档)
参考文档
1:http://www.streetdirectory.com/api/developer/docs/
2:http://api.streetdirectory.com/maps-for-print-usage.html
3:http://www.streetdirectory.com/api/widgets/
小结:
var Map;
var Marker;
var MarkersArray = [];
var geocoder;
var MarkerStaticManager;
var PolygonArrary=[];
var PolylineManager;
var Polygon;
var PolygonManagerArrary = [];
function OnInit() {
var myOptions = {//地图参数
zoom: 13,//地图放大级别
center: new GeoPoint(131.044922, -25.363882),//地图初始化的经纬度
enableDefaultLogo: true,//是否展示默认的SDMap的Log
showCopyright: false,
resize: {//地图展示的大小,
w: 500,
h: 500
}
};
Map = new SD.genmap.Map(//初始地图
document.getElementById("map_canvas"),
myOptions
);
//地图加当行控件
var navControl = new CompleteMapControl();
Map.addControl(navControl);
navControl.setDisplay(0, false);
//初始化 SDGeocode(主要是通过 地址找经纬度或通过经纬度找地址 的对象)
geocoder = new SDGeocode(Map);
//初始化Maker的对象管理。
MarkerStaticManager = new SD.genmap.MarkerStaticManager({
map: Map
});
//添加事件 (如果注册事件中是匿名函数,会运行2次(经过反复测试得知)做好不要用匿名函数)
EventManager.add(Map, ‘MouseDown‘, OnClickMap);//鼠标点击事件
//初始化画多边行管理对象。
PolylineManager = new SD.genmap.PolylineManager({
map: Map
});
}
//点击事件(得到点击地图事件的
function OnClickMap(e) {
if (SD.util.getMouseButton(e) == 'RIGHT') {//鼠标右键点击(不知注册click事件,在地图上有时候会报错)
var x, y;
if (document.all) {
x = e.x;
y = e.y;
}
else {
var xy =getXY(GMP.Map.dom);
x = e.pageX - xy[0];
y = e.pageY - xy[1];
}
//得到地图的经纬度
var location = GMP.Map.viewportInfo.viewportScreenToGeo(x, y); if (b) {
AddPolygonPoint(geo);
DrawPolygon();
}
else {
if (MarkersArray.length > 0) RemoveMarker();
AddMarker(geo);
}
}
//画Marker();
function AddMarker(geo) {
var icon = new SD.genmap.MarkerImage({//创建地图位置图片
image: "GoogleMap_icon.png",
iconSize: new Size(15, 15),
iconAnchor: new Point(7, 15),
infoWindowAnchor: new Point(5, 0)
});
Map.setCenter(geo, Map.zoom);
Marker = MarkerStaticManager.add({
position: geo,
map: Map,
icon: icon
});
MarkersArray.push(Marker);
}
function RemoveMarker() {
if (MarkersArray) {
for (var i = 0; i < MarkersArray.length; i++) {
MarkerStaticManager.remove(MarkersArray[i]);
}
MarkersArray.length = 0;
}
}
//画矩形
function AddPolygonPoint(location) {//注意矩形的格式 x: y:
var polygonArrary = { x: location.lon, y: location.lat };
PolygonArrary.push(polygonArrary);
}
function DrawPolygon() {
//开始画矩形
var options = {
color: "#FF0000",
size: 5,
opacity: 0.7,
bgColor: "grey",
fillOpacity: 0.5
};
Polygon = PolylineManager.add(PolygonArrary, options);
PolygonManagerArrary.push(Polygon);
}
function ClearPolygon() {//清除矩形
if (PolygonManagerArrary.length > 0) {
for (var i = 0; i < PolygonManagerArrary.length; i++) {
PolylineManager.remove(PolygonManagerArrary[i]);
}
PolygonArrary.length = 0;
PolygonManagerArrary.length = 0;
}
}
//get方法
function GetAddress() {
var address = document.getElementById("address").value;
if (address == "") {
alert("address is not null");
return;
}
var searchOption = { "q": address };
var gc = SDGeocode.SG;
geocoder.requestData(gc, searchOption);
}
function set_data(json) {
for (var i = 0; i < json.length; i++) {
var rec = json[i];
if (rec.x && rec.y) {
if (MarkersArray.length > 0) RemoveMarker();
var geo = new GeoPoint(parseFloat(rec.x), parseFloat(rec.y));
AddMarker(geo);
//
// var icon = new SD.genmap.MarkerImage({
// image: SD.BASE_URL + "img/openrice_icon.png",
// title: rec.t,
// iconSize: new Size(15, 15),
// iconAnchor: new Point(7, 15),
// infoWindowAnchor: new Point(5, 0)
// });
// mm = new SD.genmap.MarkerStaticManager({ map: map });
// var geo = new GeoPoint(parseFloat(rec.x), parseFloat(rec.y));
// map.setCenter(geo, map.zoom);
// var marker = mm.add({
// position: geo,
// map: map,
// icon: icon
// });
}
else {
alert("解析失败,请检查地址");
}
}
}
//调兼容性 IE fixfox chrom等游览器
function getXY (el) //not 100% correct if some overflow:scroll there
{
var posX = null;
var posY = null;
if (el.offsetParent) {
for (posX = 0, posY = 0; el && el.offsetParent; el = el.offsetParent) {
posX += el.offsetLeft;
posY += el.offsetTop;
}
return [posX, posY];
}
else {
posX = el.x || 0;
posY = el.y || 0;
return [posX, posY];
}
}