Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6036787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:58:36+00:00 2026-05-23T05:58:36+00:00

I have a module I created for Google Maps v3 that I’m trying to

  • 0

I have a module I created for Google Maps v3 that I’m trying to convert into a Backbone.js view constructor.

Here’s my view module so far: I’ll explain the problems I’m having after the code:

pg.views.CreateMap = Backbone.View.extend({

  tagName:  "div",
  className: "map",

  events: {},

  latitude:   "-23.56432",
  longitude:  "-46.65183", 

  initialize: function() {
    _.bindAll(this, 'render', 'dragMarker', 'dragMap');

    this.latlng = new google.maps.LatLng(this.latitude, this.longitude);
    var myOptions = {
      zoom: 16,
      center: this.latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    this.map = new google.maps.Map($(this.el)[0], myOptions);
    this.marker = new google.maps.Marker({
      map: this.map,
      position: this.latlng, 
      draggable: true
    });

    google.maps.event.addListener(this.marker, "dragend", this.dragMarker());

    google.maps.event.addListener(this.map, "dragend", this.dragMap());

  },

  render: function() {
    return this;
  },

  dragMarker: function() {
    this.latlng = this.marker.getPosition();
    this.map.panTo(this.latlng);
  },

  dragMap: function() {
    this.latlng = this.map.getCenter();
    this.marker.setPosition(this.latlng);
  }

});

The problem I’m having is with the Google Maps event listeners and how “this” is handled.

I originally didn’t have the dragMarker and dragMap methods and instead these two in the initialize block:

google.maps.event.addListener(this.marker, "dragend", function() {
  this.latlng = this.marker.getPosition();
  this.map.panTo(this.latlng);
});

google.maps.event.addListener(this.map, "dragend", function() {
  this.latlng = this.map.getCenter();
  this.marker.setPosition(this.latlng);
});

The problem I encountered with this first approach is that “this” inside those anonymous functions referred to “this.marker” and “this.map” respectively. The problem with this first approach was that in the first listener, I had no way of referring to “this.map” and therefore could not perform a panTo(). With the second listener, I had no way of referring to “this.marker” and therefore could not recenter the the map around that marker using setPosition().

I then thought that I could pull out the anonymous functions in the listeners and declare them as methods of the view, which I would then perform a _.bindAll(this, “dragMarker”, “dragMap”);

The problem with this approach is that I then had to write the listeners in the event block like so:

google.maps.event.addListener(this.marker, "dragend", this.dragMarker());

google.maps.event.addListener(this.map, "dragend", this.dragMap());

This meant that when I called the constructor with newmap = new pg.views.CreateMap; that the “this.dragMarker()” and “this.dragMap()” were evaluated immediately instead of being evaluated as a callback when the “dragend” event is triggered.

No problem I thought and then wrapped those up in anonymous functions like so:

google.maps.event.addListener(this.marker, "dragend", function() {
  this.dragMarker();
});

google.maps.event.addListener(this.map, "dragend", function() {
  this.dragMap();
});

Unfortunately this also brings me back to an earlier problem that the “this” in “this.dragMarker” no longer refers to the parent object I constructed, but instead refer to “this.marker” again. The same problem occurs with the second listener.

I’m completely stuck here. Anyone have any ideas on how I solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:58:36+00:00Added an answer on May 23, 2026 at 5:58 am

    Take the anonymous functions called on dragend and bind explicitly.

    _.bindAll(this, 'dragMarker', 'dragMap');
    google.maps.event.addListener(this.marker, "dragend", this.dragMarker);
    /* etc ... */
    

    This way this will always be tied to CreateMap even if called out of context.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a Python module that creates and populates several SQLite tables. Now,
Here's the situation: I have an abstract class with a constructor that takes a
I have an executable module created by third party. I would like to inject
I have a DotNetNuke Module created in DNN 4.9.2. It runs fine on my
i have created a module with this among others this function in it: <?php
I have created a multi module maven project. Now I have shared the project
I am using ASP MVC 2 and Nhibernate. I have created an HTTP Module
Typically I create a plugin when I have a module that I know I'm
In a module i have a sub that create modalpopup and show result of
i have asked to create a module that will record video messages form the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.