Jayant's Blog

Tuesday, October 21, 2008

The Mobile Web

So recently the guys in the ArcGIS Mobile team (Fred, Jeff & Myles) asked me whether I could put together a blog posting about integrating the JSAPI to work in mobile browsers. So here's a first of several blog posts on The Mobile Web. If you're interested in the bringing your ArcGIS Server content to mobile devices, watch the the ArcGIS Mobile Blog.

This particular post simply shows how to make REST calls and doesn't have any JSAPI, but this one will work on any mobile browser, even IE Mobile.

You can find the page hosted here. So whip out that mobile phone of yours and go to http://ecodevil.net/dev/blog/basicMap.php and check it out. Click the +/- to zoom in/out and the N/E/S/W to pan in the specific direction.

Labels: , ,

Wednesday, August 13, 2008

Using Php with ArcGIS Server 9.3 REST (v0.03)

So here's another sample. I have a pre-populated JSON file (addresses.txt) with a bunch of addresses to be geocoded.

{
  "addresses": [
    { "name":"ESRI", "street":"380 New York St", "city":"Redlands", "state":"CA", "zip":92373 },
    { "name":"Google", "street":"1600 Amphitheatre Parkway", "city":"Mountain View", "state":"CA", "zip":94043 },
    { "name":"Yahoo", "street":"701 First Avenue", "city":"Sunnyvale", "state":"CA", "zip":94089 },
    { "name":"Microsoft", "street":"One Microsoft Way", "city":"Redmond", "state":"WA", "zip":98052 }
  ]
}


This code shows how to load up the address list from the JSON file. I tried naming the file as .json, but am guessing since my json mime-type has not been set correctly, it wasn't loaded properly.

<?php
$url = "addresses.txt";
$response = file_get_contents($url);
$json = json_decode($response);
?>

Next is the code to take the address, build the request and use the geocoding service on sample server to geocode the addresses.

<?php
$geocodeUrl = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" . "Locators/ESRI_Geocode_USA/GeocodeServer/" . "findAddressCandidates?f=json";
foreach($json->addresses as $address) {
  $url = str_replace(" ", "+", $geocodeUrl . "&Address=" . $address->street . "&City=" . $address->city . "&State=" . $address->state . "&Zip=" . $address->zip);

  $response = file_get_contents($url);
  $candidates = json_decode($response);
?>

Next filter the address candidates and display only those candidates who's score is 100. We simply print these.

<?php
  $html = "Address candidate(s) for (" . $address->name . "\n<ul>\n";
  foreach($candidates->candidates as $candidate) {
    if ($candidate->score == 100) {
      $html .= "<li>" . $candidate->address . " (" . $candidate->location->x . ", " . $candidate->location->y . ")</li>";
    }
  }
  $html .= "</ul>\n";
  echo $html;
}
?>

Labels: ,

Saturday, August 09, 2008

Using Php with ArcGIS Server 9.3 REST (v0.02)

One of the powers of using GIS is that in addition to tabular data you also have access to spatial data as part of each record. So in the following Php code, I will query to find 'Los Angeles' and simple display the result with the coordinates of the city.

<?php
$url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services" . "/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0/query" . "?f=json&where=CITY_NAME='Los+Angeles'";
$response = file_get_contents($url);
$json = json_decode($response);

$la = $json->features[0];
echo $la->attributes->CITY_NAME . " (" . $la->geometry->x . ", " . $la->geometry->y . ")";
?>

Labels: ,

Using Php with ArcGIS Server 9.3 REST

The latest release of ArcGIS Server now supports REST access to the services. The best thing, it returns JSON. And at Php 5.2, there is support for JSON. So how simple is it to request an process a response?

Well, first use file_get_contents to call the REST endpoint. Next decode the JSON response as an object using json_decode. So here's a really simple sample to get a map image. Although I can't really see why you would use this rather than using the ArcGIS JavaScript API.

<?php
$url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services" . "/Specialty/ESRI_StateCityHighway_USA/MapServer/export?f=json";
$response = file_get_contents($url);
$json = json_decode($response);

echo "<img src='" . $json->href . "' width='" . $json->width . "' height='" . $json->height . "' />";
?>

Labels: ,