【问题标题】:Cannot display google map route (API) with JQuery and html无法使用 JQuery 和 html 显示谷歌地图路线(API)
【发布时间】:2013-11-10 10:14:09
【问题描述】:

我正在尝试按照this 教程的说明填充谷歌地图路线。 地图没有显示,我开始调查问题的根源。 我可以看到在将每个 GPS 条目添加到数组中,输出对象如下所示: 我相信它具有任何对象项目的名称(每个主题都称为 0)。 有人可以告诉我它是否应该是这样的吗?如果不是,那是什么问题?

代码(相关部分):

/*
 * Function that convert the objects into computed data 
 */
function get_total_km($object_key) {
    // Get all the GPS data for the specific workout
    var data = window.localStorage.getItem($object_key);
    // Turn the stringified GPS data back into a JS object
    data = jQuery.parseJSON(data);

    if (data) {
        // Calculate the total distance travelled
        total_km = 0;
        for (i = 0; i < data.length; i++) {

            if (i === (data.length - 1)) {
                break;
            }

            total_km += gps_distance(data[i].coords.latitude, data[i].coords.longitude, data[i + 1].coords.latitude, data[i + 1].coords.longitude);
        }

        total_km_rounded = parseFloat(total_km.toFixed(2));
        // Calculate the total time taken for the track
        start_time = new Date(data[0].timestamp).getTime();
        //Seperated start_time_public for returning object
        start_time_public = new Date(data[0].timestamp).getHours();
        date_id = new Date(data[0].timestamp).getDay();
        end_time = new Date(data[data.length - 1].timestamp).getTime();
        total_time_ms = end_time - start_time;
        total_time_s = total_time_ms / 1000;
        final_time_m = Math.floor(total_time_s / 60);
        final_time_s = Math.floor(total_time_s - (final_time_m * 60));
//        console.log({total_km_rounded: total_km_rounded, final_time_m: final_time_m, final_time_s: final_time_s});
        return ({total_km_rounded: total_km_rounded, final_time_m: final_time_m, final_time_s: final_time_s, date_id: date_id, start_time_public: start_time_public, data: data});
    }
}

                    // Set the initial Lat and Long of the Google Map
                    var myLatLng = new google.maps.LatLng(get_total_km(key).data[0].coords.latitude, get_total_km(key).data[0].coords.longitude);
                    // Google Map options
                    var myOptions = {
                        zoom: 15,
                        center: myLatLng,
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
                    // Create the Google Map, set options
                    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

                    var trackCoords = [];
                    // Add each GPS entry to an array
                    for (ion = 1; ion < get_total_km(key).data.length; ion++) {
                        if (ion === (get_total_km(key).data.length - 1)){
                            break;
                        }
                        var dataa = get_total_km(key);
                        trackCoords.push(new google.maps.LatLng(dataa.data[ion].coords.latitude, dataa.data[ion].coords.longitude));
                    }
                    console.log(trackCoords);
                    // Plot the GPS entries as a line on the Google Map
                    var trackPath = new google.maps.Polyline({
                        path: trackCoords,
                        strokeColor: "#FF0000",
                        strokeOpacity: 1.0,
                        strokeWeight: 2
                    });
                    // Apply the line to the map
                    trackPath.setMap(map);
                });

【问题讨论】:

    标签: jquery google-maps-api-3


    【解决方案1】:

    您的 GPS 对象与 get_total_km 函数中预期的格式不匹配。

    您需要将 GPS 对象格式化为下一个格式:

    [
        {coords:{latitude:some, longitude:some}},  
        {coords:{latitude:some, longitude:some}}
    ]
    

    【讨论】:

    • 我已更改为:trackCoords.push(new google.maps.LatLng({coords:{latitude: dataa.data[ion].coords.latitude, longitude: dataa.data[ion]. coords.longitude}}));现在对象结构化但 Lat Lng 为 NULL..
    • [O, O, O, O, O, O, O, O, O, O, O] 0: O nb: NaN ob: NaN proto: O 1: O nb: NaN ob: NaN proto: O 2: O
    • 当您调用 get_total_km 函数时,您的 GPS 对象必须已经是所需格式,即 get_total_km([ {coords:{latitude:some, longitude:some}}, {coords:{latitude:some , 经度: 一些}} ]);
    • 我已经尝试过你的答案,但它不起作用。这是我现在的 trackCode 对象(我可以在 Google 地图 api 中看到这是我应该拥有的对象结构): [O, O, O, O, O, O, O, O, O, O, O, O] 0:O nb:45.427393662508806 ob:-121.72107615977819 proto:O 1:O nb:45.427393662508806 ob:-121.72107615977819 proto:O 2:O 3:O 4:O
    【解决方案2】:

    这是一个愚蠢的问题... 问题是我不小心用相同的 id 名称命名了 2 个 div,这导致了错误。

    【讨论】:

      猜你喜欢
      • 2011-07-22
      • 1970-01-01
      • 2013-09-29
      • 2016-08-16
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 2011-08-10
      • 2012-07-28
      相关资源
      最近更新 更多