Adding Markers to Google Maps v3 After Map Created

2019-05-31 13:36发布

问题:

I'm relatively new with using Google Maps API. Right now I am working on a project where a user can select various search filters and see results automatically show up on the map without reloading the page. My approach thus far has been to create a Javascript object with controls the map so that I could manipulate pieces of it as I wish (ie, centerMap(), addMarker(), clearMap(), etc). I developed several proof-of-concept pieces before beginning this phase of the project.

Right now I am at the point where I have a custom JS object which controls my GMap. The GMap is drawn correctly, the zoom and center are fine. Next I try to run a function which adds a Marker to the map. I'm guessing it's possible to add markers after a map has been generated and I'm simply making a mistake somewhere. Below is my code as it exists. I'm wondering if anybody can shed some light as to why my marker isn't showing up...

Any and all help is greatly appreciated!

Code: http://pastebin.com/1ZzH9zHk

Thanks in advance!! Dima

回答1:

In your code, shouldn't

var map = new google.maps.Map($('map_canvas'), myOptions);

be

this.map = new google.maps.Map($('map_canvas'), myOptions);