null不是对象-jQuery+传单

null is not an object - jQuery + leaflet

本文关键字:-jQuery+ 传单 对象 null      更新时间:2023-09-26

我正在尝试使用以下查询构建来获取一些数据:

function buildQuery(startDate, endDate)
{
    /*http://data.cityofnewyork.us/resource/erm2-nwe9.json?$where=(latitude%20IS%20NOT%20NULL)%
    20AND%20(complaint_type%20like%20%27'%Noise'%%27)%20AND%20(created_date%3E=%272013-08-01%27)%
    20AND%20(created_date%3C=%272013-08-08%27)&$group=complaint_type,descriptor,latitude,longitude&$
    select=descriptor,latitude,longitude,complaint_type*/
    var start_date = formattedDate(startDate);  //YYYY-MM-DD
    var end_date = formattedDate(endDate);      //YYYY-MM-DD
    var c_type = 'Noise';                                          // Complaint Type
    // Build the data URL
    URL = "http://data.cityofnewyork.us/resource/erm2-nwe9.json"; // API Access Endpoint
    URL += "?";                                                   // A query parameter name is preceded by the question mark
    URL += "$where=";                                             // Filters to be applied
    URL += "(latitude IS NOT NULL)";                              // Only return records with coordinates
    URL += " AND ";
    URL += "(complaint_type like '''%" + c_type + "''%')";
    URL += " AND ";
    URL += "(created_date>='" + start_date + "') AND (created_date<='" + end_date + "')"; // Date range
    URL += "&$group=complaint_type,descriptor,latitude,longitude";                        // Fields to group by
    URL += "&$select=descriptor,latitude,longitude,complaint_type";                       // Fields to return
    URL = encodeURI(URL);                               // Encode special characters such as spaces and quotes
    URL = URL.replace("'%5C%25", "%27''%");             // Only way that seems to work in Safari
    URL = URL.replace("%5C%25'", "''%%27");
}

然后,我将响应的纬度/经度绘制如下:

function load311ComplaintsIntoMap(map)
{
    cleanMap();
    $.getJSON(URL, function(data)
    {
        if ( data.length == 0 ) 
        {
            return;
        }
        var markers = []
        for (var i = 0; i < noise_description.length; i++) 
        {
            markers[i] = [];
        }
        var all_markers = [];
        $.each(data, function(index, rec)
        {
            var marker;
            for (var i = 0; i < noise_description.length; i++) 
            {
                if (rec.descriptor.indexOf(noise_description[i]) > -1) 
                {
                    marker = L.circleMarker([rec.latitude, rec.longitude], marker_style(i));
                    markers[i].push(marker); 
                    all_markers.push(marker); 
                    break;
                }
                if (i == noise_description.length-1) 
                {
                    marker = L.circleMarker([rec.latitude, rec.longitude], marker_style(i));
                    markers[i].push(marker); 
                    all_markers.push(marker); 
                }
            }
        });
        // Create layer of all markers but do not add to map
        var all_layers = L.featureGroup(all_markers);       
        // Create specific layers of markers and add to map
        for (var i = 0; i < markers.length; i++) 
        {
            layers[i] = L.featureGroup(markers[i]).addTo(map);
            layers[i].bringToFront();
        } // 311complaints.js:152
        map.fitBounds(all_layers.getBounds());
        for (var i = 0; i < noise_description.length; i++) 
        {
            overlays['<i style="background:' + getColor(i) + '"></i> ' +noise_description[i]] = layers[i];
        }
        // Add layer control using above object
        layer = L.control.layers(null,overlays).addTo(map);
    });
}

然而,我收到了这个错误:

TypeError: null is not an object (evaluating 't.lat')
projectleaflet.js:5:17200
latLngToPointleaflet.js:5:17604
projectleaflet.js:5:24291
latLngToLayerPointleaflet.js:5:24556
projectLatlngsleaflet.js:7:15193
redrawleaflet.js:6:28501
setRadiusleaflet.js:7:15524
_updateStyleleaflet.js:7:15290
_initStyleleaflet.js:6:30051
_initElementsleaflet.js:6:29400
onAddleaflet.js:6:27822
_layerAddleaflet.js:5:29679
addLayerleaflet.js:5:21183
eachLayerleaflet.js:6:25647
onAddleaflet.js:6:25458
_layerAddleaflet.js:5:29679
addLayerleaflet.js:5:21183
addToleaflet.js:6:25578
(anonymous function)311complaints.js:152
jjquery-2.1.0.min.js:1:26681
fireWithjquery-2.1.0.min.js:1:27490
xjquery-2.1.0.min.js:3:10523
(anonymous function)jquery-2.1.0.min.js:3:14160

我不知道为什么,因为我请求的字段有一个有效的纬度。

从API返回的某些objects没有latitude。在将其添加到地图之前,请检查他们是否这样做:

rec.hasOwnProperty("latitude") && rec.hasOwnProperty("longitude")