【问题标题】:How do you create a Marker with a custom icon for google maps API v3?如何为谷歌地图 API v3 创建一个带有自定义图标的标记?
【发布时间】:2012-05-09 17:39:15
【问题描述】:

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

这是我的 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);
  }
}

对于 gmap 的初学者有什么建议吗?

【问题讨论】:

    标签: javascript google-maps-api-3 google-maps-markers


    【解决方案1】:
    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
      });
    

    【讨论】:

    • cdn.com/my-custom-icon.png 只是一个虚拟地址,您应该对有效图像使用有效地址,并且您已经声明了两次相同的相同 var "simlerweb",您可以删除其中一个
    • 你是指任何地方?是的,图像可以托管在您想要的任何地方!
    • 我正在使用我的 aws s3 存储桶来获取图像。@Luca 但它不适用于 ios。我的项目在 nativescript-angular 上。它说 null,即使我分配了一个 url。
    • @Heena Vora:我猜是因为在移动开发中,出于安全原因,您不能包含远程资源……很可能您应该想办法在本地存储图像。
    • 对于本地图像,它可以正常工作。上面带有 URL 图标的代码对我不起作用。
    【解决方案2】:

    试试

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

    从这里

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

    【讨论】:

      【解决方案3】:

      你想要的颜色上的符号!

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

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

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

      【讨论】:

        【解决方案4】:
        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));
        

        【讨论】:

        • android != java 脚本
        • java 脚本 !== javascript
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-26
        • 2013-06-02
        • 1970-01-01
        • 2012-05-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多