I’m working on google map api v3 code.
I saved all my markers in MySQL and I want to retrieve those markers and show them on the map. I was looking for a code to do this but I found all the codes were using xml file to save the data that retrieved. I was thinking differently that I can retrieve the data from MySQL and save it in two dimensional array.
I wonder if this is a bad idea and will make my website slower?
these some parts from my code:
(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;
(googlemap.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>
I think that your idea can works well, what I do is save the data on JSON object, after I generate a JSONArray with all the markers info and read that data from a Javascript function for create the markers on the map, what I´d done is working fast with 200 markers.
Some code that could give you some diferent idea:
On javascript parse the JSON
And the display location function: