Google Map - Trigger Event - Return Results To View

Hi

Thank you for taking the time to read this post and any help with the following would be most appreciated

I have a Google map which my users search for a location.

Using the Geocoder I capture the Lat/Long and would like to trigger an event to query my database for nearby venues and return the data to my view to populate a grid list of extra results.

But I am stuck :unsure:

Do I create this in the model or in the controller? and how is this event triggered from my Google script?

Once again any help would be most appreciated

Google Script




// Geocoder - Map Search From User Input

function codeAddress() {

  var address = document.getElementById('address').value;

  geocoder.geocode( { 'address': address, 'componentRestrictions':{'country':'GB'}}, function(results, status) {

    if (status == google.maps.GeocoderStatus.OK) {

      map.setCenter(results[0].geometry.location);


          alert(results[0].geometry.location.lat());  // Test My Latitude !!!! WORKS OK !!!!

          alert(results[0].geometry.location.lng());  // Test My Longitude !!!! WORKS OK !!!


      var marker = new google.maps.Marker({

          map: map,

          position: results[0].geometry.location

      });

    } else {

      alert('Geocode was not successful for the following reason: ' + status);

    }

  });

}



MySQL




// MySQL for finding lat longs in a 10 mile radius

$distance = 10;

$latitude = Pass My Latitude Into Here !!!!!!

$longitude = Pass My Longitude Into Here !!!!!!

 

$sql = "SELECT loc.*, (((acos(sin(($latitude*pi()/180)) * sin((`latitude`*pi()/180))+cos(($latitude*pi()/180)) * cos((`latitude`*pi()/180)) * cos((($longitude - `longitude`)*pi()/180))))*180/pi())*60*1.1515) AS `distance` FROM table_with_lonlat_ref loc HAVING distance < $distance"




My View




<div>

  <div>

    <input id="address" type="textbox" value="Search">

  </div>

  <div>

    <input type="button" value="Geocode" onclick="codeAddress()" class="button" />    

  </div>

</div>


<div>

  <div id="map" style="width: 100%; height: 300px;"></div> 

</div>


// !!!!!!!!  Return my results Back to my view Here !!!!!!!!!


<?php foreach($dataRadiusResults->data as $map):?>      

<div>

  <div><?php echo CHtml::link('View Details',array('map/index','id'=>$map['id'])); ?></div>

  <div><?php echo $map['name']?></div>

  <div><?php echo $map['address']?></div>

</div>       

<?php endforeach?>



Many Thanks

GPM

I would use Ajax to call an action in your controller. This controller action finds the data and then returns in in JSON format. In the Ajax succes area you read this JSON data and use Google Maps API functions to draw the items on the map or move the map to the location you want.

Many thanks for your help with this, I have tried to find a few examples but I am struggling with the finer details.

Do you know of a good Yii example for this?

My updated code which is returning a success is:

Google Script




// Geocoder - Map Search From User Input

function codeAddress() {

  var address = document.getElementById('address').value;

  geocoder.geocode( { 'address': address, 'componentRestrictions':{'country':'GB'}}, function(results, status) {

    if (status == google.maps.GeocoderStatus.OK) {

      map.setCenter(results[0].geometry.location);


          //alert(results[0].geometry.location.lat());  // Test My Latitude !!!! WORKS OK !!!!

          //alert(results[0].geometry.location.lng());  // Test My Longitude !!!! WORKS OK !!!


            var a = results[0].geometry.location.lat();

            var b = results[0].geometry.location.lng();


            $.ajax({

              type: "POST",

              url:    "<? echo Yii::app()->createUrl('theLocation/DynamicCities'); ?>",

              data:  {a:a,b:b},

              success: function(msg){

                   alert("Success")

                  },

              error: function(xhr){

                alert("failure"+xhr.readyState+this.url)


              }

            });


      var marker = new google.maps.Marker({

          map: map,

          position: results[0].geometry.location

      });

    } else {

      alert('Geocode was not successful for the following reason: ' + status);

    }

  });

}



Controller





public function actionDynamicCities()	

{

	$distance = 10;

	$latitude = Pass My Latitude Into Here <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?

	$longitude = Pass My Longitude Into Here <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?? 

	$sql = "SELECT *, (((acos(sin(($latitude*pi()/180)) * sin((`latitude`*pi()/180))+cos(($latitude*pi()/180)) * cos((`latitude`*pi()/180)) * cos((($longitude - `longitude`)*pi()/180))))*180/pi())*60*1.1515) AS `distance` FROM table_with_lonlat_ref loc HAVING distance < $distance";


	return <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />

}



My Target View To Be Updated




<?php foreach($dataRadiusResults->data as $map):?>      

<div>

  <div><?php echo CHtml::link('View Details',array('map/index','id'=>$map['id'])); ?></div>

  <div><?php echo $map['name']?></div>

  <div><?php echo $map['address']?></div>

</div>       

<?php endforeach?>



Once again, many thanks

GPM

SOLVED :rolleyes:

Page 93 - Yii Application Development Cookbook - Second Edition

I must try to remember to read the books that I buy

Here is my working solution

Google Script




$.ajax({

  type: "POST",

  url:    "<? echo Yii::app()->createUrl('theLocation/DynamicCities'); ?>",

  dataType: "json",

  success: function(data) {

    var out = "<ol>";

	 $(data).each(function(){

	   out+="<li>Distance "+parseFloat(this.distance).toFixed(2)+" Miles</li>";

	 });

	 out += "</ol>";


    $("#updateContent").html(out);

  

});




Controller




public function actionDynamicCities()	

{	

	$q = "SELECT * Statement here";

    $cmd = Yii::app()->db->createCommand($q);    

    $result = $cmd->queryAll();

    

    $data = array();

    foreach ($result as $row) {

        $data[] = $row;

    }

    echo CJSON::encode($data);    


}



View




<div id="updateContent"></div>



Nice work!

What I could recommend. Create a Active Record Model from your Location table.

Then use CDbCriteria to get the data instead of the normal query option. Maybe your already familiar with the ActiveRecord class, else I bet you gonne like it a lot.

You can use Gii to create a model the fast way.