Open($userInfo); $address = $_GET['address'] . ', Sheboygan, WI'; // Make the request to geocoder.us passing the address. $url = 'http://rpc.geocoder.us/service/xmlrpc'; $client = new fXmlRpc\Client($url); $response = $client->call('geocode', array($address, true)); // Stuff the response into a new variable: $nested_array = $response; // For some reason (presumably a good one), geocoder.us responds with a nested // array. We need to extract the inner array from the outer and stuff it into a // new variable: $geoarray = array_pop($nested_array); // The $success variable is intended for error handling, but since geocoder.us // will send an error response to invalid queries (thus flagging $success to 1), // we can't use this to check for errors. Instead, we'll check the values in the // array and deem success or failure based on that. if (NULL != $geoarray && array_key_exists('lat', $geoarray) && array_key_exists('long', $geoarray)) { // The geocode successfully returned a location. $lat = $geoarray['lat']; $long = $geoarray['long']; // Build an address1 and address2 variables from the result. $address1 = $geoarray['number'] . ' ' . (strlen($geoarray['prefix']) > 0 ? $geoarray['prefix'] . ' ' : '') . (strlen($geoarray['street']) > 0 ? $geoarray['street'] . ' ' : '') . (strlen($geoarray['type']) > 0 ? $geoarray['type'] . ' ' : '') . (strlen($geoarray['suffix']) > 0 ? $geoarray['suffix'] . ' ' : ''); $address2 = $geoarray['city'] . ', ' . $geoarray['state'] . ' ' . $geoarray['zip']; echo ''; echo ''; echo ''; echo ''; echo ''; // Create ResourceService and FeatureService objects and use // the ResourceService object to open the map instance in // session state. $resourceService = $siteConnection->CreateService(MgServiceType::ResourceService); $featureService = $siteConnection->CreateService(MgServiceType::FeatureService); $map = new MgMap($siteConnection); $map->Open('Sheboygan'); // Check the map for the AddressMarker layer. If it does not // exist then create a feature source to store address results // and a layer to display them. $addressMarkerDataResId = new MgResourceIdentifier("Session:" . $mgSessionId . "//AddressMarker.FeatureSource"); $addressLayer = GetLayerByName($map, 'AddressMarker'); if ($addressLayer == null) { CreateAddressMarkerFeatureSource($featureService, $addressMarkerDataResId); $addressLayer = CreateAddressMarkerLayer($resourceService, $addressMarkerDataResId, $mgSessionId); $map->GetLayers()->Insert(0, $addressLayer); } else if ($addressLayer->GetVisible()) { // If the layer exists and is visible, then display the // previous results. EmitAddressResults($featureService, $addressMarkerDataResId, $mgSessionId); } // Insert the results of the Geo-Code into the temporary // feature source and ensure the address marker layer // is visible. $geometryReaderWriter = new MgAgfReaderWriter(); $geometryFactory = new MgGeometryFactory(); $addrPoint = $geometryFactory->CreatePoint($geometryFactory->CreateCoordinateXY((double)$long, (double)$lat)); $properties = new MgPropertyCollection(); $properties->Add(new MgStringProperty('Address', $address1)); $properties->Add(new MgGeometryProperty('Location', $geometryReaderWriter->Write($addrPoint))); $commands = new MgFeatureCommandCollection(); $commands->Add(new MgInsertFeatures('AddressMarker', $properties)); $featureService->UpdateFeatures($addressMarkerDataResId, $commands, false); $addressLayer->SetVisible(true); $addressLayer->ForceRefresh(); $map->Save(); $success = true; } else { echo ''; $success = false; } } catch (MgException $e) { echo $e->GetExceptionMessage(); echo '
'; echo $e->GetDetails(); } ?>
 Find Address
Search Results
'; echo ' ' . $address1 . '
' . $address2 . '
Longitude: ' . $long . '
Latitude: ' . $lat . '
Address not found: ' . $address . '
Geocoding Provided by: geocoder.us.
Locative Tecnologies offers the free geocoder.us web service for non commercial purposes. If you wish to use this geocoding web service in an production application you must sign up for a commercial license. Click the link above for more information.