I changed 2 dca/tl_dlh_googlemaps fields (so the save_callback to generateCoords() is now on "geocoderAddress", and "center" is now readonly to prevent user input and only show verified co-ordinates.
Code:
'center' => array
(
'label' => &$GLOBALS['TL_LANG']['tl_dlh_googlemaps']['center'],
'exclude' => true,
'search' => true,
'inputType' => 'text',
'eval' => array('maxlength'=>64, 'readonly'=>'readonly')
),
'geocoderAddress' => array
(
'label' => &$GLOBALS['TL_LANG']['tl_dlh_googlemaps']['geocoderAddress'],
'exclude' => true,
'search' => true,
'inputType' => 'text',
'eval' => array('maxlength'=>255, 'tl_class'=>'long clr'),
'save_callback' => array
(
array('tl_dlh_googlemaps', 'generateCoords')
)
),
and i changed generateCoords() to accept addresses or co-ordinates via the geocoderAddress field
In the case of no input, it centers a map on 0,0 (before any wrong maps wouldn't instantiate, now they will show as blue, because 0,0 is in the sea.
Code:
function generateCoords($varValue, DataContainer $dc)
{
if ($varValue) //if there is input in the address field then run validation on geocoderAddress field to see if it is a valid lat/long pair separated by a comma
{
$myArray = explode(',', $varValue); //split string on FIRST comma
if(count($myArray) == 2) //there are two parts... so test furthur
{
//is first part (as number) between -90 and 90 AND is second part (as number) between -180 and 180
if(((int)$myArray[0] > -90 && (int)$myArray[0] < 90) && (((int)$myArray[1] > -180 && (int)$myArray[1] < 180)))
{
//it validates as latlong - place it in "center"
$this->Database->prepare("UPDATE tl_dlh_googlemaps SET center=? WHERE id=?")
->limit(1)
->execute($varValue,$dc->id);
}
}else //not valid input as lat/long - run curl
{
$strGeoURL = 'http://maps.google.com/maps/api/geocode/xml?address='.str_replace(' ', '+', $varValue).'&sensor=false'.($dc->activeRecord->geocoderCountry ? '®ion='.$dc->activeRecord->geocoderCountry : '');
if(function_exists("curl_init"))
{
$curl = curl_init();
if($curl)
{
if(curl_setopt($curl, CURLOPT_URL, $strGeoURL) && curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1) && curl_setopt($curl, CURLOPT_HEADER, 0))
{
$curlVal = curl_exec($curl);
curl_close($curl);
$xml = new SimpleXMLElement($curlVal);
if($xml->result->geometry->location->lat != '' && $xml->result->geometry->location->lng != '') //ok curl return verified
{
$returnedLatLong = $xml->result->geometry->location->lat . ',' . $xml->result->geometry->location->lng;
//place in center field
$this->Database->prepare("UPDATE tl_dlh_googlemaps SET center=? WHERE id=?")
->limit(1)
->execute($returnedLatLong,$dc->id);
}else
{
//throw exception and error at top of page No Co-ordinates returned
$GLOBALS['TL_LANG']['ERR']['general'] = $GLOBALS['TL_LANG']['tl_dlh_googlemaps']['references']['noCoords'];
throw new Exception(sprintf($GLOBALS['TL_LANG']['tl_dlh_googlemaps']['references']['noCoords'], $varValue));
}
}
}else
{
//throw exception and error at top of page No Curl
$GLOBALS['TL_LANG']['ERR']['general'] = $GLOBALS['TL_LANG']['tl_dlh_googlemaps']['references']['noCurl'];
throw new Exception(sprintf($GLOBALS['TL_LANG']['tl_dlh_googlemaps']['references']['noCurl'], $varValue));
}
}
}
return $varValue;
}else
{
//there is no input in the address field ... Center on 0,0
$this->Database->prepare("UPDATE tl_dlh_googlemaps SET center=? WHERE id=?")
->limit(1)
->execute('0,0',$dc->id);
return $varValue;
}
}
Dunno what you think Christian, but these changes work well for me.
Of course the language files would need to change to reflect the fact theres now only one input field.
In English for example:
Code:
$GLOBALS['TL_LANG']['tl_dlh_googlemaps']['center'] = array('Verified Geo-coordinates', 'The verified geo-coordinates of the map center. READONLY.');
$GLOBALS['TL_LANG']['tl_dlh_googlemaps']['geocoderAddress'] = array('Address or Geo-coordinates', 'Enter an address or geo-coordinates (e.g. 48.856846,2.351024) to center the map on.');
I think the same system of a readonly verified field and a address/lat-long input field- at least on the markers and infowindows - would be good, as nobody really knows the lat/long of several houses in a suburb for instance, and it would be much easier to enter them as addresses than as lat/long.
I do like however the fact the markers give you the default map lat/long point initially.
anyway, just a suggestion,
cheers, murray.
Bookmarks