您的代码中有两个问题。您的json文件
[在开头和
]结尾都缺少。您的Javascript也是错误的,您想对的回调中的json进行操作
getJSON。您的问题的代码是:
$.getJSON("foo.txt", function(json1) { $.each(json1, function(key, data) { var latLng = new google.maps.LatLng(data.lat, data.lng); // Creating a marker and putting it on the map var marker = new google.maps.Marker({ position: latLng, map: map, title: data.title }); });});编辑:
这是一个基于google
maps教程的工作示例。您需要正确的文件
foo.txt:
<!DOCTYPE html><html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <style type="text/css"> html { height: 100% } body { height: 100%; margin: 0; padding: 0 } #map_canvas { height: 100% } </style> <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=true"> </script> <script type="text/javascript"> var map; function initialize() { var mapOptions = { center: new google.maps.LatLng(58, 16), zoom: 7, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); } </script> <script type="text/javascript" src="http://pre.jquery.com/jquery-latest.min.js"></script> </head> <body onload="initialize()"> <div id="map_canvas" ></div> <script type="text/javascript"> $(document).ready(function() { $.getJSON("foo.txt", function(json1) { $.each(json1, function(key, data) { var latLng = new google.maps.LatLng(data.lat, data.lng); // Creating a marker and putting it on the map var marker = new google.maps.Marker({ position: latLng, title: data.title }); marker.setMap(map); }); }); }); </script> </body></html>


