来自 geoJSON 的 Google 地图航点

2024-05-08

我想从 geoJSON 文件加载行程。 目前来说,它是有效的,但只有两点。

但我需要添加 4 或 5 个航路点。我的代码只读取前两个点并将它们设置为起点和目的地。

这是我的代码

 google.maps.event.addListener(map.data, 'addfeature', function (e) {

    if (e.feature.getGeometry().getType() === 'Point') {
        map.setCenter(e.feature.getGeometry().get());

        if (!origin) origin = e.feature.getGeometry().get(); //if origin does not exist

        else if (!destination) {
            destination = e.feature.getGeometry().get();

            calculate();
        }
    }
});

任何想法 ? 我必须创建一个循环吗? 或者更改航点的 json 代码?

这是我的 json :

{
"type": "FeatureCollection",
"features":

 [

    { "type": "Feature",
        "geometry": {"type": "Point", "coordinates": [-73.562686, 45.4960413]},
        "properties": {"prop0": "value0"}
    },


    { "type": "Feature",
        "geometry": {"type": "Point", "coordinates": [-73.568367, 45.4933086]},
        "properties": {"prop0": "value0"}
    }
]

  }

Thanks !


工作小提琴 http://jsfiddle.net/geocodezip/dm6wu1r2/

function calculate() {
    var request = {
        origin: origin,
        waypoints: waypts,
        destination: destination,
        travelMode: google.maps.TravelMode.DRIVING
    };
    directionsDisplay.setPanel(document.getElementById('directions-panel'));
    directionsService.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
        }
    });
}

// global variables
var origin = null;
var destination = null;
var waypts = [];
var infowindow = new google.maps.InfoWindow();
var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();

function initialize() {
    // Create a simple map.
    features = [];
    map = new google.maps.Map(document.getElementById('map-canvas'), {
        zoom: 4,
        center: {
            lat: -28,
            lng: 137.883
        }
    });
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById('directions-panel'));
    google.maps.event.addListener(map, 'click', function () {
        infowindow.close();
    });
    // process the loaded GeoJSON data.
    google.maps.event.addListener(map.data, 'addfeature', function (e) {
        if (e.feature.getGeometry().getType() === 'Point') {
            map.setCenter(e.feature.getGeometry().get());
            // set the origin to the first point
            if (!origin) origin = e.feature.getGeometry().get();
            // set the destination to the second point
            else waypts.push({
                location: e.feature.getGeometry().get(),
                stopover: true
            });
            // calculate the directions once both origin and destination are set 
            // calculate();
        }
    });
    google.maps.event.addListenerOnce(map, 'idle', function () {
        if (!destination) {
            destination = waypts.pop();
            destination = destination.location;
            // calculate the directions once both origin and destination are set 
            calculate();
        }
    });
    map.data.addGeoJson(data);
}

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

为了解决 Molle 博士关于idle在加载数据层之前触发事件,您可以创建自定义data_idle事件,并在处理完 GeoJson 中的所有点后触发该事件。

更新了小提琴 http://jsfiddle.net/geocodezip/z5auh3tw/

var features_added = 0;
function initialize() {
    // Create a simple map.
    features = [];
    map = new google.maps.Map(document.getElementById('map-canvas'), {
        zoom: 4,
        center: {
            lat: -28,
            lng: 137.883
        }
    });
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById('directions-panel'));
    google.maps.event.addListener(map, 'click', function () {
        infowindow.close();
    });
    // process the loaded GeoJSON data.
    google.maps.event.addListener(map.data, 'addfeature', function (e) {
        if (e.feature.getGeometry().getType() === 'Point') {
            features_added++;
            map.setCenter(e.feature.getGeometry().get());
            // set the origin to the first point
            if (!origin) origin = e.feature.getGeometry().get();
            // set the destination to the second point
            else waypts.push({
                location: e.feature.getGeometry().get(),
                stopover: true
            });
            setTimeout(function() {features_added--; if (features_added <= 0) google.maps.event.trigger(map, 'data_idle');
                }, 500);
        }
    });
    google.maps.event.addListenerOnce(map, 'data_idle', function () {
        if (!destination) {
            destination = waypts.pop();
            destination = destination.location;
            // calculate the directions once both origin and destination are set 
            calculate();
        }
    });
    map.data.loadGeoJson("http://www.geocodezip.com/directions.json.txt");
}

google.maps.event.addDomListener(window, 'load', initialize);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

来自 geoJSON 的 Google 地图航点 的相关文章

