Javascript 你如何为谷歌地图 API v3 创建一个带有自定义图标的标记?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10376617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:05:03  来源:igfitidea点击:

How do you create a Marker with a custom icon for google maps API v3?

javascriptgoogle-maps-api-3google-maps-markers

提问by andy

I've been reading https://developers.google.com/maps/documentation/javascript/overlaysfor a while now and I can't seem to get a custom icon for my map working.

我已经阅读https://developers.google.com/maps/documentation/javascript/overlays一段时间了,但似乎无法为我的地图工作获得自定义图标。

Here is my javascript:

这是我的javascript:

var simplerweb = new google.maps.LatLng(55.977046,-3.197118);
var marker;
var map;

function initialize() {
    var myOpts = {
        center:    simplerweb,  
        zoom:      15,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOpts);
    marker = new google.maps.Marker({
        map:        map,
        draggable:  true,
        animation:  google.maps.Animation.DROP,
        position:   simplerweb
    });
    google.maps.event.addListener(marker, 'click', toggleBounce);
}

function toggleBounce() {
  if (marker.getAnimation() != null) {
    marker.setAnimation(null);
  } else {
    marker.setAnimation(google.maps.Animation.BOUNCE);
  }
}

Any pointers for a complete beginner with gmaps?

对gmaps的完整初学者有什么指导吗?

回答by Luca Filosofi

marker = new google.maps.Marker({
    map:map,
    // draggable:true,
    // animation: google.maps.Animation.DROP,
    position: new google.maps.LatLng(59.32522, 18.07002),
    icon: 'http://cdn.com/my-custom-icon.png' // null = default icon
  });

回答by Aor Ampika

Try

尝试

    var marker = new google.maps.Marker({
      position: map.getCenter(),
      icon: 'http://imageshack.us/a/img826/9489/x1my.png',
      map: map
    });


from here

从这里

https://developers.google.com/maps/documentation/javascript/examples/marker-symbol-custom

https://developers.google.com/maps/documentation/javascript/examples/marker-symbol-custom

回答by Paolo177

Symbol You Want on Color You Want!

你想要的颜色上的符号!

I was looking for this answer for days and here it is the right and easy way to create a custom marker:

我一直在寻找这个答案好几天了,这是创建自定义标记的正确且简单的方法:

'http://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=xxx%7c5680FC%7c000000&.png' where xxx is the text and 5680fc is the hexadecimal color code of the background and 000000 is the hexadecimal color code of the text.

' http://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=xxx%7c5680FC%7c000000&.png' 其中xxx是文字,5680fc是背景的十六进制颜色代码,000000是十六进制的颜色代码.

Theses markers are totally dynamic and you can create whatever balloon icon you want. Just change the URL.

这些标记是完全动态的,您可以创建任何您想要的气球图标。只需更改网址。

回答by Avinash_ks

LatLng hello = new LatLng(X, Y);          // whereX & Y are coordinates

Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(),
                R.drawable.university);   // where university is the icon name that is used as a marker.

mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(icon)).position(hello).title("Hello World!"));

mMap.moveCamera(CameraUpdateFactory.newLatLng(hello));