在Google 地图上实现做的标记相连接


Posted in Javascript onJanuary 05, 2015

这里仅仅是将谷歌地图API的使用方法告诉大家,算是抛砖引玉吧,由于某些原因,谷歌已经远离大家了。

<!DOCTYPE html>

<html>

<head>

<title>GeoLocation</title>

<meta name="viewport" content="initial-scale=1.0, user-scalable=no">

<meta charset="utf-8">

<style>

    html, body, #map-canvas {

        margin: 0;

        padding: 0;

        height: 100%;

    }

</style>

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>

<script>

    var map;

    var poly;

    function initialize() {

    var myLatlng = new google.maps.LatLng(31.1937077, 121.4158436);

    var locations = [

    ['test1, accuracy: 150m', 31.1937077, 121.4158436, 100],

    ['test2, accuracy: 150m', 31.2937077, 121.4158436, 100],

    ['test3, accuracy: 150m', 31.0937077, 121.2158436, 100],

    ['test4, accuracy: 150m', 31.3937077, 120.4158436, 100],

    ['test5, accuracy: 150m', 31.1637077, 120.4858436, 100],

    ['test6, accuracy: 150m', 31.1037077, 121.5158436, 100]

    ];

    var mapOptions = {

    zoom: 13,

    center: myLatlng,

    mapTypeId: google.maps.MapTypeId.ROADMAP

    };

    map = new google.maps.Map(document.getElementById('map-canvas'),

    mapOptions);

    // 线条设置

    var polyOptions = {

    strokeColor: '#00ff00',    // 颜色

    strokeOpacity: 1.0,    // 透明度

    strokeWeight: 4    // 宽度

    }

    poly = new google.maps.Polyline(polyOptions);

    poly.setMap(map);    // 装载

    /* 循环标出所有坐标 */

    /*for(var i=0; i<locations.length; i++){

    var loc = [];

    loc.push(locations[i][1]);

    loc.push(locations[i][2]);

        var path = poly.getPath();    //获取线条的坐标

        path.push(new google.maps.LatLng(loc[0], loc[1]));    //为线条添加标记坐标

    //生成标记图标

    marker = new google.maps.Marker({

        position: new google.maps.LatLng(loc[0], loc[1]),

        map: map

        // icon: "https://maps.gstatic.com/mapfiles/markers/marker_green.png"

    });

    }*/

    var marker, i, circle;

    var iwarray = [];

    var infoWindow;

    var latlngbounds = new google.maps.LatLngBounds();

    var iconYellow = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/icons/yellow-dot.png");

    for (i = 0; i < locations.length; i++) {

        var loc = [];

        loc.push(locations[i][1]);

        loc.push(locations[i][2]);

            var path = poly.getPath();    //获取线条的坐标

        path.push(new google.maps.LatLng(loc[0], loc[1]));

        var latlng = new google.maps.LatLng(locations[i][1], locations[i][2]);

        latlngbounds.extend(latlng);

    if (locations[i][0].indexOf("[Cached") == 0 || (locations[i][0].indexOf("[Multiple") == 0 && locations[i][0].indexOf("[Cached") >= 0 )) {

            marker = new google.maps.Marker({

                position: latlng,

                map: map,

                icon: iconYellow

            });

            var iw = '<div style="font-size: 12px;word-wrap:break-word;word-break:break-all;"><strong><font color="#FF0000">' + locations[i][0] + '<font></strong><div>';

        } else {

            marker = new google.maps.Marker({

                position: latlng,

                map: map

            });

            var iw = '<div style="font-size: 12px;word-wrap:break-word;word-break:break-all;"><strong><font color="#000000">' + locations[i][0] + '<font></strong><div>';

        }

        iwarray[i] = iw;

        google.maps.event.addListener(marker, 'mouseover', (function(marker,i){

                return function(){

                    infoWindow = new google.maps.InfoWindow({

                        content: iwarray[i],

                        maxWidth: 200,

                        pixelOffset: new google.maps.Size(0, 0)

                    });

                    infoWindow.open(map, marker);

                }

            })(marker,i));

        google.maps.event.addListener(marker, 'mouseout', function() {

            infoWindow.close();

        });

        circle = new google.maps.Circle({

            map: map,

            radius: locations[i][3],

            fillColor: '#0000AA',

            fillOpacity: 0.01,

            strokeWeight: 1,

            strokeColor: '#0000CC',

            strokeOpacity: 0.8

        });

        circle.bindTo('center', marker, 'position');

    }

    map.fitBounds(latlngbounds);

    var listener = google.maps.event.addListenerOnce(map, "idle", function()

        {

        var zoomLevel = parseInt(map.getZoom());

        if (zoomLevel > 13)

            map.setZoom(13);

        });

    }

    google.maps.event.addDomListener(window, 'load', initialize);

