如何更改谷歌地图标记上的图标
Posted
技术标签:
【中文标题】如何更改谷歌地图标记上的图标【英文标题】:How to change icon on Google map marker 【发布时间】:2013-10-15 06:19:48 【问题描述】:我想在 Google 地图上使用我的自定义图标,并在代码中添加图标 url。但它仍然没有反映在地图上。任何人都可以建议,我在这里缺少什么。为什么添加图标网址“http://google-maps-icons.googlecode.com/files/sailboat-tourism.png”后图标没有变化。
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var markers = [
"title": 'This is title',
"lat": '-37.801578',
"lng": '145.060508',
"icon": 'http://google-maps-icons.googlecode.com/files/sailboat-tourism.png',
"description": 'Vikash Rathee. <strong> This is test Description</strong> <br/><a href="http://www.pricingindia.in/pincode.aspx">Pin Code by
City</a>'
];
</script>
<script type="text/javascript">
window.onload = function ()
var mapOptions =
center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
zoom: 10,
flat: true,
styles: [ "stylers": [ "hue": "#4bd6bf" , "gamma": "1.58" ] ],
mapTypeId: google.maps.MapTypeId.ROADMAP
;
var infoWindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
for (i = 0; i < markers.length; i++)
var data = markers[i]
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
var marker = new google.maps.Marker(
position: myLatlng,
map: map,
title: data.title
);
(function (marker, data)
google.maps.event.addListener(marker, "click", function (e)
infoWindow.setContent(data.description);
infoWindow.open(map, marker);
);
)(marker, data);
</script>
<div id="dvMap" style="width: 100%; height: 100%">
</div>
【问题讨论】:
【参考方案1】:var marker = new google.maps.Marker(
position: new google.maps.LatLng(23.016427,72.571156),
map: map,
icon: 'images/map_marker_icon.png',
title: 'Hi..!'
);
仅在图标上应用本地路径
【讨论】:
【参考方案2】:var marker = new google.maps.Marker(
position: myLatLng,
map: map,
icon: 'your-icon.png'
);
【讨论】:
虽然此答案可能是正确且有用的,但如果您 include some explanation along with it 解释它如何帮助解决问题,则最好。如果有变化(可能不相关)导致它停止工作并且用户需要了解它曾经是如何工作的,这在未来变得特别有用。谢谢!【参考方案3】:我们可以更改标记的图标,我在右键单击事件中做到了。 让我们看看它是否适合你......
// Create a Marker
var marker = new google.maps.Marker(
position: location,
map: map,
title:'Sample Tool Tip'
);
// Set Icon on any event
google.maps.event.addListener(marker, "rightclick", function()
marker.setIcon('blank.png'); // set image path here...
);
【讨论】:
【参考方案4】:Manish,Eden 根据您的建议:这里是代码。但仍显示红色(默认)图标。
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var markers = [
"title": 'This is title',
"lat": '-37.801578',
"lng": '145.060508',
"icon": 'http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_green.png',
"description": 'Vikash Rathee. <br/><a href="http://www.pricingindia.in/pincode.aspx">Pin Code by City</a>'
];
</script>
<script type="text/javascript">
window.onload = function ()
var mapOptions =
center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
zoom: 10,
flat: true,
styles: [ "stylers": [ "hue": "#4bd6bf" , "gamma": "1.58" ] ],
mapTypeId: google.maps.MapTypeId.ROADMAP
;
var infoWindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
for (i = 0; i < markers.length; i++)
var data = markers[i]
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
var marker = new google.maps.Marker(
position: myLatlng,
map: map,
icon: markers[i][3],
title: data.title
);
(function (marker, data)
google.maps.event.addListener(marker, "click", function (e)
infoWindow.setContent(data.description);
infoWindow.open(map, marker);
);
)(marker, data);
</script>
<div id="dvMap" style="width: 100%; height: 100%">
</div>
【讨论】:
【参考方案5】:试试这个
var locations = [
['San Francisco: Power Outage', 37.7749295, -122.4194155,'http://labs.google.com/ridefinder/images/mm_20_purple.png'],
['Sausalito', 37.8590937, -122.4852507,'http://labs.google.com/ridefinder/images/mm_20_red.png'],
['Sacramento', 38.5815719, -121.4943996,'http://labs.google.com/ridefinder/images/mm_20_green.png'],
['Soledad', 36.424687, -121.3263187,'http://labs.google.com/ridefinder/images/mm_20_blue.png'],
['Shingletown', 40.4923784, -121.8891586,'http://labs.google.com/ridefinder/images/mm_20_yellow.png']
];
//inside the loop
marker = new google.maps.Marker(
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
icon: locations[i][3]
);
【讨论】:
Manish,没用。能否请您在 fiddle 或 cssdeck 上发布完整的代码或演示? marker = new google.maps.Marker( position:myLatlng , map: map, icon: locations[0][3] );检查这个从数组中获取第一个图像图标。 不工作 Manish。您能否在 fiddle 或 cssdeck.com 上的某处发布您建议的工作代码或给我发电子邮件:vicky.rathee2005@gmail.com【参考方案6】:您必须添加目标地图:
var markers = [
"title": 'This is title',
"lat": '-37.801578',
"lng": '145.060508',
"map": map,
"icon": 'http://google-maps-icons.googlecode.com/files/sailboat-tourism.png',
"description": 'Vikash Rathee. <strong> This is test Description</strong> <br/><a href="http://www.pricingindia.in/pincode.aspx">Pin Code by
City</a>'
];
【讨论】:
EdenSource,没有工作。能否请您在 fiddle 或 cssdeck 上发布完整的代码或演示? icon:markers[i][3],可能是错误的。将其替换为“图标”:'google-maps-icons.googlecode.com/files/sailboat-tourism.png',以上是关于如何更改谷歌地图标记上的图标的主要内容,如果未能解决你的问题,请参考以下文章