Cakephp谷歌地图获取纬度,经度和格式化的地址

时间:2013-02-09 04:25:35

标签: cakephp

我的 ContactusController.php 功能为:

<?php
App::uses('GeocodeLib', 'Tools.Lib');
class ContactusController extends AppController
{
    public function beforeFilter()
    {
parent::beforeFilter();
$this->Auth->allow('main', 'step2');

}



public $name = 'Contactus';



public $components = array('RequestHandler', 'Email', 'Session'); 

public $helpers = array('Js','Html','Form', 'Tools.GoogleMapV3'); 





public function main() {



    if ($this->request->is('post')) {



        $this->Contactus->set($this->data);



          if ($this->Contactus->validates()) {



            $settings = array('min_accuracy'=>GeocodeLib::ACC_SUBLOC);



            $this->Geocode = new GeocodeLib();

            $this->Geocode->setOptions(array('host'=>'us')); //optional - you can set it to your country's top level domain.

            if ($this->Geocode->geocode($this->data['Contactus']['address1']. " " .$this->data['Contactus']['address2'], $settings)) {

                $result = $this->Geocode->getResult();



                 /*debug($result);*/



                $options = array(

                    'lat' => $result['lat'],

                    'lng' => $result['lng'],

                    'content' => 'Testing',

                    'draggable' => TRUE,



                );



            }

                /*

                $this->Email->to = array("sales@ichiban.com.my", "clement@ichiban.com.my");

                $this->Email->subject = 'Please contact ' . $this->data['Contactus']['name'];

                $this->Email->from = "websys@ichiban.com.my"; 

                $this->Email->bcc = "yienbin@nixser.com";

                $this->Email->replyTo = $this->data['Contactus']['email'];

                //25, Jalan Ceria 5, Taman Nusa Indah, Johor

                $this->Email->send("Name: " . $this->data['Contactus']['name'] . "\n" .

                                              "Phone: " . $this->data['Contactus']['phone'] . "\n" .

                                              "Fax: " . $this->data['Contactus']['fax'] . "\n" .

                                              "Address: " . $this->data['Contactus']['address'] . "\n" .

                                              "Email: " . $this->data['Contactus']['email'] . "\n" . 

                                              "Detail: " . $this->data['Contactus']['detail'] . "\n");



                $this->Session->setFlash('Sent', 'message', array('class' => 'success1'));

                */
                $this->request->data['Customer'] = $this->request->data['Contactus'];
                $this->loadModel('Customer');
                pr($this->request->data);
                $this->Customer->save($this->request->data);

          }



        $this->set(array(

            'options' => $options

            ));



    }

    $customerStatuses = $this->Contactus->CustomerStatus->find('list');
    $enquiryTypes = $this->Contactus->EnquiryType->find('list');
    $this->set(compact('customerStatuses', 'enquiryTypes'));    
    $this->layout = 'popup';



}



public function step2() {





    $this->layout = 'popup';



}



     }

    ?>

