  function drawMap() {
      map = new GMap2(document.getElementById("map_canvas"));
      //map.setCenter(new GLatLng(MapLat, MapLng), MapZoom);
  	switchCity(currProcinceid,currCityid,currPointid);
      if (MapControl == 1) {
          map.addControl(control = new GSmallZoomControl);
      } else if (MapControl == 2) {
          map.addControl(control = new GSmallMapControl);
      } else if (MapControl == 3) {
          map.addControl(control = new GLargeMapControl);
      }
      if (MapTypeControl) {
          map.addControl(typecontrol = new GMapTypeControl);
      }
      if (MapScaleControl) {
          map.addControl(new GScaleControl);
      }
      if (MapOverviewMap) {
          map.addControl(goverviewmapcontrol = new GOverviewMapControl);
      }
      if (MapDragging) {
          map.enableDragging();
      } else {
          map.disableDragging();
      }
      if (MapInfoWindow) {
          map.enableInfoWindow();
      } else {
          map.disableInfoWindow();
      }
      if (MapDoubleClickZoom) {
          map.enableDoubleClickZoom();
      } else {
          map.disableDoubleClickZoom();
      }
      if (MapContinuousZoom) {
          map.enableContinuousZoom();
      } else {
          map.disableContinuousZoom();
      }
      if (MapGoogleBar) {
          map.enableGoogleBar();
      } else {
          map.disableGoogleBar();
      }
      if (MapScrollWheelZoom) {
          map.enableScrollWheelZoom();
      } else {
          map.disableScrollWheelZoom();
      }

  	if(Maptraffic){
  		traffic=new GTrafficOverlay();
  		//下面为绘制控件
  		function TextualTraffControl() {
  		}

  		// To "subclass" the GControl, we set the prototype object to
  		// an instance of the GControl object
  		TextualTraffControl.prototype = new GControl();

  		// Creates a one DIV for each of the buttons and places them in a container
  		// DIV which is returned as our control element. We add the control to
  		// to the map container and return the element for the map class to
  		// position properly.
  		TextualTraffControl.prototype.initialize = function(map) {
  		  var container = document.createElement("div");

  		  var trafficDiv = document.createElement("div");
  		  setTrafficButtonStyle_(trafficDiv);
  		  TextualTraffControl.open=0;
  		  container.appendChild(trafficDiv);
  		  trafficDiv.appendChild(document.createTextNode("交通流量"));
  		  GEvent.addDomListener(trafficDiv, "click", function() {
  			if(TextualTraffControl.open==0)
  			  {   map.addOverlay(traffic);
  				TextualTraffControl.open=1;
  				setOpenTrafficButtonStyle_(trafficDiv);
  			  }
  			  else
  			  {
  				  TextualTraffControl.open=0;
  				  map.removeOverlay(traffic);
  				  setTrafficButtonStyle_(trafficDiv);
  			  }
  		  });

  		  map.getContainer().appendChild(container);
  		  return container;
  		}

  		// By default, the control will appear in the top left corner of the
  		// map with 7 pixels of padding.
  		TextualTraffControl.prototype.getDefaultPosition = function() {
  		  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7));
  		}

  		// Sets the proper CSS for the given button element.


  		map.addControl(new TextualTraffControl());
  	}
     // GEvent.addListener(map, "moveend", function () {var center = map.getCenter();});
     // GEvent.addListener(map, "zoomend", function () {var z = map.getZoom();document.getElementById("zoom").value = z;});
      showPoint();
      if (MapShowAreaList) {
         // showareaList();
      }
  	var directionsPanel = document.getElementById("my_textual_div");
  	directions = new GDirections(map, directionsPanel);
  	GEvent.addListener(directions, "addoverlay", onGDirectionsAddOverlay)
  	GEvent.addListener(directions, "error", handleErrors);
  	GEvent.addListener(map, "infowindowopen", setDirectionClass);

  }
    function handleErrors(){
    if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
      alert("找不到指定地址的对应地理位置。这可能是地址比较新，或地址不正确.\nError code: " + directions.getStatus().code);
    else if (directions.getStatus().code == G_GEO_SERVER_ERROR)
      alert("无法成功处理地址解析或行车路线请求，但是确切的失败原因未知.\n Error code: " + directions.getStatus().code);
    else if (directions.getStatus().code == G_GEO_MISSING_QUERY)
      alert("HTTP q 参数缺失或没有值。对于地址解析请求，这意味着将空地址指定为输入。对于行车路线请求，这意味着在输入中未指定查询.\n Error code: " + directions.getStatus().code);
    else if (directions.getStatus().code == G_GEO_BAD_KEY)
      alert("T给定的密钥无效或与给定的域不匹配 \n Error code: " + directions.getStatus().code);
    else if (directions.getStatus().code == G_GEO_BAD_REQUEST)
      alert("无法成功解析行车路线请求.\n Error code: " + directions.getStatus().code);
    else alert("未知错误");
  }

    function setDirectionClass()
    {
    	if(!$jq('#fromhere')||!$jq('#tohere'))
    	{
    		return;
    	}
    	if(direction)
    	{
    		
    	$jq('#fromhere').css('font-weight', 'bold');	
    	$jq('#tohere').css('font-weight', 'normal');			
    	}else
    	{
    	$jq('#tohere').css('font-weight', 'bold');	
    	$jq('#fromhere').css('font-weight', 'normal');	
    	}
    }

    function createMarker(latlng, pid,cid,pointid) {
    	var blueIcon = new GIcon(G_DEFAULT_ICON);
    	var frontImg="/img/icon_map.png";	//图标
    	var iconSize="80,46";							//google默认大小20,34
    	var shadowImg="/img/icon_map_shadow.png";								//背景
    	var shadowSize="80,46";							//google默认大小37, 34
    	var dragCrossImg=null;							//拖拽时下面的十字
    	var dragCrossSize=null;							//google默认大小16,16
    	var iconAnchor=null;							//此图标在地图上的锚定点相对于图标图像左上角的像素坐标:
    	var infoWindowAnchor=null;						//信息窗口在此图标上的锚定点相对于图标图像左上角的像素坐标:
    	var dragCrossAnchor=null;						//指定拖动图标时十字交叉图像的像素坐标偏移量（相对于 iconAnchor）
    	//自定义大小和google默认大小不符时需要调整坐标，以让图标剧中。

    	if(frontImg)
    	 blueIcon.image = frontImg;
    	if (iconSize) {
            var tmp = iconSize.split(",");
            blueIcon.iconSize = new GSize(tmp[0], tmp[1]);
        }
    	if (shadowImg) {
            blueIcon.shadow = shadowImg;
        }
    	if (shadowSize) {
            var tmp = shadowSize.split(",");
            blueIcon.shadowSize = new GSize(tmp[0], tmp[1]);
        }
    	if (dragCrossImg) {
            blueIcon.dragCrossImage = dragCrossImg;
        }
    	if (dragCrossSize) {
            var tmp = dragCrossSize.split(",");
            blueIcon.dragCrossSize = new GSize(tmp[0], tmp[1]);
        }
    	if (iconAnchor) {
            var tmp = iconAnchor.split(",");
            blueIcon.iconAnchor = new GPoint(tmp[0], tmp[1]);
        }
    	 if (infoWindowAnchor) {
            var tmp = infoWindowAnchor.split(",");
            blueIcon.infoWindowAnchor = new GPoint(tmp[0], tmp[1]);
        }
    	if (dragCrossAnchor) {
            var tmp = dragCrossAnchor.split(",");
            blueIcon.dragCrossAnchor = new GPoint(tmp[0], tmp[1]);
        }
    	if(areas[pid].status==0)
        var marker = new GMarker(latlng, {icon: blueIcon,title: areas[pid].children[cid].children[pointid].title});
    	else
    	var marker = new GMarker(latlng, {icon: blueIcon,title: areas[pid].children[pointid].title});
    	
        marker.value = pointid;
        GEvent.addListener(marker, "click", function () {
    		 zoomCity(pid,cid,pointid);
    	}
    	);
        return marker;
    }


    function getPointMarkers(){
    	 var batch = new Array();
    	    for ( i in areas) {
    	    	if(areas[i].status!=1&&areas[i].children)
    			for(n in areas[i].children)
    			{
    				if(areas[i].children[n].children)
        			for(k in areas[i].children[n].children)
        			{
        				if(!areas[i].children[n].children[k])
        				{
        					continue;
        				}
        				var point = new GLatLng(areas[i].children[n].children[k].lat, areas[i].children[n].children[k].lng);
       				 	batch.push(createMarker(point, i,n,k));
        			}
    			 
    			}       
    	    }
    	    for ( i in areas) {
    	    	if(areas[i].status==1&&areas[i].children)
    			for(n in areas[i].children)
    			{
    				
        			
        				if(!areas[i].children[n])
        				{
        					continue;
        				}
        				var point = new GLatLng(areas[i].children[n].lat, areas[i].children[n].lng);
       				 	batch.push(createMarker(point, i,0,n));
        			
    			 
    			}       
    	    }
    	    return batch;
    	        
        }
    function showPoint(){
    	 var mgr = new MarkerManager(map);
    	    mgr.addMarkers(getPointMarkers(), MapShowLevel);
    	   // mgr.addMarkers(getAreaMarkers(), 1, MapShowLevel);
    	    mgr.refresh();
    	        
        }
