草庐IT

php - jquery-gmap yii

coder 2024-04-20 原文

我正在使用这个扩展 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();
                ?>

最佳答案

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

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

看看下面的例子:这个例子我们使用一个类 Address 有 3 个公共(public)成员,latitude,longitude 和 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();

关于php - jquery-gmap yii,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11173521/

有关php - jquery-gmap yii的更多相关文章

  1. jquery - 我的 jquery AJAX POST 请求无需发送 Authenticity Token (Rails) - 2

    rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送

  2. jquery - 如何将 AJAX 变量从 jQuery 传递到他们的 Controller ? - 2

    我有一个电子邮件表格。但是我正在制作一个测试电子邮件表单,用户可以在其中添加一个唯一的电子邮件,并让电子邮件测试将其发送到该特定电子邮件。为了简单起见,我决定让测试电子邮件通过ajax执行,并将整个内容粘贴到另一个电子邮件表单中。我不知道如何将变量从我的HAML发送到我的Controllernew.html.haml-form_tagadmin_email_blast_pathdoSubject%br=text_field_tag'subject',:class=>"mass_email_subject"%brBody%br=text_area_tag'message','',:nam

  3. ruby-on-rails - 这个 C 和 PHP 程序员如何学习 Ruby 和 Rails? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它

  4. jquery - 如何在 rails 3.1 上安装 jQuery - 2

    我以为它已经安装了,但在我的gemfile中有gem"jquery-rails"但是在我的asset/javascripts文件夹中accounts.js.coffeeapplication.js都被注释掉了这是我的虚拟railsapplication但是在源代码中没有jQuery并且删除链接不起作用......任何想法都丢失了 最佳答案 看看thisRailscast.您可能需要检查application.js文件并确保它包含以下语句。//=requirejquery//=requirejquery_ujs

  5. jquery - Rails 如何创建 Jquery flash 消息而不是默认的 Rails 消息 - 2

    我想在页面顶部创建自定义Jquery消息而不是标准RailsFlash消息。我想在我的投票底部附近创建一条即时消息。我的Controller:defvote_up@post=Post.find(params[:id])current_user.up_vote(@post)flash[:message]='Thanksforvoting!'redirect_to(root_path,:notice=>'Takforditindlæg,deternuonline!')rescueMakeVoteable::Exceptions::AlreadyVotedErrorflash[:error]

  6. javascript - jQuery 的 jquery-1.10.2.min.map 正在触发 404(未找到) - 2

    我看到有关未找到文件min.map的错误消息:GETjQuery'sjquery-1.10.2.min.mapistriggeringa404(NotFound)截图这是从哪里来的? 最佳答案 如果ChromeDevTools报告.map文件的404(可能是jquery-1.10.2.min.map、jquery.min.map或jquery-2.0.3.min.map,但任何事情都可能发生)首先要知道的是,这仅在使用DevTools时才会请求。您的用户不会遇到此404。现在您可以修复此问题或禁用sourcemap功能。修复:获取文

  7. jquery - 在 Rails 中从原型(prototype)切换到 jquery,助手呢? - 2

    我目前从prototype切换到jquery主要是为了支持简单的ajax文件上传。我使用:https://github.com/indirect/jquery-rails95%的javascript代码是由railshelper编写的,例如:-remote_function-render:updatedo|page|-page.replace_html'id',:partial=>'content'-page['form']['name']=something-page.visual_effect:highlight,'head_success'...我知道我必须为Jquery重写5%

  8. jquery - Ruby 1.9.1、Rails 2.3.2 和 jrails 0.4 出现 "rescue in const_missing"错误 - 2

    我最近开始了一个项目,团队决定我们希望使用jQuery而不是Prototype/Scriptaculous来满足我们的javascript需求。我们设置了我们的项目,并开始切换。插件已安装viatheseinstructions,一切都按计划进行。不久之后,当尝试运行“./script/server”时,我们收到以下错误:=>Rails2.3.2applicationstartingonhttp://0.0.0.0:3000/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-2.3.2/lib/active_support/depende

  9. jquery - 使用 Rails 3 学习 Ajax 的资源 - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭10年前。有没有学习Ajax(jQuery)和Rails3的好资源?

  10. jquery - Sprockets::FileNotFound - 找不到文件 'jquery.ui' - 2

    这个问题已经被问过几次了,但我尝试了提供的解决方案,但仍然没有帮助,所以我提出了一个新问题。gem文件gem'jquery-ui-rails'按照建议,我将gem放在:assets组之外Application.css~*=require_self*=requirejquery.ui*=requirebootstrap-datepicker*=requirejquery.timepicker*=require_tree.*/RailsAssetPipeline根据列出的顺序加载Assets。在这里,我把它排在列表的第2位。Application.css.scss*=require_sel

随机推荐