GOOGLE MAP 深入之 如何给地图更换显示标记图标

 时间:2024-10-12 09:07:29

1、// This example adds a marker to indicate the po衡痕贤伎sition// of Bondi Be锾攒揉敫ach in Sydney, Australiafunction initialize() { var mapOptions = { zoom: 4, center: new google.maps.LatLng(-33, 151) } var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); var image = 'images/beachflag.png'; var myLatLng = new google.maps.LatLng(-33.890542, 151.274856); var beachMarker = new google.maps.Marker({ position: myLatLng, map: map, icon: image });}google.maps.event.addDomListener(window, 'load', initialize);

GOOGLE MAP 深入之 如何给地图更换显示标记图标

2、 var image = 'images/beachflag.png'; var myLatLng = new google.maps.LatLng(-33.890542, 151.274856); 设置要显示标记图标的路径及标记图标在地图上显示的位置

3、var beachMarker = new google.maps.Marker({ position: myLatLng, map: map, icon: image }); 在 google.maps.Marker 里面,新增了 icon 属性,用来传入要显示标记图标的路径.

4、当然,也可以显示多个自定义的标记图标.function initialize() { var mapOptions = { zoom: 10, center: new google.maps.LatLng(-33.9, 151.2) } var map = new google.maps.Map(document.getElementById('map-canvas')mapOptions); setMarkers(map, beaches);}var beaches = [ ['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]];function setMarkers(map, locations) { // Add markers to the map// Marker sizes are expressed as a Size of X,Y // where the origin of the image (0,0) is located // in the top left of the image.// Origins, anchor positions and coordinates of the marker // increase in the X direction to the right and in // the Y direction down. var image = { url: 'images/beachflag.png', // This marker is 20 pixels wide by 32 pixels tall. size: new google.maps.Size(20, 32), // The origin for this image is 0,0. origin: new google.maps.Point(0,0), // The anchor for this image is the base of the flagpole at 0,32. anchor: new google.maps.Point(0, 32) }; // Shapes define the clickable region of the icon. // The type defines an HTML &lt;area&gt; element 'poly' which // traces out a polygon as a series of X,Y points. The final // coordinate closes the poly by connecting to the first // coordinate. var shape = { coords: [1, 1, 1, 20, 18, 20, 18 , 1], type: 'poly' }; for (var i = 0; i < locations.length; i++) { var beach = locations[i]; var myLatLng = new google.maps.LatLng(beach[1], beach[2]); var marker = new google.maps.Marker({ position: myLatLng, map: map, icon: image, shape: shape, title: beach[0], zIndex: beach[3] }); }}google.maps.event.addDomListener(window, 'load', initialize);

GOOGLE MAP 深入之 如何给地图更换显示标记图标

5、代码解析: var beaches = [ ['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]]; 显示多个自定义的标记图标,主要是要我们自定义一个关于经纬度及提示内容的数组.加入到 title 及 position 里面就能实现.

6、对于我们国内的开发者来说,现在访问谷歌不是一件容易的事情,这里我也只能给你们提供一些自己的小经验,还是推荐大家用百度的地图,在中国稳定性和可访问性是不需要担心的,至于百度地图效果怎么样,在后面我也会跟大家分享我的一些心得体会.如果你们有自己的一些问题我这里没有讲解到的,可以在下面进行提问.我会及时给大家解答.

  • FC版最终幻想3装备怎么卡
  • Google Earth怎么更改地标样式图标
  • 《无双大蛇Z》五星激难人物心得:上杉谦信
  • 我的世界batmod怎么安装
  • 怎么查看扩圈软件用户隐私协议
  • 热门搜索
    手抄报简单又漂亮 创建文明城手抄报 团结手抄报 饮食安全手抄报内容 关于保护地球的手抄报 青春手抄报内容 小学生读书手抄报内容 关于感恩父母手抄报 手抄报简单边框 关于交通的手抄报