4

I'm using this extension Yii Framework: Jquery-gmap Im my application I've used $gmap->updateMarkerAddressFromModel & $marker->capturePosition.

But $gmap->updateMarkerAddressFromModel is not working when $marker->capturePosition is used, otherwise $gmap->updateMarkerAddressFromModel is working fine when used alone.

My code

<?php
                Yii::import('ext.jquery-gmap.*');
                $gmap = new EGmap3Widget();
                $gmap->setSize(400, 234);

                // base options
                $options = array(
                    'scaleControl' => true,
                    'zoom' => 15,
                    'center' => array(0, 0),
                    'mapTypeId' => EGmap3MapTypeId::ROADMAP,
                    'mapTypeControlOptions' => array(
                        'style' => EGmap3MapTypeControlStyle::DROPDOWN_MENU,
                        'position' => EGmap3ControlPosition::TOP_CENTER,
                    ),
                );
                $gmap->setOptions($options);

                // marker with custom icon
                $marker = new EGmap3Marker(array(
                            'draggable' => true,
                        ));
                $marker->address = 'London';
                $marker->capturePosition(
                    // the model object
                    $businessModel,
                    // model's latitude property name
                    'lat',
                    // model's longitude property name
                    'longi',
                    array('drag')
                );
                // tell the gmap to update the marker from the model fields.
                $gmap->updateMarkerAddressFromModel(
                        // the model object
                        $businessModel,
                        array('street','town','country'),
                        array('zoom'=>16)
                );
                $marker->centerOnMap();
                $gmap->add($marker);
                $gmap->renderMap();
                ?>
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
iThink
  • 1,239
  • 3
  • 13
  • 34

1 Answers1

1

You are not initialising the model you are passing to the view.

So before you can pass the model to the view you need to add $businessModel = new BusinessModel() I am assuming that this is the name of your class as well as the reference to it. Again I am assuming that the model class has the right members defined i.e. lat and long which you are using later on.

Take a look at the example below: the example us using a class Address with 3 public members, latitude, longitude and zoomlevel

From the jquery-gmap plugin documentation:

Save Marker Position and Map Zoom to Yii Model

Allows capturing the latitude and longitude from a map marker, and the map's zoom level, to a Yii model object. This is useful if you want to save additional information related to an address in your database.

Address model example :

class Address extends CActiveRecord
{
    public $latitude;
    public $longitude;
        public $mapZoomLevel;
 
    public function rules()
    {
            return array(
                array('latitude,longitude', 'numerical'),
                array('mapZoomLevel', 'numerical', 'integerOnly'=>true),
            );
    }
}

In your view file :

// init the model (usually passed to view)
$address = new Address();


// init the map
$gmap = new EGmap3Widget();
$gmap->setOptions(array('zoom' => 14));
 
// create the marker
$marker = new EGmap3Marker(array(
    'title' => 'Draggable address marker',
    'draggable' => true,
));
$marker->address = '10 Downing St, Westminster, London SW1A 2, UK';
$marker->centerOnMap();
 
// set the marker to relay its position information a model
$marker->capturePosition(
     // the model object
     $address,
     // model's latitude property name
     'latitude',
     // model's longitude property name
     'longitude',
     // Options set :
     //   show the fields, defaults to hidden fields
     //   update the fields during the marker drag event
     array('visible','drag')
);
$gmap->add($marker);
 
// Capture the map's zoom level, by default generates a hidden field
// for passing the value through POST
$gmap->map->captureZoom(
    // model object
    $address,
    // model attribute
   'mapZoomLevel',
   // whether to auto generate the field
   true,
   // HTML options to pass to the field
   array('class' => 'myCustomClass'),
);
 
$gmap->renderMap();
Community
  • 1
  • 1
Orlymee
  • 2,349
  • 1
  • 22
  • 24
  • I need an answer to my problem not explanation of the document :) And yes I've initialized businessModel – iThink Jul 19 '12 at 20:09