1

So i am trying to implement google maps in my desktop application written in java. This turned out to be a more difficult task than i imagined, especially with my lack of skills. I have never submitted a question here before but i will try to do it the way i've seen it done when researching.

This is my java class:

package GUI;

import java.net.URL;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class JavaFXGoogleMaps extends Application {

    private Scene scene;
    MyBrowser myBrowser;

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("SHEEPTRACKER3000");
        primaryStage.setWidth(400);
        primaryStage.setHeight(300);
        myBrowser = new MyBrowser();
        scene = new Scene(myBrowser, 400, 300);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);

    }

    static class MyBrowser extends Region {
        HBox toolbar;

        static WebView webView = new WebView();
        static WebEngine webEngine = webView.getEngine();

        public MyBrowser() {

            final URL urlGoogleMaps = getClass().getResource(
                    "GoogleMapsV3.html");
            webEngine.load(urlGoogleMaps.toExternalForm());
            webEngine.setJavaScriptEnabled(true);
            getChildren().add(webView);

            webEngine.setOnAlert(new EventHandler<WebEvent<String>>() {

                @Override
                public void handle(WebEvent<String> arg0) {

                    System.out.println("We are here");
                    webEngine
                            .executeScript("document.getElementById('test').innerHTML = 'the new text';");
                    webEngine.executeScript("document.setNewMarker()");
                    webEngine
                            .executeScript("document.setNewMarkerWithParameters(nAME-63.44-10.39)");
                    // webEngine.load(urlGoogleMaps.toExternalForm());
                    // getChildren().add(webView);

                }

            });

        }
    }
}

And here is my html document:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map_canvas { height: 100% }
    </style>
    <script type="text/javascript"
      src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAGiYaSPuB9Mx2t37gy9yhUR_QOUwdTWE0&sensor=false">
    </script>
    <script type="text/javascript">
      function initialize() {
        var myCenter = new google.maps.LatLng(63.43,10.39)
        var mapOptions = {
          center: myCenter,
          zoom: 13,
          mapTypeId: google.maps.MapTypeId.ROADMAP  
            };

         map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOptions);
        var tilesloaded=true;
        google.maps.event.addListenerOnce(map, 'tilesloaded', function(){
            if(tilesloaded ==true){
             window.alert();
             tilesloaded=false;
            }
        });
      }
     document.setNewMarker = function setNewMarker(){
         var marker=new google.maps.Marker({
              position:new google.maps.LatLng(63.43,10.39),
              icon:'sheep1.png',
              map:map,
              });

     }
     document.setNewMarkerWithParameters = function setNewMarkerWithParameters(info){
         var array = info.split('-');
         var a = array[0], b = array[1], c = array[2];
         var lat = parseFloat(b);
         var longi = parseFloat(c);
         var name = parseString(a);
         var marker=new google.maps.Marker({
              position:new google.maps.LatLng(lat,longi),
              icon:'sheep1.png',
              map:map,
              });

     }
     google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
  <h1 id="test"> Does dis work?</h1>
    <div id="map_canvas" style="width:400px; height:300px"></div>
  </body>
</html>

I have been at this point for a while now, and the project deadline at school is closing in. I get the error message "Can't find variable 'name'" and i don't understand what that means. To put this in context i am trying to make a function in java where i iterate over an array on the form [name-lat-long] and add markers to the map. However i can't get there as i get the error. The name variable is not used yet, as i am trying to make this part of the code working before I go further. But later I want to be able to click on the marker and find the name of the object at that location. Any feedback at all would be much appreciated.

1
  • 1
    Where do you "get the error message"? In your Java code? The Javascript? Commented Nov 3, 2013 at 12:57

2 Answers 2

1

In this line:

webEngine
    .executeScript("document.setNewMarkerWithParameters(nAME-63.44-10.39)");

...you'e telling the engine to execute the document.setNewMarkerWithParameters function, passing in the result of this expression:

nAME
- 63.44
- 10.39

E.g., the variable nAME minus those two floating point numbers. I'm guessing you haven't defined the variable nAME anywhere.

But in your function, it looks like you're expecting that to be a string. If so:

webEngine
    .executeScript("document.setNewMarkerWithParameters('nAME-63.44-10.39')");
// Note the quotes -------------------------------------^----------------^
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! That solved the problem for me :) It's the smallest things that get you :P
0

Also you have to replace

var name = parseString(a);

with

var name = a;

split returns a string anyway and as far as i know parseString doesn't exsits in java script (if you need use toString())

I acutally was able to run the code like that (using T.J. Crowder presented solution and removing the parseString)

1 Comment

Thank you for pointing it out, i was in fact aware,i had just forgotten to remove it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.