PHP Classes
elePHPant
Icontem

File: samples/panMapNavigation.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Carlos Machado  >  MapPoint PHP API  >  samples/panMapNavigation.php  >  Download  
File: samples/panMapNavigation.php
Role: Example script
Content type: text/plain
Description: ConvertToLatLong test
Class: MapPoint PHP API
Access MapPoint Web services
Author: By
Last change:
Date: 9 years ago
Size: 4,984 bytes
 

Contents

Class file image Download
<?php
require_once('../mappoint/ClassLoader.php');
session_start();

function
getMap(ViewByHeightWidth $view, $zoom) {
   
$global = $_SESSION['mappoint'];
   
$images = null;
   
$views = array($view);

   
$mapSpec = new MapSpecification();
   
$mapSpec->DataSourceName = "MapPoint.NA";
   
$mapSpec->Views = $views;
   
$mapSpec->Options = new MapOptions();
   
$mapSpec->Options->Format = new ImageFormat();
   
$mapSpec->Options->ReturnType = MapReturnType::$ReturnUrl;
   
$mapSpec->Options->Format->Height = 400;
   
$mapSpec->Options->Format->Width = 400;
   
$mapSpec->Options->Zoom = $zoom;


    try {
       
$images = $global->RenderService->GetMap($mapSpec);

       
$view = new ViewByHeightWidth();
       
$view->CenterPoint = $images[0]->View->ByHeightWidth->CenterPoint;
       
$view->Height = $images[0]->View->ByHeightWidth->Height;
       
$view->Width = $images[0]->View->ByHeightWidth->Width;

       
$_SESSION['view'] = $view;
        return
$images[0]->Url;
    } catch (
SoapFault $e) {
        die(
$e->faultstring);
    }
}

function
pan($x, $y) {
   
$renderService = $_SESSION['mappoint']->RenderService;
   
$currentView = $_SESSION['view'];

   
// Find the new center pixel of the map
   
$pixels = array();
   
$pixels[] = new PixelCoord();
   
$pixels[0]->X = 200 - $x;
   
$pixels[0]->Y = 200 - $y;

   
// ConvertToLatLong will return the new center LatLong
   
$coords = null;

    if(
$currentView != null) {
        try {
           
// Get the corresponding latlong for this new pixel
           
$coords = $renderService->ConvertToLatLong($pixels, $currentView,
           
400, 400);

            if(
$coords[0] == null) {
               
// Display error message.
                // Happens if center pixel is an invalid LatLong
                // Most likely caused by dragging when zoomed out to
                // the point where space is visible
               
die('Invalid center point');
            } else {
               
// Update the current view
               
$currentView->CenterPoint = $coords[0];
               
// Set the current map to the new zoomed map
               
$_SESSION['image_url'] = getMap($currentView, 1);
            }

           
$_SESSION['view'] = $currentView;
        }
        catch(
SoapFault $e) {
           
// Display error message on web page
           
die($e->faultstring);
        }
    }
}

function
zoom($zoom) {
   
$currentView = $_SESSION['view'];
   
    try {
       
$_SESSION['image_url'] = getMap($currentView, $zoom);
    } catch (
SoapFault $e) {
        die(
$e->faultstring);
    }
}

if (!isset(
$_SESSION['image_url'])) {
   
$global = $_SESSION['mappoint'];

   
// Display a map of seattle
   
$currentView = new ViewByHeightWidth();
   
$currentView->CenterPoint = new LatLong();
   
$currentView->CenterPoint->Latitude = 47.599803715252847;
   
$currentView->CenterPoint->Longitude = -122.33455797096831;
   
$currentView->Height = 43;
   
$currentView->Width = 57;
   
$_SESSION['image_url'] = getMap($currentView, 1);
}

if (isset(
$_POST['Button2'])) {
   
//Zoom out
   
zoom(2);
} elseif (isset(
$_POST['Button1'])) {
   
//Zoom in
   
zoom(0.5);
} elseif (isset(
$_POST['DragX'])) {
   
//Pan
   
pan($_POST['DragX'], $_POST['DragY']);
}

?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Pan Map Navigation</title>
<script language="javascript">
startX = 0;
startY = 0;
lastX = 0;
lastY = 0;

function BeginDrag(e) {
    document.all.Panel.style.clip = "rect(0px 406px 406px 0px)";
    lastX = startX = e.clientX;
    lastY = startY = e.clientY;
}

function Dragging(e) {
    dx = e.clientX - lastX;
    dy = e.clientY - lastY;

    newX = parseInt(document.forms[0].Image1.style.left) + dx;
    newY = parseInt(document.forms[0].Image1.style.top) + dy;
    document.forms[0].Image1.style.left = newX + "px";
    document.forms[0].Image1.style.top = newY + "px";

    lastX = e.clientX;
    lastY = e.clientY;
}

function EndDrag(e) {
    dx = e.clientX - startX;
    dy = e.clientY - startY;

    document.forms[0].DragX.value = dx;
    document.forms[0].DragY.value = dy;
    document.forms[0].submit();
}
</script>
</head>
<body>
<div style="font-size: small"><b>Note</b>: The pan feature will only IE.</div>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input name="DragX" id="DragX" type="hidden" />
<input name="DragY" id="DragY" type="hidden" />
<b>&nbsp;Drag the map to pan.</b>
<div id="Panel" style="border-style: Double; height: 400px; width: 400px; left: 0px; top: 100px; z-index: 100; position: absolute">
<img id="Image1" name="Image1" ondrag="Dragging(event)" ondragstart="BeginDrag(event)" ondragend="EndDrag(event)" src="<?php echo $_SESSION['image_url']; ?>" width="400px" height="400px" border="0" style="left: 0px; top: 0px; z-index: 101; position: absolute" />
</div>
<br />
<table border="0">
    <td align="center">
        <input type="submit" name="Button2" value="Zoom Out" id="Button2" />
        <input type="submit" name="Button1" value="Zoom In" id="Button1" />
    </td>
</tr>
</table>
</form>
</body>
</html>