【问题标题】:How can I read the json data from json file in google map api如何从 google map api 中的 json 文件中读取 json 数据
【发布时间】:2013-04-12 17:15:03
【问题描述】:

我正在尝试制作一个带有标记的谷歌地图 api,它将显示某些地方的纬度和经度。我正在尝试使用 json 文件执行此操作,但它不起作用.. 这是我的代码..

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Cluster Map</title>

    <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer.js" type="text/javascript"></script>

    <script type="text/javascript">
        var map;
        var markers = [];

        function initialize() {
            geocoder = new google.maps.Geocoder();
            var center = new google.maps.LatLng(43.474144,-112.03866);
            map = new google.maps.Map(document.getElementById('map'), {
                zoom: 7,
                center: center,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });
            markMultiple();
        }

        function markMap(latLng, content){
            var marker = new google.maps.Marker({
                position: latLng
            });
            google.maps.event.addListener(marker, 'click', function() {
                infowindow.setContent(content);
                infowindow.open(map, marker);
            });

            markers.push(marker);
        }
        function markMultiple(){
            $.parseJSON('test.json', function(data) {
                $.each(data.markers, function(i, obj) {
                    var latLng =  new google.maps.LatLng(obj.lat,obj.lng);
                    var content = obj.id + ':' + obj.lat + ',' + obj.lng;

                    markMap(latLng, content);
                });
            });


             var markerCluster = new MarkerClusterer(map, markers);
        }



        google.maps.event.addDomListener(window, 'load', initialize);
    </script>
</head>
<body>
    <div id="map-container">
        <div id="map"></div>
    </div>
</body>

请帮帮我..

【问题讨论】:

  • 而不是$.parseJSON,可能你需要$.getJSON方法。
  • 这段代码是否正确我的意思是我也使用了 $getJSON.. 但它没有用。你知道的任何其他方式???
  • 你从浏览器中的 test.json 得到什么输出

标签: javascript jquery json google-maps-api-3


【解决方案1】:

工程师说得对,你需要使用 $.getJSON 方法,$.parseJSON 接受一个 JSON 字符串,它不会加载外部文件。

getJSON: http://api.jquery.com/jQuery.getJSON/ 解析JSON:http://api.jquery.com/jQuery.parseJSON/

我将您的代码放在 jsFiddle 中,它正在使用 getJSON 和替代测试 JSON 文件(您没有提供您正在使用的原始 test.json)。

$.getJSON('test.json', function(data) {
                $.each(data.markers, function(i, obj) {
                    var latLng =  new google.maps.LatLng(obj.lat,obj.lng);
                    var content = obj.id + ':' + obj.lat + ',' + obj.lng;

                    markMap(latLng, content);
                });
            });

http://jsfiddle.net/WYfjv/

如果您的代码无法正常工作,可能是您的 JSON 格式不正确(尝试 JSONLINT 对其进行测试)。另外,如果您已经在改进,我建议您使用普通的 for 循环而不是 $.each :)

【讨论】:

    猜你喜欢
    • 2018-03-05
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多