<?php
function curl_req($hwnd, $url, array $req = NULL, $post = false) { 
    $options = array(
        CURLOPT_FOLLOWLOCATION => 0,
        CURLOPT_COOKIEJAR => 'cookies.m2m',
        CURLOPT_HEADER => 0,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_TIMEOUT => 4,
        CURLOPT_POST => ($post) ? 1 : 0, 
        CURLOPT_FRESH_CONNECT => 1,
        CURLOPT_FORBID_REUSE => 1,
        CURLOPT_URL => ($post) ? $url : $url . (strpos($url, '?') === FALSE ? '?' : '') . http_build_query($req),
        CURLOPT_POSTFIELDS => ($post) ? http_build_query($req) : null
    );
    curl_setopt_array($hwnd, ($options)); 
    if( ! $result = curl_exec($hwnd)) { 
        trigger_error(curl_error($hwnd)); 
    }
    return $result; 
} 

$login_url = 'http://gps-tracker.com.ua/login.php';
$login_data = array('login' => 'demo', 'password' => 'accepted');

$info_url = 'http://gps-tracker.com.ua/loadevents.php';
$info_data = array(
    'param' => 'icars',
    '_dc' => mt_rand(0, 10000000000)
);

$carinfo_url = 'http://gps-tracker.com.ua/carinfo.php';
$carinfo_data = array('action' => 'load', 'carid' => '387');

$ch = curl_init();
$login = json_decode(curl_req($ch, $login_url, $login_data, true), true);

if($login['success'] == 'true') {
    $info = json_decode(curl_req($ch, $info_url, $info_data));
    output_map($info->rows);
//    echo '<pre>';
//    var_dump($info);

//    $obj = json_decode(curl_req($ch, $carinfo_url, $carinfo_data, true), true);
//    var_dump($obj);
} else {
    echo 'Login error: ' . $login['reason'];
}

curl_close($ch); 

function output_map($rows)
{
?>
<!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">
<head>
    <title>Íàø ñåðâåð</title>
    <meta http-equiv="Content-Type" content="text/html; " />
   
<script src="http://api-maps.yandex.ru/1.1/?key=************************==&modules=pmap&wizard=constructor" 
type="text/javascript"></script>
<script type="text/javascript">
    YMaps.jQuery(window).load(function () {
        var map = new YMaps.Map(YMaps.jQuery("#YMapsID-2945")[0]);
        map.setCenter(new YMaps.GeoPoint(36.253474,49.941344), 14, YMaps.MapType.MAP);
        map.addControl(new YMaps.Zoom());
        map.addControl(new YMaps.ToolBar());
        YMaps.MapType.PMAP.getName = function () { return "str1"; };
        map.addControl(new YMaps.TypeControl([
            YMaps.MapType.MAP,
            YMaps.MapType.SATELLITE,
            YMaps.MapType.HYBRID,
            YMaps.MapType.PMAP
        ], [0, 1, 2, 3]));

        YMaps.Styles.add("constructor#pmlbmPlacemark", {
            iconStyle : {
                href : "http://api-maps.yandex.ru/i/0.3/placemarks/pmlbm.png",
                size : new YMaps.Point(28,29),
                offset: new YMaps.Point(-8,-27)
            }
        });


        YMaps.Styles.add("constructor#FF3732c85Polyline", {
            lineStyle : {
                strokeColor : "FF3732c8",
                strokeWidth : 5
            }
        });
        <?
        foreach($rows as $r) {
        ?>
       map.addOverlay(createObject("Placemark", new YMaps.GeoPoint(<?=$r->Y?>,<?=$r->X?>), "constructor#pmlbmPlacemark", 
"str2"));
        <?
        }
        ?>
        /*
       map.addOverlay(createObject("Placemark", new YMaps.GeoPoint(36.260217,49.946682), "constructor#pmlbmPlacemark", 
"str3"));
       map.addOverlay(createObject("Placemark", new YMaps.GeoPoint(36.280859,49.945879), "constructor#pmlbmPlacemark", 
"str4"));
       map.addOverlay(createObject("Polyline", [new YMaps.GeoPoint(36.281331,49.944134),new 
YMaps.GeoPoint(36.276654,49.944273),new YMaps.GeoPoint(36.270517,49.944799),new YMaps.GeoPoint(36.262964,49.945713),new 
YMaps.GeoPoint(36.260002,49.946654),new YMaps.GeoPoint(36.25996,49.943913),new YMaps.GeoPoint(36.26026,49.941586),new 
YMaps.GeoPoint(36.261118,49.936379),new YMaps.GeoPoint(36.261547,49.932861),new YMaps.GeoPoint(36.261977,49.931698),new 
YMaps.GeoPoint(36.262363,49.930894),new YMaps.GeoPoint(36.252321,49.930035),new YMaps.GeoPoint(36.251162,49.930368),new 
YMaps.GeoPoint(36.249231,49.932556),new YMaps.GeoPoint(36.246827,49.933249),new YMaps.GeoPoint(36.245926,49.933415),new 
YMaps.GeoPoint(36.245111,49.933277),new YMaps.GeoPoint(36.239746,49.939149),new YMaps.GeoPoint(36.23936,49.940866),new 
YMaps.GeoPoint(36.236699,49.940644),new YMaps.GeoPoint(36.23597,49.94034)], "constructor#FF3732c85Polyline", ""));
        */
        function createObject (type, point, style, description) {
            var allowObjects = ["Placemark", "Polyline", "Polygon"],
                index = YMaps.jQuery.inArray( type, allowObjects),
                constructor = allowObjects[(index == -1) ? 0 : index];
                description = description || "";
            
            var object = new YMaps[constructor](point, {style: style, hasBalloon : !!description});
            object.description = description;
            
            return object;
        }
    });
</script>
</head>



<div id="YMapsID-2945" style="width:600px;height:400px"></div>

</html>
	

<?
}
