<apex:page showHeader="false" standardStylesheets="false">
<HEAD>

    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />

<script type="text/javascript" src="/js/functions.js"></script>
<script src="/soap/ajax/17.0/connection.js"></script>
<script src="/soap/ajax/17.0/apex.js" type="text/javascript"></script>

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">

var _area;
var _devid;
var _strong;
var _hist;
var _range;
var _automove;
var _large;
var _int;
var _demo;
var _sessionid;

var _map;
var _markers = [];
var _myMarker;
var _paraMarker = [];
var _pos;
var _lastPos;
var _alt = 0;
var _lastAlt = 99999;

<!-- ※使用されていない※ -->
<!-- グローバルオブジェクトの情報を入手する -->
<!-- apexドキュメントをdescribeGlobalで検索→サンプルコード参考の事 -->
function doDescribeGlobal(){
    try{
        var dgResults = sforce.connection.describeGlobal();
    } catch (e) {
        sforce.debug.open();
        sforce.debug.log(e);
    }
    return dgResults;
}

<!-- ※使用されていない※ -->
<!-- グローバルSObjectの情報を入手する -->
<!-- apexドキュメントをdescribeSObject検索→サンプルコード参考の事 -->
function doDescribeSObject(entity){
    try{
        var dso = sforce.connection.describeSObject(entity);
    }catch (e) {
        sforce.debug.open();
        sforce.debug.log(e);
    }
    return dso;
}

function getGetValue( key ) {
    var value   = null;
    var query   = window.location.search.substring(1);
    var params  = query.split('&');
    for ( var i = 0; i < params.length; i++ ) {
        var item    = params[i].split('=');
        if ( key == item[0] ) {
            value   = item[1];
            break;
        }
    }
    return value;
}

function interval() {
    try {
        if ( _debug )
            alert("interval() area:" + _area + " devid:" + _devid + " pos:" + _pos.lat()+ " lon:" + _pos.lng() + " alt:" + _alt + " inc:" + (_alt - _lastAlt) + " hist:" +_hist + " range:" + _range + " strong:" + _strong);
        var ret;
        ret = sforce.apex.execute("ThermalMap","add",
              {a:_area,b:_devid,c:_pos.lat(),d:_pos.lng(),e:_alt,f:(_alt - _lastAlt),g:_hist,h:_range,i:_strong});
        if ( _debug )
            alert("件数 " + ret.length);
    } catch(e) {
        if ( _debug )
            alert("Exception in sforce.apex.execute() " + e);
        sforce.debug.trace = true;
        sforce.debug.open();
        sforce.debug.log(e);
    }
    try {
        if (_markers) {
            for (i in _markers) {
                _markers[i].setMap(null);
            }
            _markers.length = 0;
        }
        for (i = 0; i < ret.length; i++) {
            var pos = new google.maps.LatLng(ret[i].lat__c, ret[i].lon__c);
            if ( ret[i].inc__c == -99 ) {
                if ( _devid != ret[i].devid__c ){
//                    alert("para " + (i + 1) + "/" + ret.length + " area:" + ret[i].area__c + " devid:" + ret[i].devid__c + " lat:" + ret[i].lat__c + " lon:" + ret[i].lon__c + " alt:" + ret[i].alt__c);
                    var img = "http://thermalmap.appspot.com/img/para.gif";
                    var marker = new google.maps.Marker({
                        position: pos,
                        map: _map,
                        icon: img
                    });
                    marker.setMap(_map);
                    if (_large) {
//                        alert("large " + (i + 1) + "/" + ret.length + " area:" + ret[i].area__c + " devid:" + ret[i].devid__c + " lat:" + ret[i].lat__c + " lon:" + ret[i].lon__c + " alt:" + ret[i].alt__c);
                        var info_opt = { content: ret[i].devid__c + " 高度:" + ret[i].alt__c };
                        var info = new google.maps.InfoWindow(info_opt);
                        info.open(_map, marker);
                    }
                    _markers.push(marker);
                }
            } else {
//                alert("thermal " + (i + 1) + "/" + ret.length + " area:" + ret[i].area__c + " devid:" + ret[i].devid__c + " lat:" + ret[i].lat__c + " lon:" + ret[i].lon__c + " alt:" + ret[i].alt__c);
                var img = "http://thermalmap.appspot.com/img/thermal.gif";
                var marker = new google.maps.Marker({
                    position: pos,
                    map: _map,
                    icon: img
                });
                marker.setMap(_map);
                _markers.push(marker);
            }
        }
//        alert("interval 4 " + ret);
        _lastPos = _pos;
        _lastAlt = _alt;
    } catch(e) {
        if ( _debug )
            alert("Exception in interval() " + e);
        sforce.debug.trace = true;
        sforce.debug.open();
        sforce.debug.log(e);
    }
}

function initialize() {
    try {
        _area = getGetValue('area');
        _devid = getGetValue('devid');
        _strong = Number(getGetValue('strong'));
        _hist = Number(getGetValue('hist'));
        _range = Number(getGetValue('range'));
        _automove = getGetValue('automove');
        _large = getGetValue('large');
        _int = Number(getGetValue('int'));
        _demo = getGetValue('demo');
        _debug = getGetValue('debug');
        if ( _debug )
            alert('area:' + _area + ' devid:' + _devid + ' strong:' + _strong + ' hist:' + _hist + ' range:' + _range + ' automove:' + _automove + ' large:' + _large + ' int:' + _int + ' demo:' + _demo + ' sessionid:' + _sessionid + ' debug:' + _debug);
    
        var myOptions = {
            zoom: 17,
            mapTypeId: google.maps.MapTypeId.HYBRID,
            streetViewControl: false
        }
        _map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
        // 精度UPあり
        var position_options = {
            enableHighAccuracy: true
        };
    
        // 現在位置情報を取得
        navigator.geolocation.watchPosition(function(position) {
            var myLatlng;
            if ( position.coords.latitude ){
                myLatlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            } else {
                myLatlng = new google.maps.LatLng(35.685361, 139.753082);
            }
            if ( !_pos || _automove ){
                _map.setCenter(myLatlng);
            }
            _pos = myLatlng;
            if ( !position.coords.altitude ){
                _alt = 0;
            } else if ( position.coords.altitude == "null" ){
                _alt = 0;
            } else {
                _alt = position.coords.altitude;
            }
    
            if (_myMarker) {
                _myMarker.setMap(null);
            }
    
            // マーカーの表示
            var marker = new google.maps.Marker({
                position: myLatlng,
                map: _map,
                icon: "http://www.amy.hi-ho.ne.jp/para/img/para.gif"
            });
            _myMarker = marker;
        }, null, position_options);
    
        sforce.connection.init("{!$API.Session_ID}", "{!$Api.Partner_Server_URL_140}");
    
        setInterval("interval()", _int * 1000);
    } catch(e) {
      alert("Exception in initialize() " + e);
      sforce.debug.trace = true;
      sforce.debug.open();
      sforce.debug.log(e);
    }
}

</script>

<style type="text/css">
html, body {
    height: 100%;
    margin: 0;
    padding: 0;
    }

#map_canvas {
    height: 100%;
    }
</style>

</HEAD>
<BODY onload="initialize()">
    <div id="map_canvas"></div>
</BODY>
</apex:page>
