Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

from what I've read it seems to be possible to run some javascript within a java program, however I'm still struggling on fully grasping how. Would I be able to do enough to execute a googlemap api to be displayed in my java program?

The two examples of code that I have been looking at is this in java:

import javax.script.*;  

public class script {  
public static void main(String[] args) throws Exception {  
    ScriptEngineManager manager = new ScriptEngineManager();  
    ScriptEngine engine = manager.getEngineByName("JavaScript");  

    // JavaScript code in a String  
    String script = "function hello(name) { print('Hello, ' + name); }";  
    // evaluate script  
    engine.eval(script);  

    // javax.script.Invocable is an optional interface.  
    // Check whether your script engine implements or not!  
    // Note that the JavaScript engine implements Invocable interface.  
    Invocable inv = (Invocable) engine;  

    // invoke the global function named "hello"  
    inv.invokeFunction("hello", "Scripting!!" );  
}  
}  

and this as an example found on the google doc site in javascript to produce this:

var center = new google.maps.LatLng(37.4419, -122.1419);
var options = {
'zoom': 13,
'center': center,
'mapTypeId': google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById("map"), options);

var markers = [];
for (var i = 0; i < 100; i++) {
var latLng = new google.maps.LatLng(data.photos[i].latitude,
  data.photos[i].longitude);
var marker = new google.maps.Marker({'position': latLng});
markers.push(marker);
}
var markerCluster = new MarkerClusterer(map, markers);

If any of you can assist me in understanding how to integrate these two samples of code so that the map appears in a JPanel instead of "hello world", I think I could figure the rest of it out.

UPDATE: After reading through the terms of usage, I found out that I would be violating the terms, however; if I move the map onto our organizations public site, I should be able to load the result of that script into my Java programs JPanel which would give public access to the map and not be in violation. Am I correct? Is this possible to do? I don't have any experience with javascript.

share|improve this question
1  
you have to be careful! Google forbids the usage of Maps outside a web-application! you should read the license agreement first! –  desperateCoder Aug 2 '13 at 8:22
    
thanks for that @desperateCoder. I'm looking through the policy now especially segment 9 here: developers.google.com/maps/terms#section_9_1, and I would like your opinion on identifying what my usage would be classified as. My purpose is to use the service to summarize data once every hour within a java application to be used within my organization. Is that considered violating "free access"? –  illWind Aug 2 '13 at 8:30
    
section 9 should not be the problem, i think. even if there were a violation, i think noone would ever notice... sounds, like you would be the only user of your application... but what you should be concerned about is in section 10.1.1. (h) and 10.1.3, or did i misunderstand something... –  desperateCoder Aug 2 '13 at 11:11
    
and yeah, you're welcome ;) –  desperateCoder Aug 2 '13 at 11:14
    
@desperateCoder. I spent a good amount of time going over the terms of usage. I believe I have a solution but would appreciate your opinion. please see the update –  illWind Aug 3 '13 at 1:56
show 1 more comment

1 Answer

I don't know much about integrating Google map script with or the integration policies.

But, you can execute JavaScript file from your java code.So i feel you can write your script code in a js file and execute it as follows :

import javax.script.*;
public class EvalFile {
    public static void main(String[] args) throws Exception {
        // create a script engine manager
        ScriptEngineManager factory = new ScriptEngineManager();
        // create JavaScript engine
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        // evaluate JavaScript code from given file - specified by first argument
        engine.eval(new java.io.FileReader(yourfile.js));
    }
}

For more info please refer this link http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html. I hope it may help you

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.