Steps:
- Build a servlet
- Build Ajax and Google map functions
- Make the connection with XML
Technologies:
- Java
- Javascript
- XML
- HTML
Code:
1. Servlet code
public class GpsServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
// GPS coordinates (+ for Noth or East, - for South or West)
out.write("+45.508831, -73.566197");
}
}
2. Javascript code
// -------------------------------------------------------// Ajax code
//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject2() {if (window.XMLHttpRequest) {
return new XMLHttpRequest(); //To support the browsers IE7+, Firefox, Chrome, Opera, Safari
}
else if(window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP"); // For the browsers IE6, IE5
}
else {
alert("Error due to old verion of browser upgrade your browser");
}
}
//Store the browser specific XmlHttpRequest Object in a variable
var rcvReq2 = getXmlHttpRequestObject2();//Gets the current messages from the server
function alterContent2() {if (rcvReq2.readyState == 4 || rcvReq2.readyState == 0) {
// The servlet url is gpsserv as configured in the XML file
rcvReq2.open("GET", 'gpsserv', true);
rcvReq2.onreadystatechange = handleAlterContent2;
rcvReq2.send(null);
}
}
//Function for handling the return of servlet data
function handleAlterContent2() {if (rcvReq2.readyState == 4) {
var responseTextVar = rcvReq2.responseText;
//Split the text into 2 parts: the first one is latitude, the second one the longitude
var item=responseTextVar.split(',');
var lat =item[0];
var lng =item[1];
// -------------------------------------------------------// Ajax code end
// -------------------------------------------------------// Google map code
// Create a variable holding your GPS coordinates
var myLatlng = new google.maps.LatLng(lat, lng);
// Set map options for zoom and position
var mapOptions = zoom: 15, center: myLatlng};
//Create an id for your map
var map = new google.maps.Map(document.getElementById('my_map'), mapOptions);
// Create a text that will be displayed on a infowindow
var contentString =
'<div id="content">'+
'<em id="firstHeading" class="firstHeading" style="color:#FF4000" >Synaps21 |</em>'+
'<b style="color:#FF00BF">|| This is my map ! ' + '</b>'
+
'</div>';
// Create a infowindow
var infowindow = new google.maps.InfoWindow({
content: contentString
});
// Create a marker
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'Synaps21',
icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png'
// You can change the marker color
//icon: 'http://maps.google.com/mapfiles/ms/icons/orange-dot.png'
// icon: 'http://maps.google.com/mapfiles/ms/icons/red-dot.png'
});
// Make the marker open a infowindow on click
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
// --------------------------------------------------// Google map code end
}
}
//Load function
google.maps.event.addDomListener(window, 'load', init);
3. XML code
<servlet>
<servlet-name>gpsserv</servlet-name>
<servlet-class>mypackage.GpsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>gpsserv</servlet-name>
<url-pattern>gpsserv</url-pattern>
</servlet-mapping>
Keywords:
- Google map
- Java EE
- JavaScript
- Ajax
- JQuery
Aucun commentaire:
Enregistrer un commentaire