Reputation: 10893
Given a list of 'addresses' (String) I'm able to get the geocoding (latitude and longitude) in my Java code (Coordination Class).
Armed with the geocoding results my objective is to put the coordinates in an HTML page. Using bing maps, I'm writing something like this:
function GetMap()
{
// Initialize the map
map = new Microsoft.Maps.Map(document.getElementById("myMap"), {credentials:"MY_KEY_COMES_HERE"});
// Retrieve the location of the map center
var center = map.getCenter();
var pins = new Microsoft.Maps.EntityCollection();
//[PASTE] this code is outputed by my java business code and later copy-paste here
var position = new Microsoft.Maps.Location(21.1833927,-71.147288);
var pin = new Microsoft.Maps.Pushpin(position);
pins.push(pin)
var position = new Microsoft.Maps.Location(21.1822191,-71.182732);
var pin = new Microsoft.Maps.Pushpin(position);
pins.push(pin)
var position = new Microsoft.Maps.Location(22.1188265,-73.1515302);
var pin = new Microsoft.Maps.Pushpin(position);
pins.push(pin)
var position = new Microsoft.Maps.Location(22.1901809,-72.9447259);
var pin = new Microsoft.Maps.Pushpin(position);
pins.push(pin)
var position = new Microsoft.Maps.Location(21.1194505,-71.92582259999999);
var pin = new Microsoft.Maps.Pushpin(position);
pins.push(pin)
//..rest of code comes here
}
The code area inside the javascript is done manually; that is, I'm system.out the code under the word [PASTE]. At this moment, I'm doing it like this just to see that my coordinates are correct (I know, inefficient and dummy).
So that brings me to my question: how can write directly to the HTML page (or javascript) without the manual work described above. Or, how do I combine my java business code with javascript?
Upvotes: 0
Views: 516
Reputation: 17874
If the java business code can run in the browser and access all resources it needs from the client browser, you can deploy your java code as an applet: Javascript to Java Applet communication
Your use case looks more like you want it to run in an application server on a backend server, and let your browser communicate with it via http calls. From JavaScript, this is best done using JavaEE6's JAX-RS feature, for which Jersey is the reference implementation: Access Jersey based RESTful service using jQuery
In your application server you have:
class Result{
public List<Pair<Long,Long>> getList();
...
}
@Path("/coordinates")
public class CoordinatesResource {
@GET
@Produces
public Result getCoordinates(String location){
return ...;
}
}
In the html page, you have something like:
$.getJSON("http://my.server.com/coordinates/?location=" + location,
function(json) {
for(var i = 0; i < json.list.length; i++){
position = new Microsoft.Maps.Location(json.list[i].left,json.list[i].right);
var pin = new Microsoft.Maps.Pushpin(position);
pins.push(pin)
}
}
Upvotes: 1
Reputation: 8815
The simplest solution is to use move the for loop from javascript to java (in a jsp page) which spits out the evaluated locations. This example assumes you have an array myLocations
with all the location data pre-filled.
<% for (int i=0; i < myLocations.length; i++) { %>
///...get the coordinates (l1,l2)
var position = new Microsoft.Maps.Location(<%= myLocations[i].l1 %>, <%= myLocations[i].l2 %>);
// Creates a Pushpin
var pin = new Microsoft.Maps.Pushpin(position);
pins.push(pin);
<% } %>
The disadvantage to this approach is that because the generated javascript is constantly changing, you cannot cache it in the client browser leading to inefficiency. To avoid it, you could create an AJAX request to a jsp page which provides the current required locations to add.
Upvotes: 1