function showCityList(pid,cid)
{
	var str="";
	var id=0;
	currProvinceid=pid;
	for( i in areas[pid].children)
	{
		if(id==0){id=i;}
		
		if(areas[pid].children[i].id==cid)
		{
		str=str+"<option value='"+areas[pid].children[i].id+"' selected='selected'>"+areas[pid].children[i].title+"</option>";
		}
		else
		{
			str=str+"<option value='"+areas[pid].children[i].id+"' >"+areas[pid].children[i].title+"</option>";
		}
	}
	currCityid=cid;
	if(currCityid==0)
	{
		currCityid=id;
	}
	

	$jq("#citylist").html(str);
	
}
function showToaddr(pid,cid,pointid)
{
	var str="";
	var divstr="";
	var list;
	var id=0;
	if(areas[pid].status==1)
	{
		list=areas[pid].children;
	}
	else
	{
		list=areas[pid].children[cid].children;
	}
	for( i in list)
	{
		if(id==0)
		{
		id=i;
		}
		if(list[i].id==cid)
		{
		str=str+"<option value='"+list[i].id+"' selected='selected'>"+list[i].title+"</option>";
		
		}
		else
		{
			str=str+"<option value='"+list[i].id+"' >"+list[i].title+"</option>";
		}
		divstr+="<div><a onclick='zoomCity("+pid+","+cid+","+i+");return false;' href='#'>"+list[i].title+"</a></div>";
	}
	currPointid=pointid;
	if(currPointid==0)
	{
		currPointid=id;
	}
	

	$jq("#toaddr").html(str);
	$jq("#addrlist").html(divstr);
}
function showProvinceList()
{
	var str="";
	var id=0;
	for(i in areas)
	{
		if(id==0)
		{
			id=i;
		}
	
			str=str+"<option value='"+areas[i].id+"' >"+areas[i].title+"</option>";
	
	}

	$jq("#provincelist").html(str);
	currProvinceid=id;
	
	
}
function switchCity(pid,cid,pointid)
{
	if(!pid)
	{
		pid=1;
	}
	if(!cid)
	{
		cid=0;
	}
	if(!pointid)
	{
		pointid=0;
	}
	currPid=pid;
	
	$sele=$jq("#provincelist").children();
	for(i=0;i<$sele.length;i++)
	{
		if($sele[i].value==pid)
		{
			$sele[i].selected="selected";
		}
	}
	
	
	var province=areas[pid];
	var addresstitle="";
	//生成title 和 坐标
	if(province.status==1)
	{
		//直辖市
		addresstitle=province.title;
		map.setCenter(new GLatLng(province.lat*1, province.lng),province.zoom*1);
		$jq("#citylistwrap").hide();
		currCityid=0;
		
	}
	else{
		if(province.children)
		{
			if(!province.children[cid])
			{
				for(i in province.children)
				{
					cid=i;
					break;
				}
			}
			currCityid=cid;
			showCityList(pid,cid);//显示搜索区域的城市下拉菜单
			$jq("#citylistwrap").show();
			address=province.title+" "+province.children[cid].title;
			map.setCenter(new GLatLng(province.children[cid].lat*1, province.children[cid].lng*1),province.children[cid].zoom*1);
		}
		else
		{
			$jq("#citylistwrap").hide();
			map.setCenter(new GLatLng(province.lat*1, province.lng*1),province.zoom*1);
			address=province.title;
		}
	}
	$jq("#addresstitle").html(addresstitle);
	showToaddr(pid,cid,pointid);
}
function zoomCity(pid,cid,pointid){
	var point;
	var cp;
	if(!areas[pid]){return;}
	if(areas[pid].status!=1)
	{
	if(!areas[pid].children[cid]||!areas[pid].children[cid].children[pointid]){return;}
	
 point = new GLatLng(areas[pid].children[cid].children[pointid].lat, areas[pid].children[cid].children[pointid].lng);
 cp=areas[pid].children[cid].children[pointid];

	}
	else
	{
		if(!areas[pid].children[pointid]){return;}
		
		 point = new GLatLng(areas[pid].children[pointid].lat, areas[pid].children[pointid].lng);
		 cp=areas[pid].children[pointid];
	}
	tttt=cp;
	map.setCenter(point, 16);
		var myHtml = "<b>" +cp.title + "</b><br/> \u5730\u5740 " + cp.address ;
		if(cp.telephone)
		{
			myHtml+="<br> \u8054\u7CFB\u7535\u8BDD："+cp.telephone;
			
		}		

		
		if(MapDriver)
		{
		//myHtml+='<a href="#" onclick="getSearchInput(); return false;">驾车</a>';
		}

		myHtml += "</div>";
		 if(MapDriver)
		{
		//myHtml+='<div id="driverSearchInput" style="display:none"><a href="#" onclick="return false;">到达此处</a> <a href="#" onclick="return false;">从此出发</a><br><input size=10  id="searchkey" ><input type=button value="搜索" onclick="search();"><br><a href="#" onclick="hideSearchInput();return false;">返回</a> </div>';
		//myHtml+='<div id="driverSearchInput" style="display:none">出发地址<br><input size=10  id="searchkey" ><input type=button value="搜索" onclick="search();"><br><a href="#" onclick="hideSearchInput();return false;">返回</a> </div>';
		}	
	map.openInfoWindowHtml(point, myHtml);
	currPointid=pointid;
	currCityid=cid;
	currProvinceid=pid;

	$sele=$jq("#toaddr").children();
	for(i=0;i<$sele.length;i++)
	{
		if($sele[i].value==pointid)
		{
			$sele[i].selected="selected";
		}
	}
	getPromotion(currProvinceid,currCityid,currPointid);
}

