我正在研究谷歌地图 api v3 代码。
我将所有标记保存在 MySQL 中,我想检索这些标记并将它们显示在地图上。我正在寻找执行此操作的代码,但我发现所有代码都使用 xml 文件来保存检索到的数据。我的想法不同,我可以从 MySQL 中检索数据并将其保存在二维数组中。
我想知道这是否是一个坏主意并且会使我的网站变慢?
我的代码中的这些部分:
(markersDAO.java)
String searchQuery = "select * from map";
try
{
//connect to DB
currentCon = ConnectionManager.getConnection();
stmt=currentCon.createStatement();
rs = stmt.executeQuery(searchQuery);
int i=0;
// if user does not exist set userExits to false
while (rs.next()){
m[i][0]=rs.getString(1);
m[i][1]=Double.toString(rs.getDouble(2));
m[i][2]=Double.toString(rs.getDouble(3));
i++;
}
}
catch (Exception ex)
{
System.out.println("Datamap failed 2: An Exception has occurred! " + ex);
}
return m;
(谷歌地图.jsp)
<% markersDAO u= new markersDAO();
String[][] locations= u.dataMap();%>
<script type="text/javascript">
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker;
var f=0;
var myOptions= {
disableAutoPan: false
,maxWidth: "auto"
,pixelOffset: new google.maps.Size(0, 0)
,zIndex: null
,infoBoxClearance: new google.maps.Size(1, 1)
,isHidden: false
,pane: "floatPane"
,enableEventPropagation: false
};
<%for (int i = 0; i < locations.length; i++) { %>
marker = new google.maps.Marker({
position: new google.maps.LatLng(<%=locations[i][1]%>, <%=locations[i][2]%>),
map: map,
});
var infobox = new InfoBox(myOptions);
google.maps.event.addListener(marker, 'mouseover', (function(marker,f) {
return function() {
infobox.setContent('<%=locations[i][0]%>');
infobox.open(map, marker);
}
f++;
})(marker, f));
<% } %>
</script>