简体   繁体   English

Google Maps Polyline的数据库坐标在所有浏览器中均不起作用

[英]Google Maps Polyline with coordinates from database not working in all browsers

For a project of mine I am wanting to take a collection of coordinates from a database and show them on a map with poly-lines connecting these points. 对于我的一个项目,我想从数据库中获取坐标集合,并在地图上用连接这些点的折线显示它们。 To do this I am using jQuery (for the first time) to retrieve the information from the database and place it into an array. 为此,我第一次使用jQuery从数据库中检索信息并将其放入数组中。 This array is then used to create points to be displayed on the map. 然后,使用此数组创建要在地图上显示的点。 The odd thing is that it works perfectly in chrome, but not in firefox or internet explorer (An example is below). 奇怪的是,它在chrome中可以完美运行,但在Firefox或Internet Explorer中却无法运行(下面是一个示例)。

I have uploaded the page to my site here you so can see it with the database connection working. 我已经将该页面上传到我的站点您可以在数据库连接正常的情况下看到它。 I also created a similar version of the page but with the coordinates entered manually (link is below). 我还创建了页面的类似版本,但是使用了手动输入的坐标(下面是链接)。 I will also give the files below but here is a code snippet of where I think the issues is: 我还将在下面提供文件,但这是我认为问题所在的代码段:

// Creates an array which will store all the points to be connected on the map
    var flightPlanCoordinates = [];
    // takes the latitude and longitude information from latlngdata and creates points to be pushed onto the array
    for(i=0;i < latlngdata.length;i=i+2) {
        var point =new google.maps.LatLng(latlngdata[i],latlngdata[i+1]);
        flightPlanCoordinates.push(point);
    }


    // specifies the polyline
    flightPath = new google.maps.Polyline({
        path: flightPlanCoordinates,
        strokeColor: '#FF0000',
        strokeOpacity: 1.0,
        strokeWeight: 2
    });
    // adds the line
    addLine();

I get the information for latlngdata here: 我在这里获取有关latlngdata的信息:

$.ajax({
    url: "shoreData.php",
    dataType: "json",
    // if the data is succesfully found
    success: function (data) {
        // place the data into latlngdata
        latlngdata = data;
        // to show that the correct data is collected (for feedback purposes) the first four elements are displayed
        $("#status").html("Lat0: " + data[0] + "<br>" +
            "Lon0: " + data[1] + "<br>" +
            "Lat1: " + data[2] + "<br>" +
            "Lon1: " + data[3] + "<br>"
        );
    },
    // if there is an error shows so in #status
    error: function (result) {
        // update status
        $("#status").html("Error :(");
    }
});

As I am only allowed to use two links as a mere beginner to stackoverflow.com I have created a simple html file with links to everything: 由于我只允许使用两个链接作为stackoverflow.com的初学者,因此我创建了一个简单的html文件,其中包含指向所有内容的链接:

Everything 一切

  • Real is the file in question 真实是有问题的文件
  • Images shows a comparison between multiple browsers 图片显示了多个浏览器之间的比较
  • Goal shows my manually entered version 目标显示了我手动输入的版本
  • Files is a zip file containing my sources files for both versions 文件是一个zip文件,其中包含两个版本的源文件

Thanks a lot for reading this and if you have any ideas I would really appreciate it 非常感谢您阅读本文,如果您有任何想法,我将不胜感激

Theo 西奥

I think i found your error after some debugging: 我认为经过一些调试,我发现了您的错误:

You only had one LatLng Point defined with your function because you only set 2 Coordinates at the beginning! 您仅用函数定义了一个LatLng点,因为开始时只设置了2个坐标!

var latlngdata = [52.956281,4.760797];

Obviously you need at least 2 Points to draw a line, but you only give one, because a LatLng object always needs 2 Coordinates, which you provide, but you need to provide at least 4 Coordinates to be able to set 2 Points on the map where you can draw from. 显然,您至少需要2个点才能画一条线,因为LatLng对象始终需要您提供的2个坐标,但是您至少需要提供4个坐标才能在地图上设置2个点您可以从中借鉴。

I created a jsfiddle with a working example, which is a bit smaller then your code here, but you should be able to adapt it to your needs. 我创建了一个带有工作示例的jsfiddle ,该示例比此处的代码小一些,但是您应该能够使其适应您的需求。

The code is the following: 代码如下:

var map;
var latlngdata = [52.956281,4.760797, 14.354862, 21.4883214]; //Just added two more random Coordinates
var mapOptions = {
    // sets zoom
    zoom: 12,
    // sets center
    center: new google.maps.LatLng(latlngdata[0], latlngdata[1]),
    // sets map type
    mapTypeId: google.maps.MapTypeId.TERRAIN
};
map = new google.maps.Map($('#map')[0], mapOptions);

// Creates an array which will store all the points to be connected on the map
var flightPlanCoordinates = [];
// takes the latitude and longitude information from latlngdata and creates points to be pushed onto the array
for(i=0;i < latlngdata.length;i=i+2) {
    var point =new google.maps.LatLng(latlngdata[i],latlngdata[i+1]);
    flightPlanCoordinates.push(point);
}

// specifies the polyline
flightPath = new google.maps.Polyline({
    path: flightPlanCoordinates,
    strokeColor: '#FF0000',
    strokeOpacity: 1.0,
    strokeWeight: 2
});

// adds the line
flightPath.setMap(map);

The other think is that you Ajax Call is working, but you don't give the data coming from it to the map, you only display it above the map. 另一个想法是您正在使用Ajax Call,但是您没有将来自它的数据提供给地图,而是仅将其显示在地图上方。 I you would give the data to the map also all would work perfectly! 我将数据提供给地图,所有操作都将完美完成! :) :)

But then you should gurantee that the ajax call succeeded before trying to draw the line, if the drawing happens first no Line will be drawn because the data is not there. 但是然后您应该确保在尝试绘制线之前ajax调用成功,如果首先进行绘制,则不会绘制线,因为数据不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM