using address attribute in google map block?

Permalink
I found this on one of the discussions here. I want to use this in my custom page template and I already assigned address attribute to page object. I think it will work but, how can I delete country from the output of $rentaladdress attribute?
<?php
$bt_map = BlockType::getByHandle('google_map');
$bt_map->controller->title = 'my_title';
$api_key = 'my_api_key'; // get this from Google
$bt_map->controller->api_key = $api_key;
$location = $rentaladdress; // street address, city, ST
$geoCodeHelper = new GoogleGeoCodeHelper( $api_key );
$geoCodeHelper->loadGeoCodeData($location);
$coords=$geoCodeHelper->getCoords();
$bt_map->controller->latitude = floatval($coords[1]);
$bt_map->controller->longitude = floatval($coords[0]);
$bt_map->controller->zoom = 10; // 0 - 17, 17 is closest
$bt_map->render('view');
?>


This is the output of $rentaladdress
422 Jane Rd. Apt 7 Troy, Alabama 36079 United States


Thanks

ceyhuncenger
 
DWD replied on at Permalink Best Answer Reply
Hey ceyhuncenger,

A quick hack would be to use str_replace() assuming the country would always be United States. Just replace
$location = $rentaladdress; // street address, city, ST

with
$location = str_replace('United States', '', $rentaladdress); // street address, city, ST



HTH
Dave
ceyhuncenger replied on at Permalink Reply
ceyhuncenger
Thanks a lot :)
I also figured out how to get each lines;
here it is, someone may want to use it.
<?php
$page = Page::getCurrentPage();
$rentaladdress = $page->getCollectionAttributeValue('your_address_attribute_handle');
$ra1 = $rentaladdress->getAddress1();
$ra2 = $rentaladdress->getAddress2();
$racity = $rentaladdress->getCity();
$rastpr = $rentaladdress->getStateProvince();
$racountry = $rentaladdress->getCountry();
$razip = $rentaladdress->getPostalCode();
echo $ra1; echo $ra2; echo $racity; echo $rastpr; echo $racountry; echo $razip;
?>
DWD replied on at Permalink Reply
No worries.

Glad you worked it out.

Dave
ceyhuncenger replied on at Permalink Reply
ceyhuncenger
Not yet. All because of lack of my php.
now how to put them together :) ?

$location = and what (I have $ra1, $ra2, and $racity)

how i am gonna put them together?

sorry :( Help please...
DWD replied on at Permalink Reply
My apologies.

Basically all you need to do is put the strings together.
So to format the address like:
422 Jane Rd. Apt 7 Troy, Alabama 36079
Your code would look like this:
$location = $ra1 . ' ' . $ra2 ' ' . $racity . ', ' . $rastpr . ' ' . $razip;


HTH
Dave
ceyhuncenger replied on at Permalink Reply
ceyhuncenger
Thank you so much Dave.
That printed: 422 Jane Road Apt 7, Troy, AL
which is what I need.
But I am still getting error alert: "Cannot read property 'Lat,Lng' of undefined" in chrome
no error alert, no map in ie7, google is not defined error in firefox4

Do you have any ideas?
DWD replied on at Permalink Reply
I may be able to.

The error that is showing is not a browser problem, but a javascript problem. Basically it seems from the error that the lat (latitude) and lon (longitude) is not defined or empty.

Just to be sure did you add "your api key" in the code block?
$api_key = 'my_api_key'; // get this from Google


If so you are going to have to do some troubleshooting to make sure the lat and lon is being populated properly.

Dave
ceyhuncenger replied on at Permalink Reply
ceyhuncenger
I checked the apikey 2 more times , but it is correct. What I figured it out is, even if I want to add google map block with regular way from frontend, it gives me "undefined" error, even it doesn't require api key.

Anyways, when I add from the template, refresh the page, and look at the html, it prints this which the latlng has already included;

<style type="text/css">
    .googleMapCanvas{ width:100%; border:0px none; height: 400px;}
</style>
    <script type="text/javascript"> 
    function googleMapInit() { 
        try{
            var latlng = new google.maps.LatLng(31.7794722, -85.9589493);
            var mapOptions = {
              zoom: 10,
              center: latlng,
              mapTypeId: google.maps.MapTypeId.ROADMAP,
              streetViewControl: false,
              mapTypeControl: false
            };
            var map = new google.maps.Map(document.getElementById('googleMapCanvas'), mapOptions);



in view.php bID is written in some parts of the code, but the one I sent doesn't have anything about bID. Does that cause the problem?
DWD replied on at Permalink Reply
The $bID is just a variable, which I am assuming is sequentially created when adding a block the normal way. It changes for each block, so if you have 10 maps on page, you have 10 different functions, one for each map, otherwise they would all be the same.

And unfortunately I am a PHP guy and not a JS guy, but I added a block to one of my demo sites and found the page source to be OK compared to yours, minus the generated $bID, mine was 45.
I will post my code at the bottom so you may compare, but I think there is a problem with the javascript, somewhere, maybe in another file that you made a change to when testing? Not sure?

Also since your initial question was solved, it'd probably best to start a new thread, with your new problem and mark this one as solved. That way you can attract someone more familiar with the Google Maps block, to help you further.

But if you need PHP help in the future, feel free to PM me, and I'll see what I can do.

Dave

My source as promised:
<style type="text/css"> 
   .googleMapCanvas{ width:100%; border:0px none; height: 400px;}
   </style> 
   <script type="text/javascript"> 
   function googleMapInit45() { 
      try{
         var latlng = new google.maps.LatLng(31.7794722, -85.9589493);
          var mapOptions = {
            zoom: 10,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            streetViewControl: false,
            mapTypeControl: false
         };
          var map = new google.maps.Map(document.getElementById('googleMapCanvas45'), mapOptions);
ceyhuncenger replied on at Permalink Reply
ceyhuncenger
Thank you so much for your help
dwayneparton replied on at Permalink Reply
dwayneparton
<?php
$page = Page::getCurrentPage();
$rentaladdress = $page->getCollectionAttributeValue('your_address_attribute_handle');
$ra1 = $rentaladdress->getAddress1();
$ra2 = $rentaladdress->getAddress2();
$racity = $rentaladdress->getCity();
$rastpr = $rentaladdress->getStateProvince();
$racountry = $rentaladdress->getCountry();
$razip = $rentaladdress->getPostalCode();
echo $ra1; echo $ra2; echo $racity; echo $rastpr; echo $racountry; echo $razip;
?>

Just so you know what I am talking about, I am replying to the above code that was posted.

Was this documented anywhere? I haven't been able to find it if so. It was very helpful to me! Thanks for posting it. :)
ceyhuncenger replied on at Permalink Reply
ceyhuncenger
I am glad to hear that it is helpful on your project. I actually don't know if it is documented somewhere, but I figured that out by myself while trying to find out how the address attribute works.
The following file helped me to figure it out;

/concrete/models/attribute/types/address/controller.php