随机推荐

  • 使用 jest 存根函数

    有没有办法使用 jest API 来存根函数 我习惯于使用 sinon 存根 在这里我可以使用存根为来自我的测试单元的任何函数调用编写单元测试 http sinonjs org releases v1 17 7 stubs http sin
  • SWI Prolog 使用的检查优化会发生什么情况?

    去引用SICStus Prolog 手册 https sicstus sics se sicstus docs 3 12 9 html sicstus Occur html 逻辑编程背后的通常数学理论禁止 创建循环项 规定发生检查应该是 每
  • Firefox 扩展中的 localStorage

    我正在尝试从 Firefox 扩展访问页面的 localStorage 我的理解是content给出了参考window当前页面的 当我尝试访问页面的 localStorage 时content localStorage 我想我正在得到它的参
  • webrtc - 视频出现斑点,但它仍然是黑色的

    我使用 chrome 21 运行我的 webrtc 代码 如果我在同一个 chrome 中打开两个选项卡 然后打开其中包含 webrtc 代码的页面 一个选项卡用于发送视频流 一个选项卡用于接收视频流 效果很好 但是 如果我使用两种隐身模式
  • 当数据源中只有 1 项时 FormView 不显示 PagerTemplate

    我有一个带有自定义 PagerTemplate 的 FormView 控件和我自己的分页 LinkBut ton 一切都很好 直到我加载的数据集仅包含一个记录 项目并完全隐藏 PagerTemplate 我在网上搜索了一下 找到了几个答案
  • 有没有办法只从 python 列表中输出数字?

    简单的问题 list 1 asdada 1 123131 131 blaa adaraerada 0 000001 34 12451235265 stackoverflow is awesome 我想创建一个list 2这样它只包含数字 l
  • AWS ALB 截断 HTTP 响应

    我有一个带有目标组的 ALB 和运行 PHP API 的 ECS 集群 我正在尝试查询 API 以获得 CSV 响应 但如果请求通过 ALB 我会得到被截断的结果 当我通过 SSH 连接到运行集群的 EC2 实例并尝试手动运行curl 通过
  • WP 用户注册 - 也可以立即选择他/她的密码

    这是一个非常简短的前端注册指南 但我在密码方面遇到了一个小问题 我禁用了用户注册时发送的带有密码生成的电子邮件 Don t Send Notification Email To Registered User if function exi
  • 获取我的 VC++ 代码使用的符号列表

    我正在构建一个处理 VC 源代码的工具 为此 我需要获取符号列表 包括我的代码使用的局部变量名称及其类型 我知道Visual C 2010已经提供了一个 bsc文件 允许对象浏览器快速定位符号 但这是一个交互式工具 我需要获取文件中的符号列
  • 如何在ListView中添加页脚?

    我正在开发一个应用程序 在我的应用程序中 我使用 Listview 使用 dom 解析显示数据 我想在列表视图中添加页脚 当我单击页脚时将更多数据添加到列表视图中 我附加了图像 我想要该设计和流程 请参考image1和imgae2 我在红色
  • 如何更改 PyGame 中声音或音乐的音量?

    如何更改 PyGame 中的音量 例如通过设置更改音量 我制作了 UI 元素 只需要知道如何更改音量即可 我知道我说不清楚 但你可以理解我 请帮忙 更改音量取决于您是否正在播放pygame mixer Sound https www pyg
  • 如何在 data-disable-with 上设置 html 到 Rails Submit_tag

    我有一个使用 bootstrap 的 RoR 应用程序 我正在尝试将 fontawesome html 图标标签应用于 Submit tag 帮助程序 但它似乎不受支持 当我单击 提交 时 禁用内容仅显示为字符串 而不是解释为 html 尽
  • Bellman-Ford 算法检测什么?负重还是负循环?

    如果给定一个图 现在我们要从源头计算最短路径 现在 如果一条边具有负权重 但在到达目的地时有边到后边返回到该边 我的意思是如果没有循环 那么我们就没有负循环 但是here http en wikipedia org wiki Bellman
  • 跟踪 pthread 调度

    我想做的是创建某种图表 详细说明 Linux 中 两个 线程的执行情况 我不需要查看线程的作用 只需查看它们何时被安排以及持续多长时间 基本上是一条时间线 在过去的几个小时里 我一直在互联网上搜索跟踪 pthread 调度的方法 不幸的是
  • 将 KeyUp 作为参数传递 WPF 命令绑定文本框

    我有一个文本框 KeyUp 事件触发器连接到 WPF 中的命令 我需要将按下的实际键作为命令参数传递 该命令执行得很好 但处理它的代码需要知道按下的实际键 记住这可能是一个回车键或不仅仅是一个字母的任何键 所以我无法从 TextBox te
  • 为什么 `boost::any` 比 `void*` 更好?

    有什么先天优势boost any and boost any cast提供超过使用void and dynamic cast 优点是boost any比类型安全得多void E g int i 5 void p i static cast
  • nested_form/cocoon:可以将表行用于嵌套字段吗?

    我通常不使用表格作为表单 但是当有嵌套表单时 使用nested form或cocoon gem时 可以将每组表单元素放在表格行中吗 对我来说 这似乎非常直观 表中的每一行都代表一个对象 但是 nested form 和 cocoon gem
  • 提取 zip 文件时 Parallel.ForEach 抛出异常

    我正在阅读 zip 文件的内容并尝试提取它们 var allZipEntries ZipFile Open zipFileFullPath ZipArchiveMode Read Entries 现在 如果我提取使用 Foreach 循环
  • ASP.NET 中的 JavaScript 事件处理程序

    我有以下 iframe 控件 旨在成为类似 facebook 的按钮 iframe gt 我在上面定义了 javascript 函数 如下所示
  • 来自 geoJSON 的 Google 地图航点

    我想从 geoJSON 文件加载行程 目前来说 它是有效的 但只有两点 但我需要添加 4 或 5 个航路点 我的代码只读取前两个点并将它们设置为起点和目的地 这是我的代码 google maps event addListener map