【问题标题】:jquery-gmap yiijquery-gmap yii
【发布时间】:2012-06-23 22:36:23
【问题描述】:

我正在使用此扩展程序Yii Framework: Jquery-gmap 我使用了$gmap->updateMarkerAddressFromModel$marker->capturePosition 的应用程序。

但是$gmap->updateMarkerAddressFromModel在使用$marker->capturePosition时不起作用,否则$gmap->updateMarkerAddressFromModel单独使用时工作正常。

我的代码

<?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();
                ?>

【问题讨论】:

  • 执行$gmap->capturePosition时yii日志告诉你什么
  • 什么是 $businessModel?你能给出那个代码吗?
  • $businessModel 没问题.. 这部分代码不用担心

标签: php google-maps yii jquery-gmap3


【解决方案1】:

您没有初始化传递给视图的模型。

所以在你可以将模型传递给你需要添加的视图之前 $businessModel = new BusinessModel() 我假设这是您的班级的名称以及对它的引用。我再次假设模型类定义了正确的成员,即您稍后使用的 lat 和 long。

看看下面的例子:这个例子我们使用了一个类Address,它有3个公共成员,纬度、经度和缩放级别

From the jquery-gmap plugin documentation:

保存标记位置和地图缩放到 Yii 模型

允许从地图标记和地图的缩放级别捕获纬度和经度到 Yii 模型对象。如果您想在数据库中保存与地址相关的其他信息,这将非常有用。

地址模型示例:

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

在您的视图文件中:

// 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();

【讨论】:

  • 我需要回答我的问题而不是解释文档 :) 是的,我已经初始化了 businessModel
猜你喜欢
  • 1970-01-01
  • 2011-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-30
  • 2023-03-27
  • 1970-01-01
相关资源
最近更新 更多