这是 main.ctp 功能

        <div class="row">

        <div class="span10 breakline">
            <h1 class="headtitle">Your Contact Details</h1>
            <h2>Please confirm your address and provide your name, phone number and email address</h2>

            <?php 
                echo $this->Form->create('Contactus');

                echo "<div class='control-group'>\n";
                echo "". $this->Form->input('full_name', array( 'label' => 'Full Name' )) . "\n";
                echo "</div>\n";

                echo "<div class='control-group'>\n";
                echo "". $this->Form->input('company_name', array( 'label' => 'Company Name' )) ."\n";
                echo "</div>\n";

                echo "<div class='control-group'>\n";
                echo "". $this->Form->input('phone', array( 'label' => 'Phone Number' )) ."\n";
                echo "</div>\n";

                echo "<div class='control-group'>\n";
                echo "". $this->Form->input('email', array( 'label' => 'Email Address' )) ."\n";
                echo "</div>\n";

                echo "<div class='control-group'>\n";
                echo "". $this->Form->input('address1', array( 'label' => 'Address Line 1')) ."\n";
                echo "</div>\n";

                echo "<div class='control-group'>\n";
                echo "". $this->Form->input('address2', array( 'label' => 'Address Line 2')) ."\n";
                echo "</div>\n";


                echo  $this->Form->input('formated_address', array( 'label' =>false,'type'=>'hidden'));
                echo  $this->Form->input('lat', array( 'label' =>false,'type'=>'hidden'));
                echo  $this->Form->input('lng', array( 'label' =>false,'type'=>'hidden'));



                echo "<div class='control-group'>\n";
                echo "". $this->Form->input('contact_by', array( 'label' => 'Contact Me By','type'=>'select','options'=>array('Email'=>'Email','Telephone'=>'Telephone'))) ."\n";
                echo "</div>\n";

                echo "<div class='control-group'>\n";
                echo "". $this->Form->input('enquiry_type_id', array('type'=>'select', 'label' => 'Type Of Enquiry')) ."\n";
                echo "</div>\n";

                echo "<div class='control-group'>\n";
                echo "". $this->Form->input('customer_status_id', array('type'=>'select', 'label' => 'Customer Type')) ."\n";
                echo "</div>\n";


                //echo $this->Form->end('Next', array('class' => 'btn btn-primary'));
                echo $this->Form->end(__('Next', array('class' => 'btn btn-primary')));
            ?>

        </div>

        <div class="span10 breakline">

        </div>

    </div>

    <h2>Please drag the map to find your installation location, and place the pin on your roof</h2>

    <?php echo $this->Session->flash(); 
    echo $this->Html->script($this->GoogleMapV3->apiUrl());
    $options2 = array(
                'zoom'=>6,
                'type'=>'R',
                'autoCenter' => true,
                'div' => array('id'=>'mapCanvas'),
                'map' => array('navOptions'=>array('style'=>'SMALL'), 'typeOptions' => array('style'=>'HORIZONTAL_BAR', 'pos'=>'RIGHT_TOP'))
            );
            //debug($options2);
    ?>

    <?php
    if ($this->request->is('post')) {

            // tip: use it inside a for loop for multiple markers
            //echo $this->GoogleMapV3->map($options2);


            $js = "
                var geocoder = new google.maps.Geocoder();

                function geocodePosition(pos) {
                  geocoder.geocode({
                    latLng: pos
                  }, function(responses) {
                    if (responses && responses.length > 0) {
                      updateMarkerAddress(responses[0].formatted_address);
                    } else {
                      updateMarkerAddress('Cannot determine address at this location.');
                    }
                  });
                }

                function updateMarkerStatus(str) {
                  document.getElementById('markerStatus').innerHTML = str;
                }

                function updateMarkerPosition(latLng) {
                  document.getElementById('info').innerHTML = [
                    latLng.lat(),
                    latLng.lng()
                  ].join(', ');
                }

                function updateMarkerAddress(str) {
                  document.getElementById('address').innerHTML = str;
                }

                function initialize() {
                  var latLng = new google.maps.LatLng(" . $options['lat'] . ", " . $options['lng'] . ");
                  var map = new google.maps.Map(document.getElementById('mapCanvas'), {
                    zoom: 17,
                    center: latLng,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                  });
                  var marker = new google.maps.Marker({
                    position: latLng,
                    title: 'Point A',
                    map: map,
                    draggable: true
                  });

                  // Update current position info.
                  updateMarkerPosition(latLng);
                  geocodePosition(latLng);

                  // Add dragging event listeners.
                  google.maps.event.addListener(marker, 'dragstart', function() {
                    updateMarkerAddress('Dragging...');
                  });

                  google.maps.event.addListener(marker, 'drag', function() {
                    updateMarkerStatus('Dragging...');
                    updateMarkerPosition(marker.getPosition());
                  });

                  google.maps.event.addListener(marker, 'dragend', function() {
                    updateMarkerStatus('Drag ended');
                    geocodePosition(marker.getPosition());
                  });
                }

                // Onload handler to fire off the app.
                google.maps.event.addDomListener(window, 'load', initialize);

            ";

            //$this->GoogleMapV3->addMarker($options);

            //$this->GoogleMapV3->addCustom($js);

            // print js
            echo $this->Html->scriptBlock($js);

        }

    ?>
    <div id="mapCanvas"></div>

    <div id="infoPanel">

        <b>Marker status:</b>
        <div id="markerStatus"><i>Click and drag the marker.</i></div>

        <b>Current position:</b>
        <div id="info"></div>

        <b>Closest matching address:</b>
        <div id="address"></div>

    </div>

我希望 和inser进入字段“formated_address”,  “lat”“long”

请帮帮我,我无法做到。

当我调试时,我得到了这个:

    Array
    (
[Contactus] => Array
    (
        [full_name] => yein Bin
        [company_name] => Microsoft
        [phone] => +6012345678
        [email] =>someting@gmail.com
        [address1] => 25, Jalan Ceria 5
        [address2] => Taman Nusa Indah, Johor
        [formated_address] => 
        [lat] => 
        [lng] => 
        [contact_by] => Email
        [enquiry_type_id] => 1
        [customer_status_id] => 1
    )

[Customer] => Array
    (
        [full_name] => yein Bin
        [company_name] => Microsoft
        [phone] => +6012345678
        [email] =>someting@gmail.com
        [address1] => 25, Jalan Ceria 5
        [address2] => Taman Nusa Indah, Johor
        [formated_address] => 
        [lat] => 
        [lng] => 
        [contact_by] => Email
        [enquiry_type_id] => 1
        [customer_status_id] => 1
    )

  )
有人请帮帮我

0 个答案:

没有答案