在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 防止刷新,后退,关闭
Aug 07 Javascript
使用jquery的ajax需要注意的地方dataType的设置
Aug 12 Javascript
javascript函数重载解决方案分享
Feb 19 Javascript
Javascript学习笔记之函数篇(六) : 作用域与命名空间
Nov 23 Javascript
javascript中html字符串转化为jquery dom对象的方法
Aug 27 Javascript
Function.prototype.apply()与Function.prototype.call()小结
Apr 27 Javascript
VUE实现一个分页组件的示例
Sep 13 Javascript
JS闭包的几种常见形式实例详解
Sep 16 Javascript
vue.js或js实现中文A-Z排序的方法
Mar 08 Javascript
vue 中filter的多种用法
Apr 26 Javascript
微信公众号平台接口开发 菜单管理的实现
Aug 14 Javascript
解决vue侦听器watch,调用this时出现undefined的问题
Oct 30 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实现无限级分类
2014/12/24 PHP
php批量删除超链接的实现方法
2015/10/19 PHP
Yii2 中实现单点登录的方法
2018/03/09 PHP
Laravel获取所有的数据库表及结构的方法
2019/10/10 PHP
javascript 函数式编程
2007/08/16 Javascript
javascript 支持链式调用的异步调用框架Async.Operation
2009/08/04 Javascript
网页前台通过js非法字符过滤代码(骂人的话等等)
2010/05/26 Javascript
jquery插件制作 提示框插件实现代码
2012/08/17 Javascript
replace()方法查找字符使用示例
2013/10/28 Javascript
Jquery1.9.1源码分析系列(六)延时对象应用之jQuery.ready
2015/11/24 Javascript
全面解析DOM操作和jQuery实现选项移动操作代码分享
2016/06/07 Javascript
Node.js利用Net模块实现多人命令行聊天室的方法
2016/12/23 Javascript
highcharts 在angular中的使用示例代码
2017/09/20 Javascript
JS实现的简单表单验证功能示例
2017/10/13 Javascript
用vue封装插件并发布到npm的方法步骤
2017/10/18 Javascript
JS伪继承prototype实现方法示例
2018/06/20 Javascript
JavaScript中常用的简洁高级技巧总结
2019/03/10 Javascript
jQuery内容选择器与表单选择器实例分析
2019/06/28 jQuery
VueCli4项目配置反向代理proxy的方法步骤
2020/05/17 Javascript
学前端,css与javascript重难点浅析
2020/06/11 Javascript
Python中unittest模块做UT(单元测试)使用实例
2015/06/12 Python
浅谈python 四种数值类型(int,long,float,complex)
2016/06/08 Python
Python中max函数用于二维列表的实例
2018/04/03 Python
Python实现统计给定字符串中重复模式最高子串功能示例
2018/05/16 Python
Python OOP类中的几种函数或方法总结
2019/02/22 Python
Becextech新西兰:数码单反相机和手机在线商店
2018/04/27 全球购物
Blancsom美国/加拿大:服装和生活用品供应商
2018/07/27 全球购物
internal修饰符起什么作用
2013/12/16 面试题
yy婚礼司仪主持词
2014/03/14 职场文书
市场营销毕业生自荐信范文
2014/04/01 职场文书
高职教师先进事迹材料
2014/08/24 职场文书
承诺书模板大全
2015/05/04 职场文书
严以律己学习心得体会
2016/01/13 职场文书
幽默口才训练经典句子(48句)
2019/08/19 职场文书
Python获取百度热搜的完整代码
2021/04/07 Python
SQL Server使用T-SQL语句批处理
2022/05/20 SQL Server