<? 
$tnode 
$_REQUEST['tnode'];
$fnode $_REQUEST['fnode'];
$index $_REQUEST['index'];
if(
$tnode && $fnode) {

  
// url for get Data
  
$urlGetRoute 'http://mmmap15.longdo.com/mmroute/GetRoute?fnode='.$tnode.'&tnode='.$fnode.'&mode=t&type=17&locale=th&key=4649992ddf13299b6c49ef8e8c487d6d';

  
$result file_get_contents($urlGetRoute);
  list(
$fdistance$tdistance$id$time) = explode(","$result);
  
  
/**
   * เอา id (object array ตัวที่ 2) ไปดึง Geom โดยส่งเข้าฟังก์ชัน 
   **/
  
$geom setGeom($id$result);
  echo 
$result 'allGeom='.$geom.';result='.$result.';indexroute='.$index.';exportRouteLine(allGeom, indexroute);';
  exit();
}
function 
setGeom($id$result) {
  global 
$tnode$fnode;
  
//echo $result;
  
list($tlat$tlon) = explode('+'$tnode);
  list(
$flat$flon) = explode('+'$fnode);
  
$arrT =array($tlon$tlat);
  
$arrF =array($flon$flat);
  
/**
   * paramiter to call
   * - id of route
   * - level of zoom is fix to 3
   * - var is result valiable
   * - callback is to function to call 
   * Exp : http://mmmap15.longdo.com/mmroute/GetGeom?id=16&level=3&var=result&callback=test(result)               
   **/
  
$level 2;
  
$urlGetGeom 'http://mmmap15.longdo.com/mmroute/GetGeom?level='.$level.'&id='.$id.'&key=4649992ddf13299b6c49ef8e8c487d6d'
    
  
$allGeom file_get_contents($urlGetGeom); 

  
$resultList json_decode($result);
  
$allList json_decode($allGeom);
  if(
$resultList[0][0] != "" || $resultList[0][0] != null$resultList $resultList[0];
  
    
$road;
    
$turn;
    
$count 0;
    
$tempRoute = array('road'=> array(), 'type'=>0);
    
$allGeom = array();
    
$allGeom[0][] = $arrT;
    foreach(
$allList as $key =>$value) {
    
$road $value;
    foreach(
$road as $val) {
      
$allGeom[0][] = $val;  
    }
     
$resultList_ = (object)$resultList[$key+4]; 
     
$turn $resultList_->t
     if (
$turn 6) continue;
     
     if (
$turn == 9) {
        
$allGeom[++$count] = $tempRoute;
        
$tempRoute = array('road'=> array(), 'type'=>0);
        continue;
      }
      
$tempRoute['type'] = $turn;
            foreach(
$road as $val) {
        
$tempRoute['road'][] = $val;   
      }
  }
  
$allGeom[0][] = $arrF;
  return 
json_encode($allGeom);
}

include(
"config.php"); 
?>
<!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" lang="th" xml:lang="th">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>Route Suggestion Query String version</title>
  <style type="text/css">
    h2{ margin: 10px 0px;}
    h3{ margin: 8px 0px;}
    #mmmapDiv { position:absolute; left: 0px; top: 0px; margin: 5px }
    #mmrouteDivDetail { position:absolute; right: 0px; top: 0px; width: 250px; overflow:auto; margin-top: 10px; font-size:14px;}
  </style>
  <script type="text/javascript" src="http://<?=$mmmap_server?>"></script>
  <script type="text/javascript"><!--
    var mmmap;
    var pointFactory = [[18.04913,99.81259], [15.17943,99.69784], [15.20178,104.86326], [11.53027,99.62365]];
    var pointStart = [13.72947,100.53587];
    var colorRoute = [['#EF630F', 'สีส้ม'], ['#0CCF14', 'สีเขียว'], ['#0081AF', 'สีฟ้า'], ['#FF00CA', 'สีชมพู'], ['#000000', 'สีดำ']];
    
    function init() {
      window.onresize = resize;
    
      mmmap = new MMMap(document.getElementById('mmmapDiv'), 13.767734, 100.5351375, 12, 'gray');
      var canvas = mmmap.addControl('canvas');
      canvas.addButton('measure');
      canvas.addButton('poly');
      canvas.addButton('clear'); 
      
      resize();
      drawCustomDivOnMap(); 
      drawCustomDivPin();
    }
  
    function resize(){
        chkWinSize();
        var newwidth = parseInt(ww) - 275;
        var newheight = parseInt(wh);
        mmmap.setSize(newwidth, newheight);
        mmmap.rePaint();
    }
    
    function drawCustomDivOnMap() {      
      for(var i =0; i< pointFactory.length; i++) {
        var factory = document.createElement("img");
        factory.src = 'images/factory.png';
        factory.latitude = pointFactory[i][0];
        factory.longitude = pointFactory[i][1];
        factory.zIndex = 501;
        var attributes = {
          "centerOffsetX" : "10px",
          "centerOffsetY" : "20px"
        };
        var customdiv_id = mmmap.drawCustomDiv(factory, pointFactory[i][0],pointFactory[i][1], 'my factory', attributes);
      }
    } 

    function drawCustomDivPin() {
        pointStart[0] = mmmap.centerLat();
        pointStart[1]= mmmap.centerLong();       
        var imageObject = document.createElement("img");
        imageObject.src = "http://map.longdo.com/mmmap/images/pin_mark.png";
        
        imageObject.onmouseup = divMouseup;
        imageObject.zIndex = 999;
        var attr = {"centerOffsetX" : "11px", "centerOffsetY" : "45px", "draggable" : true};
        var customdivid = mmmap.drawCustomDiv(imageObject, pointStart[0], pointStart[1], "Drag to change location", attr);
        mmmap.rePaint(); 
        divMouseup ();  
    }

    function divMouseup () {
        mmmap.removeAllVectors('line');
        document.getElementById('mmrouteDiv').innerHTML = "";
        setTimeout(function () {
          var lat = mmmap.currentmarkerlat , lon = mmmap.currentmarkerlon;
          if(typeof lat === "undefined" && typeof lon === "undefined") {
            lat = mmmap.centerLat();
            lon = mmmap.centerLong();      
          }
          pointStart[0] = lat, pointStart[1] = lon;
          var points_ = pointFactory.slice();
          points_[points_.length] = pointStart; 
          mmmap.autoMoveAndSetAppropriateZoom(mmmap.swapLatLonPointsInArray(points_));
          
          
          for(var i =0; i<pointFactory.length; i++) { 
            var strJsonRequest = 'index-route-php.php?tnode='+lat+'%2B'+lon+'&fnode='+pointFactory[i][0]+'%2B'+pointFactory[i][1]+'&index='+i+'&callback=divMousedown(result, indexroute)'; 
            loadJSON(strJsonRequest);
          }
        }, 1);
    }

    function exportRouteLine(resultJSON) { 
      eval(resultJSON);  
      document.getElementById('mmrouteDiv').innerHTML += '<span class="routeTime" style="color:'+colorRoute[indexroute][0]+';"><b>'+colorRoute[indexroute][1]+'</b>: เวลารวม '+setFormatTime(result[3])+'</span><br />';
        try {
          if (typeof allGeom === 'undefined' || !allGeom[0]) return;
              var polygon = new MMLine(mmmap);
                polygon.setMode('line');
              polygon.setLineOpacity(0.8);
              polygon.setLineWidth(4);
                polygon.setLineColor(colorRoute[indexroute][0]); 
              polygon.setSwapShapeOnMouseover(false);
                polygon.setHandler({ 'onmouseover' : function() {this.lineobject.setLineWidth(7); this.lineobject.rePaint();}, 'onmouseout' : function() {this.lineobject.setLineWidth(4); this.lineobject.rePaint();}});
                polygon.setPointsLongLat(allGeom[0]);
              
                for (var i = 1; i < allGeom.length; i++) {
                    polygon = new MMLine(mmmap);
                    polygon.setMode('line');
                polygon.setLineOpacity(0.8);
                polygon.setLineWidth(4);
                polygon.setSwapShapeOnMouseover(false);
                    polygon.setLineColor(colorRoute[indexroute][0]);
                  polygon.setHandler({ 'onmouseover' : function() {this.lineobject.setLineWidth(7); this.lineobject.rePaint();}, 'onmouseout' : function() {this.lineobject.setLineWidth(4); this.lineobject.rePaint();}});
            polygon.setPointsLongLat(allGeom[i].road);
                }
        } catch (err) {
      }    
    }
    
    function setFormatTime(TotalMinutes) {
        if(TotalMinutes == 0) return false;
        var t = parseInt(TotalMinutes);
        var days = parseInt(t/86400);
        t = t-(days*86400);
        var hours = parseInt(t/3600);
        t = t-(hours*3600);
        var minutes = parseInt(t/60);
        t = t-(minutes*60);
        var content = "";
        if(days)content+=days+' วัน';
        if(hours||days){ if(content)content+=", "; content+=hours+' ชม.'; }
        if(minutes > 0){content+=" "; content+=minutes+' นาที';}
        return content; 
    }
--></script>
</head>
<body onload="init();">
  <div id="mmmapDiv"></div>
  <div id="mmrouteDivDetail">
      <h2>Demo Route to Multiple Locations with PHP</h2>
      <h3>วิธีใช้</h3>
      เลื่อนหมุดสีแดงไปมาเพื่อเปลี่ยนตำแหน่ง
      <h3>ผลลัพธ์</h3>
      <div id="mmrouteDiv"></div>
  </div>
</body>
</html>