简体   繁体   English

来自JSON数据的Google Maps Marker无法呈现标记

[英]Google Maps Marker from JSON data not rendering marker

So I'm using a .json file to feed marker information for my google maps script. 所以我正在使用.json文件来为我的Google Maps脚本提供标记信息。 For some reason, it will not render the map marker. 由于某些原因,它不会渲染地图标记。

The map will render and I can place a marker as documented from google but when I try to convert my JSON data to render the marker it simply does nothing. 地图将呈现,我可以放置google记录的标记,但是当我尝试转换JSON数据以呈现标记时,它什么都不做。 No console errors either. 也没有控制台错误。

You'll note the console logs as I've been trying to figure this out and I end up with: 您会注意到控制台日志,因为我一直试图弄清楚这一点,最后得到了:

[0] Event Location: [object Object]

Am I not properly creating the object for this? 我是否为此未正确创建对象?

jQuery(document).ready(function ($) {
    $.getJSON('../wp-content/plugins/loopden/json/map.json', function (eventsData) {
    // Grabbing our JSON file to mark the map.

        Object.size = function(obj) {
            var size = 0, key;
            for (key in obj) {
                if (obj.hasOwnProperty(key)) size++;
            }
            return size;
        };

        // Get the size of an object
        var eventsSize = Object.size(eventsData);
        console.log(eventsSize);
        console.log(eventsData[0].lat);
        for (var i = 0, l=eventsSize; i <l; i++){
            var lat = eventsData[i].lat;
            var lng = eventsData[i].lng;

            var latlng = {"lat":parseInt(lat) , "lng":parseInt(lng)};
            console.log('['+i+'] Event Location: '+latlng);

            var marker = new google.maps.Marker({
                position: latlng,
                map: map,
                title: eventsData[i].title
            });
        }
    });
});

Here is the JSON data I'm working with: 这是我正在使用的JSON数据:

{
  "0": {
    "lat": "-93.2745179",
    "lng": "44.9817854",
    "title": "Super Cool Event",
    "date": "2017-08-25",
    "time": "8:00pm - 10:00am",
    "cost": "$15",
    "bio": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec consectetur, leo ac gravida vestibulum, nisi metus posuere nibh, malesuada faucibus erat eros nec metus. Morbi tincidunt iaculis eros quis fringilla.",
    "featured": "off"
  }
}

Part of the problem is that you're using parseInt to parse a floating point number, so everything after the decimal is truncated. 问题的部分原因是您使用parseInt解析浮点数,因此小数点后的所有内容都会被截断。

The other problem is that the lat-long you're using in the JSON data is invalid - from here : 另一个问题是您在JSON数据中使用的经纬度无效- 从此处开始

The valid range of latitude in degrees is -90 and +90 for the southern and northern hemisphere respectively. 南半球和北半球的有效纬度范围分别是-90和+90。 Longitude is in the range -180 and +180 specifying coordinates west and east of the Prime Meridian, respectively. 经度在-180和+180范围内,分别指定本初子午线以西和以东的坐标。

So, a latitude of -93 doesn't make any sense. 因此,-93的纬度没有任何意义。

I think your problem is related to parseInt . 我认为您的问题与parseInt有关。

Your lat/lng values are being modified by parseInt. 您的经/纬度值已由parseInt修改。 If you have "-93.2745..." as latitude, when you use parseInt only the 93 will be extracted from it. 如果您将纬度设置为“ -93.2745 ...”,则在使用parseInt时,只会从中提取93。

The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems). parseInt()函数解析字符串参数,并返回指定基数(数学数字系统中的基数)的整数。

Try parseFloat instead: 尝试使用parseFloat代替:

for (var i = 0, l=eventsSize; i <l; i++) {
    var lat = data[i].lat;
    var lng = data[i].lng;

    var latlng = {
        lat: parseFloat(lat), 
        lng: parseFloat(lng)
    };

    console.log('['+i+'] Event Location: ', latlng);

    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: data[i].title
    });
}

Also, you are getting this log [0] Event Location: [object Object] because you are trying to "concatenate" the object. 另外,您正在获取此日志[0] Event Location: [object Object]因为您试图“连接”该对象。

If you want to log an object, just use it as an argument in console.log like: 如果要记录对象,只需将其用作console.log的参数,例如:

console.log('['+i+'] Event Location: ', latlng);

Hope this helps. 希望这可以帮助。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/parseInt

I was able to fix this buy using new google.maps.latlng instead of trying to put it together outside of the marker.. 我能够使用新的google.maps.latlng修复此购买,而不必尝试将其放到标记之外。

    jQuery(document).ready(function ($) {
    $.getJSON('../wp-content/plugins/loopden/json/map.json', function (eventsData) {
    // Grabbing our JSON file to mark the map.

        Object.size = function(obj) {
            var size = 0, key;
            for (key in obj) {
                if (obj.hasOwnProperty(key)) size++;
            }
            return size;
        };

        // Get the size of an object
        var eventsSize = Object.size(eventsData);
        console.log(eventsSize);
        console.log(eventsData[0].lat);
        for (var i = 0, l=eventsSize; i <l; i++){
            var lat = eventsData[i].lat;
            var lng = eventsData[i].lng;

            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(parseFloat(lat), parseFloat(lng)),
                map: map,
                title: eventsData[i].title
            });
        }
    });
});

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

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