﻿    var map = null;
    var geocoder = null;
    var marker = null;
    var checkaddress = null;
    function initialize(GLatLngx,GLatLngy) {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setMapType(G_NORMAL_MAP); 
        var center = new GLatLng(GLatLngx,GLatLngy);
        document.getElementById("GLatLng").value='坐标:'+GLatLngx+','+GLatLngy+'';
        map.setCenter(center, 1);
        map.setUIToDefault();
        map.setMapType(G_HYBRID_MAP);
        map.disableScrollWheelZoom();
        marker = new GMarker(center, {draggable: true});
        map.addControl(new GScaleControl());//加入比例工具
        GEvent.addListener(marker, "dragstart", function() {
          map.closeInfoWindow();
        });
        GEvent.addListener(marker, "dragend", function(p) {
								         if (geocoder) {
								        geocoder.getLocations(p,function(result) {if (!result) {
								            } else {if(result.Status.code==G_GEO_SUCCESS){
								              checkaddress = result.Placemark[0].address;
								              document.getElementById("address").value=checkaddress;
								              var GLatLngx=result.Placemark[0].Point.coordinates[1];
								              var GLatLngy=result.Placemark[0].Point.coordinates[0];
								              document.getElementById("GLatLng").value='坐标:'+GLatLngx+','+GLatLngy;
											  }
								        }})};
        });
        if(document.getElementById("address").value=='示例：故宫 或更为精确 北京故宫'){
               marker.openInfoWindowHtml("<h3 style='color:red; margin-top:20px'>拖动此图标,精确定位景点的地理位置</h3>");
        }
        GEvent.addListener(marker, "click", function() {
               marker.openInfoWindowHtml("<h3 style='color:red; margin-top:20px'>拖动此图标,精确定位景点的地理位置</h3>");
            });//加入一个点击事件，点击就打开信息窗口。
        map.addOverlay(marker);
        geocoder = new GClientGeocoder();
      }
    }
    // 结束
function showLocation() {
	// 清除marker
	map.clearOverlays();
	var address = document.getElementById('address').value;
	if(address!=''&&address!='示例：‘故宫’或更为精确的‘北京故宫’'){
	document.getElementById('divOutput').innerHTML = "搜索中...";
	geocoder.getLocations(address, cb_showLocation);}
}
function cb_showLocation(result) {
	// 顯示結果
	if (result.Status.code == G_GEO_SUCCESS)  {
		// 成功
		document.getElementById("divOutput").innerHTML = "";
		if(result.Placemark.length==1){
		    var addressd=result.Placemark[0].address;
		    document.getElementById('address').value=addressd;
		    checkaddress = addressd;
		    var lat = result.Placemark[0].Point.coordinates[1]; // lat
            var lng = result.Placemark[0].Point.coordinates[0]; // lng
		    document.getElementById("GLatLng").value='坐标:'+lat+','+lng;
		    findLocation(''+addressd+'',lat,lng);
		}
		else{document.getElementById('divOutput').innerHTML = "请点选以下正确的位置";
		    for (var i=0; i<result.Placemark.length; i++) {
			    var lat = result.Placemark[i].Point.coordinates[1]; // lat
			    var lng = result.Placemark[i].Point.coordinates[0]; // lng
			    var address = result.Placemark[i].address; // 地址
//			    var point = new GLatLng(lat,lng);
    			
//			    var myIcon= new GIcon();
//                myIcon.image = "http://labs.google.com/ridefinder/images/mm_20_green.png";
//                myIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
//                myIcon.iconSize = new GSize(12, 20);
//                myIcon.shadowSize = new GSize(22, 20);
//                myIcon.iconAnchor = new GPoint(6, 20);
//                myIcon.infoWindowAnchor = new GPoint(5, 1);
//                
//			    var marker = new GMarker(point,//创建自定义的GMarker
//                                        {icon: myIcon,title: i+1});
//                var marker = new GMarker(point, {draggable: true});
//                map.addOverlay(marker);
                
    			document.getElementById("GLatLng").value='坐标:'+lat+','+lng;
			    document.getElementById("divOutput").innerHTML += "<li>"+((i+1) + "、<a href=\"javascript:void(0)\" onclick=\"findLocation('"+address+"',"+lat+","+lng+");return false;\">" + address + "</a></li>");
		    }
		}
	}
	else {
		document.getElementById("divOutput").innerHTML = "没有你要找的内容!";//result.Status.code;
	}
}
function addAddressToMap(response) {
      map.clearOverlays();
      if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
      } else {
        place = response.Placemark[0];
        
        point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
        marker = new GMarker(point, {draggable: true});
        
        GEvent.addListener(marker, "dragstart", function() {
        map.closeInfoWindow();
        });
        GEvent.addListener(marker, "dragend", function(p) {
         /* marker.openInfoWindowHtml("正在反弹...");*/
		 								if (geocoder) {
								        geocoder.getLocations(
								          p,
								          function(result) {
								            if (!result) {
								              alert("不能解析: " + address);
								            } else {
								            	if(result.Status.code==G_GEO_SUCCESS)
												{
								              document.getElementById("address").value=result.Placemark[0].address;
								              var GLatLngx=result.Placemark[0].Point.coordinates[1];
								              var GLatLngy=result.Placemark[0].Point.coordinates[0];
								              document.getElementById("GLatLng").value='坐标:'+GLatLngx+','+GLatLngy;
											  }
								        }})};
        });
        map.addOverlay(marker);
        marker.openInfoWindowHtml(place.address + '<br>' +
          '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
      }
    }
function findLocation(address,GLatLngy,GLatLngx) {
    document.getElementById('address').value=address;
    checkaddress = address;
    map.clearOverlays();
    var center = new GLatLng(GLatLngx,GLatLngy);
    map.setCenter(center, 13);
    geocoder.getLocations(address, addAddressToMap);
}
    //结束
    //
function CheckTermName(getid)
{
    var textbox1=document.getElementById("TextBox1").value;
    var dropdownlistobj=document.getElementById("DropDownList5");
    var dropdownlist5=dropdownlistobj.options[dropdownlistobj.selectedIndex].text
    var checktermobj=document.getElementById("checkterm");
    var address = document.getElementById("address").value;
    var reqid = document.getElementById("reqid").value;
    if(getid==1){
        $.get("../ajaxquery/Default.aspx", { getkey: textbox1,msg: "checkct"},
            function(data){
                checktermobj.innerHTML = data;       
		if (checktermobj.innerHTML = data) {
				document.getElementById("descrip").style.display = "none";}
			else {
				document.getElementById("descrip").style.display = "block";
			}
		});
    }
    if(getid==2){
        if(textbox1=='')
        {
            alert("词条名称为必选!");
            document.getElementById("TextBox1").focus();
            return false;
        }
        if(dropdownlist5=='请选择'&&checktermobj.innerHTML=='')
        {
            alert("词条类别为必选!");
            document.getElementById("DropDownList5").focus();
            return false;
        }
        if(address == "示例：‘故宫’或更为精确的‘北京故宫’"&&checktermobj.innerHTML=='')
        {
            alert("词条地理位置为必填!");
            document.getElementById("address").focus();
            return false;
        }
        if(checkaddress == null && reqid==''&&checktermobj.innerHTML=='')
        {
            alert('检测到你未使用地图定位，请先选择地图定位!');
            document.getElementById("address").focus();
            return false;
        }
        return true;
    }
}
