Skip to content

Instantly share code, notes, and snippets.

@chaschev
Created November 15, 2013 08:58
Show Gist options
  • Save chaschev/7481304 to your computer and use it in GitHub Desktop.
Save chaschev/7481304 to your computer and use it in GitHub Desktop.
JavaFX Google Maps demo
<!DOCTYPE html>
<html>
<head>
<title>Java-Buddy: Google Maps</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<style>#mapcanvas { height: 360px; width: 100%}</style>
</head>
<body>
<h1>Java-Buddy: Google Maps</h1>
<div id="mapcanvas">
<script type="text/javascript">
document.map = new google.maps.Map(document.getElementById("mapcanvas"));
var latlng = new google.maps.LatLng(35.857908, 10.598997);
var Options = {
zoom: 15,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("mapcanvas"), Options);
//var carMarkerImage = new google.maps.MarkerImage('resources/images/car.png');
var marker = new google.maps.Marker({
position: new google.maps.LatLng(35.857908, 10.598997),
map: map,
draggable: false,
//icon: carMarkerImage,
title: "",
autoPan: true
});
var infobulle = new google.maps.InfoWindow({
content: "ddddd"
});
google.maps.event.addListener(marker, 'mouseover', function() {
infobulle.open(map, marker);
});
document.goToLocation = function(x, y) {
alert("goToLocation, x: " + x +", y:" + y);
var latLng = new google.maps.LatLng(x, y);
marker.setPosition(latLng);
map.setCenter(latLng);
}
</script>
</div>
</body>
</html>
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import java.net.URL;
public class GoogleApp extends Application {
private Scene scene;
MyBrowser myBrowser;
double lat;
double lon;
private Stage stage;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
this.stage = stage;
myBrowser = new MyBrowser();
Scene scene = new Scene(myBrowser);
stage.setScene(scene);
stage.setWidth(1200);
stage.setHeight(600);
stage.show();
}
//stuff and stuff
class MyBrowser extends Pane {
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
public MyBrowser() {
final URL urlGoogleMaps = getClass().getResource("demo.html");
webEngine.load(urlGoogleMaps.toExternalForm());
webEngine.setOnAlert(new EventHandler<WebEvent<String>>() {
@Override
public void handle(WebEvent<String> e) {
System.out.println(e.toString());
}
});
getChildren().add(webView);
final TextField latitude = new TextField("" + 35.857908 * 1.00007);
final TextField longitude = new TextField("" + 10.598997 * 1.00007);
Button update = new Button("Update");
update.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
lat = Double.parseDouble(latitude.getText());
lon = Double.parseDouble(longitude.getText());
System.out.printf("%.2f %.2f%n", lat, lon);
webEngine.executeScript("" +
"window.lat = " + lat + ";" +
"window.lon = " + lon + ";" +
"document.goToLocation(window.lat, window.lon);"
);
}
});
HBox toolbar = new HBox();
toolbar.getChildren().addAll(latitude, longitude, update);
getChildren().addAll(toolbar);
}
}
}
@ckcchiu
Copy link

ckcchiu commented Nov 21, 2018

I found it can't show the map on macOS High Sierra Version 10.13.4, if I change to show "http://www.google.com", it works. May I know how to fix it? What Java SDK should I use and what Java SE Run Time should I use?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment