javascript - Change icon of google map marker when onmouseover div (Google maps v3 api) - Stack Overflow

How do i change the icon of a marker on google maps when I mouseover the text in a div? I managed to ch

How do i change the icon of a marker on google maps when I mouseover the text in a div? I managed to change the marker icon onmouseover the marker in the map itself using

google.maps.event.addListener(marker1, "mouseover", function(event) {
            this.setIcon(";chld=1|ffffff|c41200");
}

EDIT:

Here is what I have now:

function initialize(){
....
var marker1 = new google.maps.Marker({  

            position: new google.maps.LatLng(1.288693,103.846733),

            map: map,

            icon: ";chld=1|c41200|ffffff"

        }); 
....
}


function changeMarker(marker) {

            alert(marker);

    }

and

<div id="searchresult" onmouseover="changeMarker(marker1)">

I'm using Chrome. In the console, onmouseover the div I get the error "Uncaught ReferenceError: marker1 is not defined"

How do i change the icon of a marker on google maps when I mouseover the text in a div? I managed to change the marker icon onmouseover the marker in the map itself using

google.maps.event.addListener(marker1, "mouseover", function(event) {
            this.setIcon("http://chart.apis.google./chart?chst=d_map_pin_letter&chld=1|ffffff|c41200");
}

EDIT:

Here is what I have now:

function initialize(){
....
var marker1 = new google.maps.Marker({  

            position: new google.maps.LatLng(1.288693,103.846733),

            map: map,

            icon: "http://chart.apis.google./chart?chst=d_map_pin_letter&chld=1|c41200|ffffff"

        }); 
....
}


function changeMarker(marker) {

            alert(marker);

    }

and

<div id="searchresult" onmouseover="changeMarker(marker1)">

I'm using Chrome. In the console, onmouseover the div I get the error "Uncaught ReferenceError: marker1 is not defined"

Share Improve this question edited May 19, 2011 at 4:15 Nyxynyx asked May 18, 2011 at 18:52 NyxynyxNyxynyx 63.7k163 gold badges507 silver badges856 bronze badges 1
  • On a side note, I would remove the php tag, since this question has nothing to do with php. I'd do it myself, but I don't yet have the privileges to retag questions. – matzahboy Commented May 18, 2011 at 23:14
Add a ment  | 

3 Answers 3

Reset to default 6

Add a onmouseover property to the div. Let's say it was called changeMarker.

function changeMarker(marker) {
    var icon = new Google.maps.MarkerImage({ url:"http://chart.apis.google./chart?chst=d_map_pin_letter&chld=1|ffffff|c41200"});
    marker.setIcon(icon);
}

Your div could then look like:

<div onmouseover="changeMarker(marker1)">

I would remend however caching the MarkerImage (since it seems pretty static) so that Google doesn't need to keep regenerating the graph image.

You can set other properties of the image. See the documentation

google.maps.event.addListener(marker1, 'mouseover', function () {
    marker1.setIcon('miniMarker.png');                      
 });

first call initialize function, define marker1 and then use this code, You can also call this function from different ways like you want on div mouse over etc.

I'm using Chrome. In the console, onmouseover the <div> I get the error:

Uncaught ReferenceError: marker1 is not defined

If you set variable like this:

function a() {
  var marker1 = "foo";
}

alert(marker1);

marker1 is not accessible at "window" level. You have to write it like this:

var marker1;

function a() {
  marker1 = "foo";
}

alert(marker1);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742323057a4422201.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信