Google Maps Java
Total Page:16
File Type:pdf, Size:1020Kb
Google Maps Java 1 Contents 1 Lizenzprodukte 3 1.1 JXBrowser ........................................ 3 1.2 JXMaps.......................................... 4 1.3 weitereProdukte ................................... .. 5 2 lizenzfrei 5 2.1 klassischeWebengine................................. .. 5 2.2 JSONObjekt....................................... 6 2.3 webEngine.executeScript(); . ..... 7 2.4 DasgetClass().getResource()-Problem . ........ 7 2.5 Favorit........................................... 7 3 legal - illegal 7 4 Lizenzen und Preise 8 5 Ausweg aus dem Dilemma 8 6 Terms of Service 8 2 1 Lizenzprodukte 1.1 JXBrowser import com.teamdev.jxbrowser.chromium.Browser; import com.teamdev.jxbrowser.chromium.swing.BrowserView; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class jxBrowser { public static final int MIN_ZOOM = 0; public static final int MAX_ZOOM = 21; /*** In map.html file default zoom value is set to 4. */ private static int zoomValue = 4; public static void main(String[] args) { final Browser browser = new Browser(); BrowserView browserView = new BrowserView(browser); JButton zoomInButton = new JButton("Zoom In"); zoomInButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (zoomValue < MAX_ZOOM) { browser.executeJavaScript("map.setZoom(" + ++zoomValue + ")"); } } }); JButton zoomOutButton = new JButton("Zoom Out"); zoomOutButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (zoomValue > MIN_ZOOM) { browser.executeJavaScript("map.setZoom(" + --zoomValue + ")"); } } }); JButton setMarkerButton = new JButton("Set Marker"); setMarkerButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { browser.executeJavaScript("var myLatlng = new google.maps.LatLng(48.4431727,23.0488126);\n" + "var marker = new google.maps.Marker({\n" + " position: myLatlng,\n" + " map: map,\n" + " title: ’Hello World!’\n" + "});"); } }); JPanel toolBar = new JPanel(); toolBar.add(zoomInButton); toolBar.add(zoomOutButton); toolBar.add(setMarkerButton); JFrame frame = new JFrame("map.html"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.add(toolBar, BorderLayout.SOUTH); frame.add(browserView, BorderLayout.CENTER); frame.setSize(900, 500); frame.setLocationRelativeTo(null); frame.setVisible(true); browser.loadURL("C://map.html"); } } https://jxbrowser.support.teamdev.com/support/solutions/articles/9000012874-google-maps 3 1.2 JXMaps /* * Copyright (c) 2000-2016 TeamDev Ltd. All rights reserved. * Use is subject to Apache 2.0 license terms. */ package com.teamdev.jxmaps.examples; import com.teamdev.jxmaps.Map; import com.teamdev.jxmaps.MapOptions; import com.teamdev.jxmaps.MapTypeControlOptions; import com.teamdev.jxmaps.MapReadyHandler; import com.teamdev.jxmaps.MapStatus; import com.teamdev.jxmaps.ControlPosition; import com.teamdev.jxmaps.LatLng; import com.teamdev.jxmaps.swing.MapView; import javax.swing.*; import java.awt.*; /** * This example demonstrates how to create a MapView instance, * display it in JFrame and open a simple map. * * @author Vitaly Eremenko */ public class MapExample extends MapView { public MapExample() { // Setting of a ready handler to MapView object. onMapReady will be called when map initialization is done and // the map object is ready to use. Current implementation of onMapReady customizes the map object. setOnMapReadyHandler(new MapReadyHandler() { @Override public void onMapReady(MapStatus status) { // Check if the map is loaded correctly if (status == MapStatus.MAP_STATUS_OK) { // Getting the associated map object final Map map = getMap(); // Creating a map options object MapOptions options = new MapOptions(map); // Creating a map type control options object MapTypeControlOptions controlOptions = new MapTypeControlOptions(map); // Changing position of the map type control controlOptions.setPosition(ControlPosition.TOP_RIGHT); // Setting map type control options options.setMapTypeControlOptions(controlOptions); // Setting map options map.setOptions(options); // Setting the map center map.setCenter(new LatLng(map, 35.91466, 10.312499)); // Setting initial zoom value map.setZoom(2.0); } } }); } public static void main(String[] args) { MapExample sample = new MapExample(); JFrame frame = new JFrame("Map Integration"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.add(sample, BorderLayout.CENTER); frame.setSize(700, 500); 4 frame.setLocationRelativeTo(null); frame.setVisible(true); } } https://www.teamdev.com/jxmaps#features 1.3 weitere Produkte • JXMapViewer 16 Beispiele http://www.programcreek.com/java-api-examples/index.php?api=org.jdesktop.swingx. JXMapViewer • JXMapKit • etc. Beispiel f¨ur die Zusammenarbeit zwischen JXMapViewer und JXMapKit GoogleMaps Script I Beispiel 3: JXMapViewer() 2 lizenzfrei Hinweis Beispiele dienen nur der Verdeutlichung unterschiedlicher Prinzipien, d.h. es handelt sich um Quellen verschiedener Herkunft, die so nicht in das Projekt einzubetten sind. 2.1 klassische Webengine import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.concurrent.Worker; import javafx.concurrent.Worker.State; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ScrollPane; import javafx.scene.layout.VBox; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; public class app extends Application { WebView browser; WebEngine webEngine; @Override public void start(final Stage stage) { stage.setTitle("HTML"); stage.setWidth(500); stage.setHeight(500); Scene scene = new Scene(new Group()); VBox root = new VBox(); browser = new WebView(); webEngine = browser.getEngine(); webEngine.loadContent("<div =\"div0\">Text</div>"); ScrollPane scrollPane = new ScrollPane(); scrollPane.setContent(browser); webEngine.getLoadWorker().stateProperty().addListener( new ChangeListener<State>() { 5 @Override public void changed(ObservableValue ov, State oldState, State newState) { if (newState == Worker.State.SUCCEEDED) { stage.setTitle(webEngine.getLocation()); } } }); root.getChildren().addAll(scrollPane); scene.setRoot(root); stage.setScene(scene); stage.show(); } public static void main(String[] args) {launch(args);} 2.2 JSON Objekt \\ java import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.concurrent.Worker; import javafx.concurrent.Worker.State; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ScrollPane; import javafx.scene.layout.VBox; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; import netscape.javascript.JSObject; public class app extends Application { WebView browser; WebEngine webEngine; @Override public void start(final Stage stage) { stage.setTitle("HTML"); stage.setWidth(500); stage.setHeight(500); Scene scene = new Scene(new Group()); VBox root = new VBox(); browser = new WebView(); webEngine = browser.getEngine(); webEngine.loadContent("<div =\"div0\"><a href=\"#\" onClick=\"javaObj.test()\">klick</a></div>"); ScrollPane scrollPane = new ScrollPane(); scrollPane.setContent(browser); webEngine.getLoadWorker().stateProperty().addListener( new ChangeListener<State>() { @Override public void changed(ObservableValue ov, State oldState, State newState) { if (newState == Worker.State.SUCCEEDED) { stage.setTitle(webEngine.getLocation()); JSObject win = (JSObject) webEngine.executeScript("window"); win.setMember("javaObj", new JavaApp()); } } }); root.getChildren().addAll(scrollPane); scene.setRoot(root); stage.setScene(scene); stage.show(); } 6 final class JavaApp {public void test() {}} public static void main(String[] args) {launch(args);} } 2.3 webEngine.executeScript(); 2.4 Das getClass().getResource()-Problem package javafxgooglemaps; import java.net.URL; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.HBox; import javafx.scene.layout.Region; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; /** * * @web http://java-buddy.blogspot.com/ */ public class googleMaps extends Application { private Scene scene; MyBrowser myBrowser; @Override public void start(Stage primaryStage) { primaryStage.setTitle("HTML"); 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);} class MyBrowser extends Region { HBox toolbar; WebView webView = new WebView(); WebEngine webEngine = webView.getEngine(); public MyBrowser(){ final URL urlGoogleMaps = getClass().getResource("GoogleMapsV3.html"); webEngine.load(urlGoogleMaps.toExternalForm()); webEngine.setJavaScriptEnabled(true); getChildren().add(webView); } } } 2.5 Favorit webEngine.executeScript(); 3 legal - illegal ”... technically it is possible (using JXMapViewer), legaly it is not. Google does not allow using Google Maps outside of the browser.” http://stackoverflow.com/questions/1303333/googleearth-inside-java-swing Das erkl¨art die Tatsache, das alle g¨angigen Versionen aus 3 Komponenten bestehen. 7 Es ist also illegal, rein Java zu verwenden. So dauert es ein paar Monate bis Jahre und lizenzrechtliche Probleme mit immensen Kosten treffen jeden beteiligten des Projektes.