0

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?

adhg
  • 10,437
  • 12
  • 58
  • 94
  • It's not clear what you're talking about in the last paragraph. Can you show more of the code? I'm not sure I understand how you can run a program by repeatedly copy-pasting code? Maybe I really just misunderstand the problem, in which case, further clarification would help. Good luck! :) – jamesmortensen Nov 11 '12 at 19:45
  • What's the Coordination class? What resources does it use? – GeertPt Nov 12 '12 at 22:01
  • 1
    @jmort253 you're right, I just edited my code to explain how I do it. Thanks for the pointer. – adhg Nov 13 '12 at 01:09
  • @greyfairer the resources comes from a different application (given the addresses - get the coordinations as in lat/lng) – adhg Nov 13 '12 at 01:10
  • I guess you can't directly access that application from within the browser? You can run the java in an application server and use it via AJAX/jQuery to access it. – GeertPt Nov 13 '12 at 14:56

2 Answers2

1

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.

just.another.programmer
  • 8,579
  • 8
  • 51
  • 90
1

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)
    }
}
Community
  • 1
  • 1
GeertPt
  • 16,398
  • 2
  • 37
  • 61
  • The 2nd option is the one to go with. It would be much more helpful if you'd give an example like [just.another.programmer](http://stackoverflow.com/a/13334752/552792) did. The 1st option is a bit dated. Applets are dead. – jamesmortensen Nov 11 '12 at 21:24
  • @jmort253 there's a huge difference in operation cost between a static html with an applet in it, and an application server. And about 'applets are dead', google 'JavaFX'? – GeertPt Nov 12 '12 at 22:07
  • I guess you learn something new everyday as the latest release of that was just last month. Still, I wonder why I've not come across many major websites utilizing this technology... – jamesmortensen Nov 13 '12 at 01:22