user6656728
user6656728

Reputation:

Is not a function error in googlemaps api javascript

I am using google api to show the vehicle current position in one of my function i have a code like this

 function fromLatLngToDivPixel(map, latLng) {
    var overlay = new google.maps.OverlayView();
    overlay.draw = function () { };
    overlay.setMap(map);
    var point = overlay.getProjection().fromLatLngToDivPixel(latLng);
    overlay.setMap(null);
    overlay = null
    return point;
}

but while executing this code i am getting the error on this overlay.setMap(null);

and i am not able to execute this. the error says

Uncaught TypeError: this.pa.remove is not a function

yes i know it is very dificult to answer questions like this, but if only anyone have the idea about this error?

i am attaching the image for better reference.enter image description here

and when i commented this line my code is running properly,

can i use the function without this -> overlay.setMap(null);

Upvotes: 1

Views: 1193

Answers (2)

geocodezip
geocodezip

Reputation: 161324

Per the documentation for OverlayView you must implement 3 methods:

You must implement three methods: onAdd(), draw(), and onRemove().

  • In the onAdd() method, you should create DOM objects and append them as children of the panes.
  • In the draw() method, you should position these elements.
  • In the onRemove() method, you should remove the objects from the DOM.

You don't implement the onRemove (or the onAdd) method.

function fromLatLngToDivPixel(map, latLng) {
  var overlay = new google.maps.OverlayView();
  overlay.draw = function() {};
  overlay.onAdd = function() {};
  overlay.onRemove = function() {};
  overlay.setMap(map);
  var point = overlay.getProjection().fromLatLngToDivPixel(latLng);
  overlay.setMap(null);
  overlay = null
  return point;
}

Upvotes: 2

PA Knudsen
PA Knudsen

Reputation: 13

Looks like setMap(null) is used to remove markers. https://developers.google.com/maps/documentation/javascript/markers#remove

So unless that is your intention the code should work fine without calling setMap(null) in your code.

I would change it to

function fromLatLngToDivPixel(map, latLng) {
    var overlay = new google.maps.OverlayView();
    overlay.draw = function () { };
    overlay.setMap(map);
    return overlay.getProjection().fromLatLngToDivPixel(latLng);
}

You might find some useful info here too: why setMap(null) is not working google maps api v3?

Upvotes: 0

Related Questions