Page 1 of 1

CGGoogleMaps address to GLatLng [SOLVED]

Posted: Tue Jun 08, 2010 5:08 am
by antosha
Hi,
I want to draw a polyline from two dynamicaly loaded (with {cggm_add_dynpoint} plugin).
However, google API requires Lat/Long coordinates to draw a line, I need to find a way to automatically retrieve coordinates from address.

Code: Select all

<__script__ type="text/javascript">

var polyOptions = {geodesic:true};
var polyline = new GPolyline([
  new GLatLng(I NEED TO FIND THESE COORDINATES GENERATED FROM ADDRESS),
  new GLatLng(I NEED TO FIND THESE COORDINATES GENERATED FROM ADDRESS)
  ], "#f36c25", 5, 0.8, polyOptions);
map.addOverlay(polyline);
  if (GBrowserIsCompatible()) {
        map.addOverlay(polyline);
  }
</__script>
Thanks

Re: CGGoogleMaps address to GLatLng

Posted: Tue Jun 08, 2010 6:02 am
by janb
Hi

Have you looked at the CGGM GetCoordsFromAddress() function?


JanB

Re: CGGoogleMaps address to GLatLng

Posted: Tue Jun 08, 2010 7:56 am
by janb
Example:

UDT named 'cggm_getaddr' :

Code: Select all


global $gCms;
$cggm =& $gCms->modules['CGGoogleMaps']['object'];

$addr1 = 'SomeAddress 1';
$addr2 = 'SomeAddress 2';

$smarty->assign('point1',implode(',',$cggm->GetCoordsFromAddress($addr1)) );
$smarty->assign('point2',implode(',',$cggm->GetCoordsFromAddress($addr2)) );


Add to the top and to the bottom of the CGGoogleMaps template:

Code: Select all


{cggm_getaddr}
..
..
..
..
..
<__script__ type="text/javascript">
{literal}
//    map.setCenter(new GLatLng({/literal}{$MyGeoLocationLatLon}{literal}));

var polyOptions = {geodesic:true};
var polyline = new GPolyline([
  new GLatLng({/literal}{$point1}{literal}),
  new GLatLng({/literal}{$point2}{literal})
  ], "#f36c25", 5, 0.8, polyOptions);
map.addOverlay(polyline);
  if (GBrowserIsCompatible()) {
        map.addOverlay(polyline);
  }
{/literal}
</__script>
JanB

Re: CGGoogleMaps address to GLatLng

Posted: Wed Jun 09, 2010 4:40 pm
by antosha
Hi,
Thanks a lot for your reply!
However, for some reason I can't get it working.
As you told me, I created the new UDT, modified the CGGoogleMaps template and added {CGGoogleMaps} into my content page.

Now the content box where the google map is suppose to appear is blank.

Re: CGGoogleMaps address to GLatLng

Posted: Wed Jun 09, 2010 4:46 pm
by janb
Hi

Maybe a dumb question, but did you leave the original template code untouched ?
..and added {cggm_getaddr} to the top and the java-script section to the bottom (have to be after {$google_map_js})?

JanB

Re: CGGoogleMaps address to GLatLng

Posted: Thu Jun 10, 2010 6:31 am
by antosha
Well, your question wasn't that dumb, since the reason why the page was showing blank was because I defined
{cggm_poadd_dynint} tags after {CGGoogleMaps} one (it needs to be before)  :P

However now I am facing another problem, which is passing the two addresses from the two {content block} into the UDT.

What I did :

In my CMS template :

Code: Select all

{capture name=pointc1 assign=addresspoint1}
  {content block='point 1' oneline='true'}
{/capture}
{capture name=pointc2 assign=addresspoint2}
  {content block='point 2' oneline='true'}
{/capture}
{cggm_poadd_dynint map="1" name='aaa' address=$addresspoint1} {cggm_add_dynpoint map="1" name='asda' address=$addresspoint2}
{content}
CGGoogleMaps template :

Code: Select all

...
{$google_map_js}
{cggm_getaddr}

<__script__ type="text/javascript">
{literal}
//    map.setCenter(new GLatLng({/literal}{$MyGeoLocationLatLon}{literal}));

var polyOptions = {geodesic:true};
var polyline = new GPolyline([
  new GLatLng({/literal}{$point1}{literal}),
  new GLatLng({/literal}{$point2}{literal})
  ], "#f36c25", 5, 0.8, polyOptions);
map.addOverlay(polyline);
  if (GBrowserIsCompatible()) {
        map.addOverlay(polyline);
  }
{/literal}
</__script>
<!-- end display map -->
UDT:

Code: Select all

global $gCms;
$cggm =& $gCms->modules['CGGoogleMaps']['object'];

$smarty->assign('point1',implode(',',$cggm->GetCoordsFromAddress($addresspoint1)) );
$smarty->assign('point2',implode(',',$cggm->GetCoordsFromAddress($addresspoint2)) );