function getPromotion(pid,cid,pointid){
    if($jq("#promotionBox").size()>0){
	$jq("#promotionBox").load("/index.php?m=promotion&pid="+pid+"&cid="+cid+"&pointid="+pointid);

    }
}

function onGDirectionsAddOverlay(){

	for (var i=0; i<pathMarkers.length; i++){
    map.removeOverlay(pathMarkers[i]);
  	}
  	pathMarkers=[];

  for (var i=0; i<=directions.getNumRoutes(); i++){
    var originalMarker = directions.getMarker(i);
	if(i!=directions.getNumRoutes())
	  {
		 var newMarker = createUserMarker(originalMarker.getLatLng(),originalMarker);
	  }
	  else
	  {	 
		  
		   var newMarker = createEndUserMarker(originalMarker.getLatLng(),originalMarker);
	 
	  }
     pathMarkers[i] =newMarker;
    map.removeOverlay(originalMarker);
    map.addOverlay(newMarker);    
   
    GEvent.addListener(newMarker, "dragend", function(){ 
      var points = [];
     for (var i=0; i<pathMarkers.length; i++){
        points[i]= pathMarkers[i].getLatLng();
      }
     tttt=points;
    searchResultPoint=points[0];
      directions.loadFromWaypoints(points,  { "locale": locale,"travelMode":travelMode  });
    });
    
  }

}
function createUserMarker(latlng,originalMarker) {
	var markerOptions;
	var blueIcon=originalMarker.getIcon();
	markerOptions={ title:'您选择的地点 可移动到新位置',draggable :true,icon:blueIcon };	
    var marker = new GMarker(latlng,markerOptions);
    /*
	GEvent.addListener(marker, "click", function() {
    var myHtml = "<b>您选择的地点</b>";
    map.openInfoWindowHtml(latlng, myHtml);
 
  });*/
      return marker;     
      
	}
