JavaScript InfoWindow showing in wrong place

I have little problem with my webpage. When I'm trying to show InfoWindow on marker. That windows shows on the top left corner of a map instead of on the marker. Just like in screen below: enter image description here

Here is my script I use for placing markers and using InfoWindows.

var infowindow;
function point(name, lat, long) {
    var self = this;
    self.name = name;
    self.lat = ko.observable(lat);
    self.long = ko.observable(long);

var marker = new google.maps.Marker({
    position: new google.maps.LatLng(lat, long),
    title: name,
    map: map,
    draggable: true
});

//if you need the poition while dragging
google.maps.event.addListener(marker, 'drag', function () {
    var pos = marker.getPosition();
    this.lat(pos.lat());
    this.long(pos.lng());
}.bind(this));

//if you just need to update it when the user is done dragging
google.maps.event.addListener(marker, 'dragend', function () {
    var pos = marker.getPosition();
    this.lat(pos.lat());
    this.long(pos.lng());
}.bind(this));

google.maps.event.addListener(marker, 'mouseover', function () {
    infowindow = new google.maps.InfoWindow({ content: "empty" });
    console.log(marker.title);
    infowindow.setContent(marker.name);
    infowindow.open(map, marker);

}.bind(this));


}

var map = new google.maps.Map(document.getElementById('googleMap'), {
    zoom: 5,
    center: new google.maps.LatLng(55, 11),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

var viewModel = {
    points: ko.observableArray([
        new point('Test1', 55, 11),
        new point('Test2', 56, 12),
        new point('Test3', 57, 13)])

};
function addPoint() {
    viewModel.points.push(new point('a', 58, 14));
}

ko.applyBindings(viewModel);

here all on jsfiddle: http://jsfiddle.net/24q8n/

Answer:1

You are using marker.name which is undefined. So you are assigning undefined to setContent what apparently stops the infoWindow from working and ends up in the top left corner. Remove infowindow.setContent(marker.name) or replace it with something that does have a value like marker.title. Or define marker.name when you define var marker = ...:

var marker = new google.maps.Marker({
    position: new google.maps.LatLng(lat, long),
    title: name,
    name: name, // define the name so setContent(marker.name) will work.
    map: map,
    draggable: true
});

Or remove the line that creates the problem:

google.maps.event.addListener(marker, 'mouseover', function () {
    infowindow = new google.maps.InfoWindow({ content: "empty" });
    console.log(marker.title);
    // This line gives you the problem because marker.name is undefined.
    // infowindow.setContent(marker.name);
    infowindow.open(map, marker);    
}.bind(this)); 
Answer:2

I used https://github.com/fatlinesofcode/ngDraggable for Drag&Drop in AngularJS. It works very fine, but I need to do some operations when the user is starting with dragging. This is myMethod(). I ...

I used https://github.com/fatlinesofcode/ngDraggable for Drag&Drop in AngularJS. It works very fine, but I need to do some operations when the user is starting with dragging. This is myMethod(). I ...

  1. call method starting new task
  2. abap call method starting new task
  3. sap call method starting new task

I am not able to figure out, why this code (only js) is not running - http://jsfiddle.net/fLEAw/ populateList(); populateList: function () { var accData = [{ A: "A1" }, { B: "B1"}]; $....

I am not able to figure out, why this code (only js) is not running - http://jsfiddle.net/fLEAw/ populateList(); populateList: function () { var accData = [{ A: "A1" }, { B: "B1"}]; $....

  1. jquery each not working
  2. jquery each not working on array
  3. jquery each not working for dynamic content
  4. jquery each not working on ajax loaded content
  5. jquery each not working after ajax call
  6. jquery each not working safari
  7. jquery each not working in ie11
  8. jquery each not working firefox
  9. jquery each not working in ie
  10. jquery each not working chrome
  11. jquery each doesn't work
  12. jquery each does not work
  13. jquery each function not working
  14. jquery each loop not working
  15. jquery each return not working
  16. jquery each class not working
  17. jquery each function not working in ie
  18. jquery each index not working
  19. jquery each element not working
  20. jquery each object not working

I have two text fields with radio buttons. When I click on 1st button that text field should be active and other text field should be inactive. But, When I click on 2nd button both text fields are ...

I have two text fields with radio buttons. When I click on 1st button that text field should be active and other text field should be inactive. But, When I click on 2nd button both text fields are ...

How to filter data for a nested Kendo UI datasource in Hierarchy grid Below is the datasource: Obj1{"Name":"abc","id":1 ,Obj2 {{"Name":"A1","oid":1},{"Name":"A2","oid":2}} I need to search for Obj2 ...

How to filter data for a nested Kendo UI datasource in Hierarchy grid Below is the datasource: Obj1{"Name":"abc","id":1 ,Obj2 {{"Name":"A1","oid":1},{"Name":"A2","oid":2}} I need to search for Obj2 ...

  1. filter nested data
  2. filter nested data d3
  3. filter nested data r
  4. json filter nested data
  5. dplyr filter nested data frame
  6. js filter nested data
  7. javascript filter nested data