我有一个javafx谷歌地图应用程序,我可以搜索一个位置。当我运行我的应用程序时,地图会显示但标记没有显示,当我搜索某个位置时,它不会导航并指向该位置。你能帮我解决这个问题吗? 包裹申请;
import java.net.URL;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.SplitPane;
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;
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) {
this.stage = stage;
myBrowser = new MyBrowser();
Scene scene = new Scene(myBrowser);
stage.setScene(scene);
stage.setWidth(800);
stage.setHeight(480);
stage.show();
}
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 city = new TextField("" + "Guntur");
//final TextField latitude = new TextField("" + 17.387140 * 1.00007);
//final TextField longitude = new TextField("" + 78.491684 * 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());
String cityname = city.getText();
System.out.printf("%.2f %.2f%n", lat, lon);
webEngine.executeScript("getCityname(' " + cityname + " ') ");
}
});
SplitPane toolbar = new SplitPane();
HBox hb = new HBox();
hb.setPadding(new Insets(15, 12, 15, 12));
hb.setSpacing(10);
hb.getChildren().addAll(city, update);
toolbar.setOrientation(Orientation.VERTICAL);
toolbar.setDividerPositions(new double[] {.1});
toolbar.getItems().addAll(hb,webView);
getChildren().addAll(toolbar);
}
}
}
<!DOCTYPE html>
<html>
<head>
<title>Google Maps</title>
<script
src="http://maps.googleapis.com/maps/api/js">
</script>
<script type="text/javascript">
var latLng1=new google.maps.LatLng(17.387140,78.491684);
function initialize() {
var carMarkerImage = new google.maps.MarkerImage('resources/images/map_marker.png');
var mapProp = {
center:latLng1,
zoom:7,
//icon:carMarkerImage,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var marker = new google.maps.Marker({
position: new google.maps.LatLng(17.387140,78.491684),
map: map,
draggable: true,
icon: carMarkerImage,
title: "",
autoPan: true
});
var infobulle = new google.maps.InfoWindow({
content: "ddddd"
});
google.maps.event.addListener(marker, 'mouseover', function() {
infobulle.open(map, marker);
});
var map=new google.maps.Map(document.getElementById("mapcanvas"), mapProp);
}
google.maps.event.addDomListener(window, 'load', initialize);
function getCityname(city) {
geocoder = new google.maps.Geocoder();
var address = city;
alert("cityname: " +address);
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
latLng1 = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());
alert("Latitude: "+results[0].geometry.location.lat());
alert("Longitude: "+results[0].geometry.location.lng());
marker.setPosition(latLng1);
map.setCenter(latLng1);
google.maps.event.addDomListener(window, 'load', initialize);
}
else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
<style>
#mapcanvas {
height: 360px;
width: 100%
}
</style>
</head>
<body>
<div id="mapcanvas">
</div>
</body>
</html>``