﻿var map = null;
var cnt=0;
pin=new Array();
x1=new Array();
y1=new Array();
x2=new Array();
y2=new Array();
var req;
var res;
var surl;
var strurl;
totaldist=new Array();
var dist;
dist=0;
var layer;
var cle;
pin=new Array();
lin=new Array();
dragFlag = false;
dragObj = null	// ドラッグ対象オブジェクト情報
function MyOnload()
{
	map = new VEMap('mymap');
	map.LoadMap(new VELatLong(35.6760, 139.7459), 16 ,'h' , false);
	map.AttachEvent('onclick', myEventTester);
	AddMyControl();
}
function AddClickablePin(pinID,location, icon_url, title, details)
{
	pin[pinID] = new VEShape(VEShapeType.Pushpin, location);
	pin[pinID].SetCustomIcon(icon_url);
	pin[pinID].SetTitle(title);
	pin[pinID].SetDescription(details);
	map.AddShape(pin[pinID]);
	map.ShowInfoBox(pin[pinID]);
	//3Dmap時は一度InfoBoxを消さないとアンカーポイントが更新されない
	map.HideInfoBox();
	map.ShowInfoBox(pin[pinID]);
}
function myEventTester(e)
{
	if(!e.rightMouseButton){return};
	var LL = e.latLong;
	if(!LL){LL=map.PixelToLatLong(new VEPixel(e.mapX,e.mapY))};
	if(cnt==0)
	{
		AddClickablePin(
		cnt,
		LL,
		'./icon_img/' +cnt+ '.gif',
		'GeOAP',
		'出発地点'
		);
		x1[cnt]=LL.Longitude;
		y1[cnt]=LL.Latitude;
	}
	if((cnt>0) && (cnt<10))
	{
		AddClickablePin(
		cnt,
		LL,
		'./icon_img/' +cnt+ '.gif',
		'経路に沿った標高グラフ',
		'GeOAPより経路・標高を取得中・・・'
		);
		x1[cnt]=LL.Longitude;
		y1[cnt]=LL.Latitude;
		strurl='&x1=' +x1[cnt-1]+ '&y1=' +y1[cnt-1]+ '&x2=' +x1[cnt]+ '&y2=' +y1[cnt]+ '&num=' +cnt+ '&num1=1'
		surl='./keirocoord_hyoukou.aspx' //経路と標高呼び出しURL
		req=createHttpRequest()
		sendRequest(onGeOAPLoaded,strurl,'GET',surl) //リクエスト実行

		function onGeOAPLoaded(req)
		{
			res=req.responseText
			var rows1=res.split("\n")
			var cols=rows1[0].split(",")
			points = new Array();
			var LL2 = pin[cols[0]].GetPoints();
			if(cols[2] != "error")
			{
				map.DeleteShape(pin[cols[0]]);
				AddClickablePin(
				cols[0],
				LL2 ,
				'./icon_img/' +cols[0]+ '.gif',
				'経路に沿った標高グラフ',
				'<div style="width:400px;height:300px"><img src="./icon_img/'+(cols[0]-1)+ '.gif"> から <img src="./icon_img/' +cols[0]+ '.gif"> までの経路距離：' +cols[1]+ 'm<BR><img src="'+cols[2].replace('localhost','dev.geoap.jp')+'"></div>'
				);
				for(var j = 0; j< rows1.length-4; j++)
				{
					var cols1=rows1[j+1].split(",")
					points[j] = new VELatLong(cols1[1]-0,cols1[0]-0);
				}
				lin[cols[0]] = new VEShape(VEShapeType.Polyline, points);
				lin[cols[0]].SetLineColor(new VEColor(255,0,0,0.8));
				lin[cols[0]].SetLineWidth(3);
				lin[cols[0]].HideIcon();

				map.AddShape(lin[cols[0]]);
			}
			else
			{
				map.DeleteShape(pin[cols[0]]);
				AddClickablePin(
				cols[0],
				LL2,
				'./icon_img/' +cols[0]+ '.gif',
				'経路に沿った標高グラフ',
				'<div style="width:400px;height:50px"><img src="./icon_img/'+(cols[0]-1)+ '.gif"> から <img src="./icon_img/' +cols[0]+ '.gif"> までの経路を特定できませんでした<BR><BR>message:' +cols[1]+
'<BR>※当体験サイトでは、簡易的に数値地図の道路データを使用しており、<BR>２点間の直線距離は25km以内に制限させていただいております。</div>'

				);
			}
		}
	}
	if(cnt<10){cnt=cnt+1}
}

function Search()
{
	req=createHttpRequest()
	function onGAdrLoaded(req)
	{

		res=req.responseText
		var rows1=res.split("\n")
		var cols=rows1[0].split(",")
		if(cols[0]!="error")
		{
			try{map.DeleteShape(pin[10]);}
			catch(err){};
			map.SetCenter(new VELatLong(cols[1],cols[0]));
			AddClickablePin(
				10,
				new VELatLong(cols[1],cols[0]) ,
				'./icon_img/x.png',
				'住所検索結果',
				document.getElementById("textbox").value
			);
		}
		else
		{
			document.getElementById("textbox").value = cols[1];
		}
	}
	var surl;
	var strurl;
	surl="./adrsearch.aspx";
	strurl='&adr=' +document.getElementById("textbox").value+ ' '
	sendRequest(onGAdrLoaded,strurl,'GET',surl)
}
function AddMyControl()
{
	var el = document.createElement("div");
	el.id = "pnl";
	el.style.top ="70px";
	el.style.left = "45px";
	el.style.width = "197px";
	el.style.height = "166px";
	el.style.border = "1px solid black";

	var el3 = document.getElementById("helpdiv")
	el3.style.visibility="visible";
	el3.style.top ="57px";
	el3.style.left = "2px";

	var el4 = document.getElementById("searchdiv")
	el4.style.top ="2px";
	el4.style.left = "2px";

	document.getElementById('searchbtn').innerHTML = '検索';

	el.appendChild(el3);
	el.appendChild(el4);
	map.AddControl(el);

	var shim = document.createElement("iframe");
	shim.id = "myShim";
	shim.frameBorder = "0";
	shim.style.position = "absolute";
	shim.style.zIndex = "1";
	shim.style.top  = el.offsetTop;
	shim.style.left = el.offsetLeft;
	shim.width  = el.offsetWidth;
	shim.height = el.offsetHeight;
	el.shimElement = shim;
	el.parentNode.insertBefore(shim, el);
}
function clearAll()
{
	map.DeleteAllShapes();
	cnt = 0;
}
function back()
{
	if(cnt > 0)
	{
		cnt = cnt - 1;
		try{map.DeleteShape(pin[cnt]);}
		catch(err){};
		try{map.DeleteShape(lin[cnt]);}
		catch(err){};
	}
}
