Google Maps JS API v3 - 带有自定义标记的简单多标记示例 [英] Google Maps JS API v3 - Simple Multiple Marker Example with Custom Markers

查看:16
本文介绍了Google Maps JS API v3 - 带有自定义标记的简单多标记示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的 Google 地图的绝佳示例:Google Maps JS API v3 - 简单的多重标记示例

Here is a great example of a simple Google Map: Google Maps JS API v3 - Simple Multiple Marker Example

我正在尝试向此示例添加为每个制造商分配自定义图标的功能.鉴于链接的例子:

I am trying to add to this example the ability to assign each maker a custom Icon. Given the linked example:

var locations = [   
['Bondi Beach', -33.890542, 151.274856, 4],   
['Coogee Beach', -33.923036, 151.259052, 5],   
['Cronulla Beach', -34.028249, 151.157507, 3],   
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],   
['Maroubra Beach', -33.950198, 151.259302, 1] ]; 

我在想:

var locations = [   
['Bondi Beach', -33.890542, 151.274856, 4,'images/icon1.png'],   
['Coogee Beach', -33.923036, 151.259052, 5,'images/icon2.png' ],   
['Cronulla Beach', -34.028249, 151.157507, 3,'images/icon3.png' ],   
['Manly Beach', -33.80010128657071, 151.28747820854187, 2,'images/icon4.png' ],   
['Maroubra Beach', -33.950198, 151.259302, 1,'images/icon5.png' ] ]; 

提前感谢您的回答!

推荐答案

这里是一个简单的JSFiddle Demo 显示使用以下代码创建的自定义标记.创建标记时,您可以使用 icon 参数自定义标记的图标.例如:

Here is a simple JSFiddle Demo showing a custom marker created with the following codes. You can customize the icon of your marker with the icon parameter when creating a marker. For instance:

var marker = new google.maps.Marker({
   position: latlng,
   map: map,
   icon: 'http://www.kjftw.com/gmap/images/icons/numeric/red10.png',
   zIndex: Math.round(latlng.lat() * -100000) << 5
});

您可以使用自定义标记图像的位置设置图标参数.或者在你的情况下是这样的:

You can set the icon param with the location of your custom marker image. Or in your case something like this:

for(var i=0; i<locations.length; i++){
     myGlobalMarker.push(new google.maps.Marker({
          position: new google.maps.LatLng(locations[i][1], locations[i][2]),
          map: map,
          icon: locations[i][4],
          title: locations[i][0]
     });
}

假设 locations[i][1] 是纬度,locations[i][2] 是 lng.这未经测试,但它是您创建自定义图标的基础.myGlobalMarker 是一个包含所有标记的全局变量数组.我不是 100% 确定图标位置是否需要绝对 URL 或相对 URL.但是,在我的 Google Map 应用程序中,我能够使用相对路径在我的标记上显示自定义图标.这是最终结果:

Where assuming locations[i][1] is the lat and locations[i][2] is the lng. This is not tested, but it's a base for what you can do to create custom icon. myGlobalMarker is a global variable array that holds all markers. I am not 100% sure if absolute URL or relative URL is needed for the icon location. However, in my Google Map app i was able to use relative path to display custom icon on my marker. This is the final result:

这篇关于Google Maps JS API v3 - 带有自定义标记的简单多标记示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