简体   繁体   English

在onmouseover div上更改谷歌地图标记的图标(Google maps v3 api)

[英]Change icon of google map marker when onmouseover div (Google maps v3 api)

How do i change the icon of a marker on google maps when I mouseover the text in a div? 当我将鼠标悬停在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.com/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.com/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. 我正在使用Chrome。 In the console, onmouseover the div I get the error "Uncaught ReferenceError: marker1 is not defined" 在控制台中,onmouseover div我得到错误“Uncaught ReferenceError:marker1未定义”

Add a onmouseover property to the div. 将一个onmouseover属性添加到div。 Let's say it was called changeMarker. 让我们说它叫做changeMarker。

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

Your div could then look like: 你的div可能看起来像:

<div onmouseover="changeMarker(marker1)">

I would recommend however caching the MarkerImage (since it seems pretty static) so that Google doesn't need to keep regenerating the graph image. 我建议缓存MarkerImage(因为它看起来很静态),因此谷歌不需要继续重新生成图形图像。

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. 首先调用初始化函数,定义marker1然后使用这个代码,你也可以通过不同的方式调用这个函数,就像你想要div鼠标一样。

I'm using Chrome. 我正在使用Chrome。 In the console, onmouseover the <div> I get the error: 在控制台中, onmouseover <div>我得到错误:

Uncaught ReferenceError: marker1 is not defined 未捕获的ReferenceError:未定义marker1

If you set variable like this: 如果你设置这样的变量:

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

alert(marker1);

marker1 is not accessible at "window" level. 在“窗口”级别无法访问marker1。 You have to write it like this: 你必须像这样写:

var marker1;

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

alert(marker1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM