我正在谷歌地图和songkick之间进行混搭,但是我遇到了一个我不明白的错误。该程序通过小程序连接以获取音乐会和地点列表。然后它将该列表作为由 | 分隔的字符串传递。到javascript方法测试。它应该在那里创建一个 LatLng 对象。这是我的html文件代码。
<!DOCTYPE html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyCYstt2SQVTX52XZ-hhvt1Smd9UZTTqDho&sensor=false">
</script>
<script>
function initialize()
{
var mapProp = {center:new google.maps.LatLng(51.508742,-0.120850), zoom:5, mapTypeId:google.maps.MapTypeId.ROADMAP};
var map=new google.maps.Map(document.getElementById("googleMap") ,mapProp);
}
function test(name, lat, lon)
{
var names = name.split("|");
var lats = name.split("|");
var lons = name.split("|");
for(int i = 0; i<names.length; i++)
{
var myLatlng = new google.maps.LatLng(lats[i],lons[i]);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="googleMap" style="width:600px;height:600px;"></div>
<applet code=GreendayConcerts.GreenDayConcerts.class
archive="Applet.jar"
width=120 height=120>
</applet>
</body>
</html>
这是我的java代码
public class GreenDayConcerts extends Applet
{
public void init()
{
try
{
URL url = new URL("http://api.songkick.com/api/3.0/artists/269937/calendar.xml?apikey=lKK1RXr7dIu9fhIw");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(url.openStream());
Element resultsPage = (Element) doc.getElementsByTagName("resultsPage").item(0);
Element results = (Element) resultsPage.getElementsByTagName("results").item(0);
NodeList events = results.getElementsByTagName("event");
StringBuffer names = new StringBuffer();
StringBuffer lats = new StringBuffer();
StringBuffer lons = new StringBuffer();
for(int i = 0; i<events.getLength(); i++)
{
Element event = (Element) events.item(i);
names.append("|"+event.getAttribute("displayName"));
Element location = (Element) event.getElementsByTagName("location").item(0);
lats.append("|"+Float.valueOf(location.getAttribute("lat")));
lons.append("|"+Float.valueOf(location.getAttribute("lng")));
}
String name = names.toString();
String lat = lats.toString();
String lon = lons.toString();
name = name.replaceFirst("[|]", "");
lat = lat.replaceFirst("[|]", "");
lon = lon.replaceFirst("[|]", "");
JSObject.getWindow(this).call("test", new Object[]{name, lat, lon});
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
}
}
}
这一行 var myLatlng = new google.maps.LatLng(lats[i],lons[i]); 似乎是造成问题的原因
我也是 js 的新手,但我在 java 方面很先进。
编辑:我得到的错误是在 JavaScript 对象上没有这样的方法“测试”。但是,当删除 for 循环时,它似乎工作正常。