获取json数据后在 地图上打点,根据 json不断移动点的位置

<?php

echo <<<_END
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>点标记</title>
    <link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css"/>
    <style>
        .marker {
            color: #ff6600;
            padding: 4px 10px;
            border: 1px solid #fff;
            white-space: nowrap;
            font-size: 12px;
            font-family: "";
            background-color: #0066ff;
        }
    </style>
    <script src="http://webapi.amap.com/maps?v=1.3&key=您申请的key值"></script>
    <script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
</head>
<body>
<div id="container"></div>
<script>
    var marker, map = new AMap.Map("container", {
        resizeEnable: true,
        center: [126.60580555556, 45.702363888889],
        zoom: 13
    });
    var getJSON = function(url) {
      return new Promise(function(resolve, reject) {
        var xhr = new XMLHttpRequest();
        xhr.open(‘get‘, url, true);
        xhr.responseType = ‘json‘;
        xhr.onload = function() {
          var status = xhr.status;
          if (status == 200) {
            resolve(xhr.response);
          } else {
            reject(status);
          }
        };
        xhr.send();
      });
    };

    getJSON(‘http://web.cellpies.com/api/driving/getVehicleLocationPoints?vehicleDeviceId=0400000000030603&timeType=4&startTime=2017-03-17%2013:00:00&stopTime=2017-03-17%2014:00:00‘).then(function(jdata) {
        //alert(‘Your Json result is:  ‘ + jdata); //you can comment this, i used it to debug
        //alert(jdata.data[0].gpsx);
        //alert(jdata.data[0].gpsy);
        window.i=0;
        //addMarker(jdata.data[i].gpsx,jdata.data[i].gpsy);
        setInterval(function () { addMarker(jdata.data[window.i].gpsx,jdata.data[window.i].gpsy); },"1000");

    }, function(status) { //error detection....
      alert(‘Something went wrong.‘);
    });

    // 实例化点标记
 function addMarker(v1,v2) {

     window.i+=10;
        marker = new AMap.Marker({
            icon: "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
            position: [v1,v2]
        });
        marker.setMap(map);
    }

</script>
</body>
</html>

</script>
</body>
</html>

_END;

?>

效果图

涉及setInterval传参的问题。

发现用addMarker(jdata.data[i].gpsx,jdata.data[i].gpsy);时程序可正常运行,但是将该函数 放到setInterval中后却出现了问题,可通过闭包解决。

时间: 2024-08-03 07:19:48

获取json数据后在 地图上打点,根据 json不断移动点的位置的相关文章

sencha touch 通过.axhx获取后台数据时,Unable to parse the JSON returned by the server: Error: You&#39;re trying to

注意:如果你的store跟我一样是使用.ashx从.NET后台获取的,并且用sencha cmd自带的web服务器调试,在chrome调试的时候回会返回如下错误. [WARN][Ext.data.reader.Reader#process] Unable to parse the JSON returned by the server: Error: You're trying to decode an invalid JSON String: <%@ WebHandler Language=&qu

Json数据序列化对象,及对象序列化为Json格式

public class JsonHelper { /// <summary> /// 生成Json格式 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <returns></returns> public static string GetJso

Jquery 使用Ajax获取后台返回的Json数据后,页面处理

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <sc

jquery用ajax方式从后台获取json数据后如何将内容填充到下拉列表

对于问题从后台获取json数据,将内容填充到下拉列表,代码非常简单,具体过程请看下面代码. 需求:url:链接     par:ID       sel:下拉列表选择器 function BuildSelectBox(url, par, sel) {  $(sel).empty();  $.getJSON(url, { id: par }, function (json, textStatus) {   for (var i = json.length - 1; i >= 0; i--) {  

ajax 请求 对json传的处理 Jquery 使用Ajax获取后台返回的Json数据后,页面处理

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <sc

Python爬取房产数据,在地图上展现!

小伙伴,我又来了,这次我们写的是用python爬虫爬取乌鲁木齐的房产数据并展示在地图上,地图工具我用的是 BDP个人版-免费在线数据分析软件,数据可视化软件 ,这个可以导入csv或者excel数据. 首先还是分析思路,爬取网站数据,获取小区名称,地址,价格,经纬度,保存在excel里.再把excel数据上传到BDP网站,生成地图报表 本次我使用的是scrapy框架,可能有点大材小用了,主要是刚学完用这个练练手,再写代码前我还是建议大家先分析网站,分析好数据,再去动手写代码,因为好的分析可以事半功

ajax 发送json数据时为什么需要设置contentType: &quot;application/json”

1. ajax发送json数据时设置contentType: "application/json"和不设置时到底有什么区别? contentType: "application/json",首先明确一点,这也是一种文本类型(和text/json一样),表示json格式的字符串,如果ajax中设置为该类型,则发送的json对象必须要使用JSON.stringify进行序列化成字符串才能和设定的这个类型匹配.同时,对应的后端如果使用了Spring,接收时需要使用@Req

ajax发送json数据时为什么需要设置contentType: &quot;application/json”

1. ajax发送json数据时设置contentType: "application/json”和不设置时到底有什么区别?contentType: "application/json”,首先明确一点,这也是一种文本类型(和text/json一样),表示json格式的字符串,如果ajax中设置为该类型,则发送的json对象必须要使用JSON.stringify进行序列化成字符串才能和设定的这个类型匹配.同时,对应的后端如果使用了Spring,接收时需要使用@RequestBody来注解

iOS:实现MKAnnotation协议,在地图上设置大头针,点击显示具体的位置信息

如何添加大头针(地标): 通过MapView的addAnnotation方法可以添加一个大头针到地图上 通过MapView的addAnnotations方法可以添加多个大头针到地图上 –(void)addAnnotation:(id <MKAnnotation>)annotation; 说明:需要传入一个遵守了MKAnnotation协议的对象 基本步骤为: <1>新建一个遵守MKAnnotation协议的类: @interface MyAnnotation : NSObject