</script>

</head>

<body>

    <div id="map-canvas"></div>

</body>

</html>
Javascript 相关文章推荐
Javascript设置对象的ReadOnly属性(示例代码)
Dec 25 Javascript
javascript查询字符串参数的方法
Jan 28 Javascript
JS打字效果的动态菜单代码分享
Aug 21 Javascript
手机端js和html5刮刮卡效果
Sep 29 Javascript
javaScript基础详解
Jan 19 Javascript
很棒的vue弹窗组件
May 24 Javascript
Vue中正确使用jQuery的方法
Oct 30 jQuery
JS实现的贪吃蛇游戏完整实例
Jan 18 Javascript
非常漂亮的js烟花效果
Mar 10 Javascript
全局安装 Vue cli3 和 继续使用 Vue-cli2.x操作
Sep 08 Javascript
Vue组件生命周期运行原理解析
Nov 25 Vue.js
JS算法教程之字符串去重与字符串反转
Dec 15 Javascript
js获取时间并实现字符串和时间戳之间的转换
Jan 05 #Javascript
jQuery中attr()方法用法实例
Jan 05 #Javascript
jQuery实现自定义下拉列表
Jan 05 #Javascript
使用javascript实现监控视频播放并打印日志
Jan 05 #Javascript
jQuery圆形统计图开发实例
Jan 04 #Javascript
jQuery中:selected选择器用法实例
Jan 04 #Javascript
jQuery中:checked选择器用法实例
Jan 04 #Javascript
You might like
随时给自己贴的图片加文字的php代码
2007/03/08 PHP
PHPUnit安装及使用示例
2014/10/29 PHP
php传值赋值和传地址赋值用法实例分析
2015/06/20 PHP
JS弹出窗口代码大全(详细整理)
2012/12/21 Javascript
jQuery contains过滤器实现精确匹配使用方法
2013/04/12 Javascript
js判断客户端是iOS还是Android等移动终端的方法
2013/12/11 Javascript
用Jquery实现滚动新闻
2014/02/12 Javascript
jQuery简单tab切换效果实现方法
2015/04/08 Javascript
js中数组结合字符串实现查找(屏蔽广告判断url等)
2016/03/30 Javascript
Bootstrap Paginator分页插件使用方法详解
2016/05/30 Javascript
JS简单获取及显示当前时间的方法
2016/08/03 Javascript
JavaScript 限制文本框不可输入英文单双引号的方法
2016/12/20 Javascript
jQuery简单实现MD5加密的方法
2017/03/03 Javascript
jQuery树插件zTree使用方法详解
2017/05/02 jQuery
微信小程序request请求后台接口php的实例详解
2017/09/20 Javascript
react-native组件中NavigatorIOS和ListView结合使用的方法
2017/09/30 Javascript
手把手教你使用vue-cli脚手架(图文解析)
2017/11/08 Javascript
vue基础之事件v-onclick=&quot;函数&quot;用法示例
2019/03/11 Javascript
JQuery获取可视区尺寸和文档尺寸及制作悬浮菜单示例
2019/05/14 jQuery
jQuery编写QQ简易聊天框
2020/08/27 jQuery
Javascript异步流程控制之串行执行详解
2020/09/27 Javascript
[08:44]DOTA2发布会群星聚首 我们都是刀塔人
2014/03/21 DOTA
Python卸载模块的方法汇总
2016/06/07 Python
python3+PyQt5使用数据库表视图
2018/04/24 Python
python 定时任务去检测服务器端口是否通的实例
2019/01/26 Python
python生成requirements.txt的两种方法
2019/09/18 Python
wxPython绘图模块wxPyPlot实现数据可视化
2019/11/19 Python
基于python+selenium的二次封装的实现
2020/01/06 Python
Ranorex通过Python将报告发送到邮箱的方法
2020/01/12 Python
python 实现两个npy档案合并
2020/07/01 Python
python数据抓取3种方法总结
2021/02/07 Python
基于Html5实现的react拖拽排序组件示例
2018/08/13 HTML / CSS
律师授权委托书范本
2014/10/07 职场文书
客房领班岗位职责
2015/02/11 职场文书
学雷锋团日活动总结
2015/05/06 职场文书
mysql如何能有效防止删库跑路
2021/10/05 MySQL