function createEndUserMarker(latlng,originalMarker) {
	var markerOptions;
	var blueIcon=originalMarker.getIcon();
	var ttt;
	if(areas[currPid].status==1){
		ttt=areas[currPid].children[currPointid].title;
	}
	else
	{
		ttt=areas[currPid].children[currCityid].children[currPointid].title;
	}
	markerOptions={ title:ttt,icon:blueIcon };	
    var marker = new GMarker(latlng,markerOptions);
    /*
	GEvent.addListener(marker, "click", function() {
    var myHtml = "<b>您选择的地点</b>";
    map.openInfoWindowHtml(latlng, myHtml);
 
  });*/
   GEvent.addListener(marker, "click", function () {
		 zoomCity(currProvinceid,currCityid,currPointid);
	}
	);
      return marker;     
      
	}


function drawMode(mode){
	travelMode=mode;
	$jq("#travelmode").children()[mode-1].selected="selected";
	
}

function searchfromform(){
	drawMode($jq("#travelmode").val());
	pointid=$jq("#toaddr").val();	
	//currPointid=pid;//MapPoints[currCityid].pointlist[pid].id;
	if(areas[currPid].status==1)
	{
		currPoint=new GLatLng(areas[currPid].children[pointid].lat, areas[currPid].children[pointid].lng);;
		searchfromgoogle(areas[currPid].title+$jq("#fromaddr").val());
		
	}
	else{
	currPoint=new GLatLng(areas[currPid].children[currCityid].children[pointid].lat, areas[currPid].children[currCityid].children[pointid].lng);;
	searchfromgoogle(areas[currPid].children[currCityid].title+$jq("#fromaddr").val());
	}

}
function searchfromgoogle(address){
	if(address)
	{
		currAddr=address;
	}
	else
	{
		address=currAddr;
	}
	geocoder = new GClientGeocoder();
	directions.clear();
	$jq("#othermode").hide();
	if(address)
	{
		geocoder.getLocations(
	    address,
	    function(response) {
	    	searchResponse=response;
	    	var rows=response.Placemark;
	    	if(!rows){alert("没有对应地址");return;}
	    	$jq('#rowlist').empty();
			if(rows.length>0){
			$str="<div class='rowlistline'></div>";
			$jq('#rowlist').append($str);
			}
			
	    	for(i=0;i<rows.length;i++)
	    	{
				var style="";
				if(i==0)
				{
					style+="padding-top:3px;";
				}
				if(i==rows.length-1)
				{
					style+="padding-bottom:9px";
				}
				if(i!=0&&i!==rows.length-1) {
					style="";
				}
				if(style)
				{
					style="style='"+style+"'";
				}
				$str="<div "+style+"><a href='#' class='row' onclick='showResponsePoint("+i+");return false;'>"+rows[i].address+"</a></div>";
				$jq('#rowlist').append($str);
	    	}
	    	//map.closeInfoWindow();	    	
	    });
	    
		
	}
}
function showResponsePointWrap(i){
	return function (){
		showResponsePoint(i); return false;
	}
}
function showResponsePoint(i){
	
	//读取搜索点坐标
	if(typeof(i)=="undefined")
	{
		var point=searchResultPoint;
	}
	else
	{
		var point=new GLatLng(searchResponse.Placemark[i].Point.coordinates[1], searchResponse.Placemark[i].Point.coordinates[0]);
		searchResultPoint=point;
	}
	
	directions.clear();
	$jq("#othermode").hide();
	var points=new Array();
	var ppp;
	if(areas[currPid].status==1)
	{
		ppp=new GLatLng(areas[currPid].children[currPointid].lat,areas[currPid].children[currPointid].lng);
	}
	else
	{
		ppp=new GLatLng(areas[currPid].children[currCityid].children[currPointid].lat,areas[currPid].children[currCityid].children[currPointid].lng);
	}
	if(direction)
	{
	points[0]=ppp
	points[1]=point;
	
	}
	else
	{
	points[0]=point;
	points[1]=ppp;
	
	}
	directions.loadFromWaypoints(points,  { "locale": locale,"travelMode":travelMode  });
	$jq(".modes").show();
	$jq("#othermode").show();
	$jq("#mode"+travelMode).hide();
	
}

