<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="<?php echo $mymps_global[SiteUrl]?>/template/global/button.css">
<script language="javascript" src="<?php echo $mymps_global[SiteUrl]?>/template/global/noerr.js"></script>
<?php if(!$mymps_global['mapapi']){?>
<script type="text/javascript">alert('您没有开通蚂蚁CMS地图功能！');</script>
</head>
<body>
<center style="margin:50px;"><a href="https://www.mayicms.com/jiaocheng/article-6.htm" target="_blank">点击查看地图详细设置步骤</a></center>
</body>
</html>
<?php }else{?>
<script type="text/javascript" language="javascript" src="<?php echo $mymps_global[mapapi]; ?>"></script>
<script language="javascript">
function Gid(id) {
    return document.getElementById(id)
}

function besuremark() {
    var point = Gid("result_").value;
	var address = Gid("address_").value;
    if (point == '') {
        if (!confirm('您尚未标注地图\n\n确认保存吗?')) return false
    } else {
        parent.document.getElementById('mappoint').value = point;
		parent.document.getElementById('address').value = address;
        alert('地理位置标注成功！')
    }
    parent.closeopendiv()
}
</script>
</head>
<body style="margin:10px; font-size:14px;" class="map">
<?php if($action == 'markpoint'){?><input class="maptext" id="text_" type="text" value="" placeholder="鼠标单击地图进行标注或者输入地址搜索"/><div id="searchResultPanel" class="none"></div><font class="none">经纬度：<input id="result_" type="text"/><input id="address_" type="text"/></font><input class="mapsubmit" type="button" value="标注完成后，点我确定" onclick="besuremark();"/><?php }?>
<div id="container" style="width: <?php echo $action == 'markpoint'? $width.'px' :'100%';?>; height: <?php echo $height; ?>px;overflow:hidden;"></div>
</body>
</html>
<script type="text/javascript">
var p1 = "<?php echo $p1; ?>";var p2 = "<?php echo $p2; ?>";var view_level = "<?php echo $level; ?>";

if (p1 && p2) {
    Gid("result_").value = p1 + "," + p2
}
var map = new BMap.Map("container");
map.addControl(new BMap.NavigationControl());
if (p1 != "" && p2 != "") {
    var _1 = new BMap.Point(p1, p2);
    var marker = new BMap.Marker(_1);
    map.centerAndZoom(_1, view_level);
    map.addOverlay(marker);
    marker.enableDragging();
    marker.setAnimation(BMAP_ANIMATION_BOUNCE);
    marker.addEventListener("dragend", function(e) {
        Gid("result_").value = e.point.lng + "," + e.point.lat
    });
	getAddress(_1);
    map.addEventListener("click", showInfo);
} else {
    map.centerAndZoom(new BMap.Point(), view_level);
    var geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(function(r) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            var marker = new BMap.Marker(r.point);
            map.addOverlay(marker);
            map.panTo(r.point);
            marker.enableDragging();
            marker.setAnimation(BMAP_ANIMATION_BOUNCE);
            Gid("result_").value = r.point.lng + "," + r.point.lat;
			
			getAddress(r.point);
			
            marker.addEventListener("dragend", function(e) {
                Gid("result_").value = e.point.lng + "," + e.point.lat;
				getAddress(e.point);
            })
        } else {
            var myCity = new BMap.LocalCity();
            myCity.get(myFun)
        }
    });
    map.addEventListener("click", showInfo)
}
map.enableScrollWheelZoom();
map.enableContinuousZoom();
var myValue;
var localSearch = new BMap.LocalSearch(map);
var ac = new BMap.Autocomplete({
    "input": "text_",
    "location": map
});
localSearch.enableAutoViewport();
ac.addEventListener("onhighlight", highLight);
ac.addEventListener("onconfirm", showSea);

function getAddress(pt){
	new BMap.Geocoder().getLocation(pt, function(rs) {
	var addComp = rs.addressComponents;
	Gid("address_").value = addComp.province +addComp.city +addComp.district +addComp.street +addComp.streetNumber 
	});
}

function showInfo(e) {
    map.clearOverlays();
    var point = new BMap.Point(e.point.lng, e.point.lat);
    var marker = new BMap.Marker(point);
    Gid('result_').value = e.point.lng + ',' + e.point.lat;
    map.addOverlay(marker);
    map.panTo(e.point);
    marker.enableDragging();
    marker.addEventListener("dragend", function(e) {
        Gid("result_").value = e.point.lng + "," + e.point.lat
    });
	getAddress(e.point);
    marker.setAnimation(BMAP_ANIMATION_BOUNCE)
}

function highLight(e) {
    var str = "";
    var _0 = e.fromitem.value;
    var value = "";
    if (e.fromitem.index > -1) {
        value = _0.province + _0.city + _0.district + _0.street + _0.business
    }
    str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
    value = "";
    if (e.toitem.index > -1) {
        _0 = e.toitem.value;
        value = _0.province + _0.city + _0.district + _0.street + _0.business
    }
    str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
    Gid("searchResultPanel").innerHTML = str
}

function setPlace() {
    map.clearOverlays();

    function myFun() {
        var pp = local.getResults().getPoi(0).point;
        Gid("result_").value = pp.lng + "," + pp.lat;
        map.centerAndZoom(pp, view_level);
        var marker = new BMap.Marker(pp);
        map.addOverlay(marker);
		getAddress(pp);
        marker.enableDragging();
        marker.addEventListener("dragend", function(e) {
            Gid("result_").value = e.point.lng + "," + e.point.lat
        });
        marker.setAnimation(BMAP_ANIMATION_BOUNCE)
    }
    var local = new BMap.LocalSearch(map, {
        onSearchComplete: myFun
    });
    local.search(myValue)
}

function searchByStationName() {
    map.clearOverlays();
    var keyword = Gid("text_").value;
    localSearch.setSearchCompleteCallback(function(searchResult) {
        var poi = searchResult.getPoi(0);
        Gid("result_").value = poi.point.lng + "," + poi.point.lat;
        map.centerAndZoom(poi.point, view_level);
        var marker = new BMap.Marker(new BMap.Point(poi.point.lng, poi.point.lat));
        map.addOverlay(marker);
        var content = Gid("text_").value + "<br/><br/>经度：" + poi.point.lng + "<br/>纬度：" + poi.point.lat + "<br/>如有误请输入详细的小区名称重新查询";
        var infoWindow = new BMap.InfoWindow("<p style='font-size:14px;'>" + content + "</p>");
        marker.openInfoWindow(infoWindow);
        marker.setAnimation(BMAP_ANIMATION_BOUNCE)
    });
    localSearch.search(keyword)
}

function showSea(e) {
    var _0 = e.item.value;
    myValue = _0.province + _0.city + _0.district + _0.street + _0.business;
    Gid("searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
    setPlace()
}

function myFun(result) {
    var cityName = result.name;
    map.setCenter(cityName)
}
</script>
<?php }?>