作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我制作了一个 html 文件,该文件从 XML 文件中获取数据并使用其中的数据在 map 上绘制两个标记,有什么方法可以连接这两个没有固定位置的标记这是我的代码标记。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Simple markers</title>
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<script>
var map;
window.onload=function ()
{
var myLatlng = new google.maps.LatLng(0,0);
var mapOptions = {
zoom: 4,
center: myLatlng
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
//google.maps.event.addDomListener(window, 'load', mapready);
getdata();
setInterval(function () {getdata()}, 1000);
}
function getdata()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","xxxxxxxxxxx",true);
xmlhttp.onreadystatechange=function () {gotdata()};
xmlhttp.send();
}
var lastCoordinates={};
var polyline = new google.maps.Polyline({map:map})
var path = [];
function gotdata(){
if (xmlhttp.readyState == 4){
var d = xmlhttp.responseXML.documentElement
//innerHTML shouldn't work for XML-Nodes
y = d.getElementsByTagName("y")[0].textContent,
x = d.getElementsByTagName("x")[0].textContent,
h = [x,y].join('_');
if(lastCoordinates[h]){
return;
}
lastCoordinates[h]= new google.maps.Marker({
position: new google.maps.LatLng(x,y),
map: map,
title: 'YAY'
});
path.push(lastcoordinates[h].getPosition());
if (path.length >= 2) {
polyline.setPath(path);
}
}
}
这是我的 XML 文件中的内容
<Location>
<x>42</x>
<y>14</y>
</Location>
它从中获取 x 和 y 数据,当我更改它时, map 会创建另一个文件。
最佳答案
创建 polyline (有关可用选项,请参阅 documentation)
var polyline = new google.maps.Polyline({
// set desired options for color, opacity, width, etc.
strokeColor:"#0000FF", // blue (RRGGBB, R=red, G=green, B=blue)
strokeOpacity: 0.4 // opacity of line
}); // create the polyline (global)
var path = []; // global variable to hold all the past locations
用你的分数更新它
function gotdata(){
if (xmlhttp.readyState == 4){
var d = xmlhttp.responseXML.documentElement
//innerHTML shouldn't work for XML-Nodes
y = d.getElementsByTagName("y")[0].textContent,
x = d.getElementsByTagName("x")[0].textContent,
h = [x,y].join('_');
if(lastCoordinates[h]){
return;
}
lastCoordinates[h]= new google.maps.Marker({
position: new google.maps.LatLng(x,y),
map: map,
title: 'YAY'
});
path.push(lastCoordinates[h].getPosition());
if (path.length >= 2) {
// display the polyline once it has more than one point
polyline.setMap(map);
polyline.setPath(path);
}
}
}
代码片段:
var geocoder;
var map;
var lastCoordinates = [];
var count = 0;
var polyline = new google.maps.Polyline({
// set desired options for color width
strokeColor:"#0000FF", // blue (RRGGBB, R=red, G=green, B=blue)
strokeOpacity: 0.4 // opacity of line
}); // create the polyline (global)
var path = []; // global variable to hold all the past locations
function initialize() {
map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
setInterval(gotdata, 1000);
}
google.maps.event.addDomListener(window, "load", initialize);
function gotdata() {
// if (xmlhttp.readyState == 4){
count++;
// var d = xmlhttp.responseXML.documentElement
//innerHTML shouldn't work for XML-Nodes
y = count * 0.01; // d.getElementsByTagName("y")[0].textContent,
x = count * 0.01; //d.getElementsByTagName("x")[0].textContent,
h = [x, y].join('_');
if (lastCoordinates[h]) {
return;
}
lastCoordinates[h] = new google.maps.Marker({
position: new google.maps.LatLng(x, y),
map: map,
title: 'YAY'
});
map.panTo(lastCoordinates[h].getPosition());
path.push(lastCoordinates[h].getPosition());
if (path.length >= 2) {
// display the polyline once it has more than one point
polyline.setMap(map);
polyline.setPath(path);
}
// }
}
html, body, #map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas" style="border: 2px solid #3872ac;"></div>
关于javascript - 如何在谷歌地图 api3 中用一条线连接两个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30667509/
我是一名优秀的程序员,十分优秀!