Now what happens, the google map shows the two points, but doesn't show the polyline and displays the following error after the Map :
Warning: implode() [function.implode]: Invalid arguments passed in  /XXXX/XX/XXXX/XXXX//lib/content.functions.php(771) : eval()'d code on line 4

Warning: implode() [function.implode]: Invalid arguments passed in /XXXX/XX/XXXX/XXXX/content.functions.php(771) : eval()'d code on line 5
If I replace GetCoordsFromAddress($addresspoint1)) ); to GetCoordsFromAddress(New York City, USA)) ); for example, the line will draw correctly.

So if I understand right, the $addresspoint1 and $addresspoint2 are for some reason not passed to the UDT...

Re: CGGoogleMaps address to GLatLng

Posted: Thu Jun 10, 2010 7:10 am
by janb
Hi

You have a typo in your template..

Code: Select all

{cggm_poadd_dynint map="1" name='aaa' address=$addresspoint1} {cggm_add_dynpoint map="1" name='asda' address=$addresspoint2}
should be

Code: Select all

{cggm_add_dynpoint map="1" name='aaa' address=$addresspoint1} {cggm_add_dynpoint map="1" name='asda' address=$addresspoint2}
I have not tested this the way you describe, but will do as soon i have time to..

JanB

Re: CGGoogleMaps address to GLatLng

Posted: Thu Jun 10, 2010 8:57 am
by janb
Hi

Maybe this could be better:
Shortened the parameter names a little bit..

In your template:

Code: Select all


{capture name=pointc1 assign=addr1}
  {content block='point 1' oneline='true'}
{/capture}
{capture name=pointc2 assign=addr2}
  {content block='point 2' oneline='true'}
{/capture}

{cggm_getaddr addr1="$addr1|Point 1" addr2="$addr2|Point 2" map="1"}
{content}

UDT cggm_getaddr

Code: Select all


global $gCms;
$cggm =& $gCms->modules['CGGoogleMaps']['object'];

$map = $params['map'];
$param1 = explode('|',$params['addr1']);
$param2 = explode('|',$params['addr2']);

$cggm->AddDynamicPoint($map,$param1[1],'',$param1[0]);
$cggm->AddDynamicPoint($map,$param2[1],'',$param2[0]);

$smarty->assign('point1',implode(',',$cggm->GetCoordsFromAddress($param1[0])) );
$smarty->assign('point2',implode(',',$cggm->GetCoordsFromAddress($param2[0])) );

CGGoogleMaps template :
(remove the UDT. ...and the map.setCenter line. That's a ref to a GeoLocation tag i use)

Code: Select all


{$google_map_js}

<__script__ type="text/javascript">
{literal}

var polyOptions = {geodesic:true};
var polyline = new GPolyline([
  new GLatLng({/literal}{$point1}{literal}),
  new GLatLng({/literal}{$point2}{literal})
  ], "#f36c25", 5, 0.8, polyOptions);
map.addOverlay(polyline);
  if (GBrowserIsCompatible()) {
        map.addOverlay(polyline);
  }
{/literal}
</__script>
<!-- end display map -->

..and in your content page just the {CGGoogleMaps} tag.

You can of course put the UDT in the content page before the CGGoogleMaps tag but then you have to remove it from the template. In that way it's more dynamic and you can have several pages with different maps, points and lines.

JanB

Re: CGGoogleMaps address to GLatLng

Posted: Fri Jun 11, 2010 11:24 pm
by antosha
Thank you so much, it works great now !
Two more things: in your code, where can I define the point icon and how can I change the point name to be the same as its address?

Re: CGGoogleMaps address to GLatLng

Posted: Sat Jun 12, 2010 9:40 am
by janb
Hi

Since we do not need to define the address twice, we can use the icon instead as the second value in the addrx parameter.
The icon name have to be defined in CGGoogleMaps.

Change the tag to:

Code: Select all

{cggm_getaddr addr1="$addr1|building" addr2="$addr2|building" map="1"}
where building is the icon name

Change UDT cggm_getaddr to:

Code: Select all

global $gCms;
$cggm =& $gCms->modules['CGGoogleMaps']['object'];

$map = $params['map'];
$param1 = explode('|',$params['addr1']);
$param2 = explode('|',$params['addr2']);

$cggm->AddDynamicPoint($map,trim($param1[0]),'',$param1[0],'','',$param1[1]);
$cggm->AddDynamicPoint($map,trim($param2[0]),'',$param2[0],'','',$param2[1]);

$smarty->assign('point1',implode(',',$cggm->GetCoordsFromAddress($param1[0])) );
$smarty->assign('point2',implode(',',$cggm->GetCoordsFromAddress($param2[0])) );

JanB

Re: CGGoogleMaps address to GLatLng

Posted: Sat Jun 12, 2010 9:43 pm
by antosha
Thank you so much ! Everything works great :D