Appeler une fonction Javascript dans une page jsp

dev_88 Messages postés 1 Date d'inscription mercredi 23 octobre 2013 Statut Membre Dernière intervention 29 octobre 2013 - 29 oct. 2013 à 11:25
KX Messages postés 16727 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 2 décembre 2023 - 29 oct. 2013 à 18:30
salut a vous. je ne sais pas comment appeler la fonction addMarker() dans le script-let jsp. j'aimerai ajouter des markers sur Google Map a chaque couple latitude-longitude retourne. aidez moi appeler cette fonction dans le script jsp.

voici le code source:
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<title>Google Map API V3 with markers</title>

<style type="text/css">
body {
font: normal 10pt Helvetica, Arial;
}

#map {
width: 100%;
height:100%;
border: 100%;
padding: 100%;
}
</style>

<script src="http://maps.google.com/maps/api/js?v=3&sensor=false"
type="text/javascript"></script>

<script type="text/javascript">
//Sample code written by August Li

var icon = new google.maps.MarkerImage(
"http://maps.google.com/mapfiles/ms/micons/blue.png",

new google.maps.Size(32, 32), new google.maps.Point(0, 0),

new google.maps.Point(16, 32));

var center = null;

var map = null;

var currentPopup;

var bounds = new google.maps.LatLngBounds();

function addMarker(lat, lng, info) {

var pt = new google.maps.LatLng(lat, lng);

bounds.extend(pt);

var marker = new google.maps.Marker({position : pt,icon : icon,map : map});

var popup = new google.maps.InfoWindow({content : info,maxWidth : 300});

google.maps.event.addListener(marker, "click", function() {

if (currentPopup != null) {

currentPopup.close();

currentPopup = null;

}

popup.open(map, marker);

currentPopup = popup;

});

google.maps.event.addListener(popup, "closeclick", function() {

map.panTo(center);

currentPopup = null;

});

}

function initMap() {

map = new google.maps.Map(document.getElementById("map"), {

center : new google.maps.LatLng(2.93069262, 11.41507819),

zoom : 14,

mapTypeId : google.maps.MapTypeId.ROADMAP,

mapTypeControl : false,

mapTypeControlOptions : {

style : google.maps.MapTypeControlStyle.HORIZONTAL_BAR

},

navigationControl : true,

navigationControlOptions : {

style : google.maps.NavigationControlStyle.SMALL

}

});




}


<%
try {
PreparedStatement prepare=null;
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:54321/moptgis";
String user = "joeky";
String passwd = "joeky";
Connection conn = DriverManager.getConnection(url, user, passwd);
System.out.println("Connexion effective !");
prepare = conn.prepareStatement("select * from geolocalisation");
ResultSet resultSet = prepare.executeQuery();
while(resultSet.next()){
String latitude=(resultSet.getString(resultSet.findColumn("latitude")));
String longitude=resultSet.getString(resultSet.findColumn("longitude"));
String site_name=resultSet.getString("site_name");
String province=resultSet.getString(resultSet.findColumn("province"));
out.print(addMarker(latitude, longitude, site_name));

}

} catch (Exception e) {
e.printStackTrace();
} finally{

}

%>
center = bounds.getCenter();

map.fitBounds(bounds);


</script>

</head>

<body onload="initMap()" style="margin: 0px; border: 0px; padding: 0px;">

<div id="map"></div>

</html>

1 réponse

KX Messages postés 16727 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 2 décembre 2023 126
29 oct. 2013 à 18:30
Tu ne devrais pas mettre des gros morceaux de code Java, JavaScript, ou même CSS au sein de ta Jsp. La Jsp ne concerne que la partie présentation de ta page web. La grosse partie accès des données doit être dans une classe Java à part (d'autant que les scriptlets sont à proscrire depuis l'apparition des jstl). Et les parties JavaScript et CSS seront quant à eux placés en ressources des pages dans des fichiers .js et .css comme pour une application web normale...
-1
Rejoignez-nous