You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GMapsFX两点间路线无法显示问题求助

GMapsFX路线规划问题:地图无法正确加载

问题详情

在JavaFX学生项目中使用GMapsFX实现两点间路线规划,代码无编译/运行报错,但地图显示**「此页面无法正确加载Google地图」**,控制台输出日志:

INFO com.dlsc.gmapsfx.GoogleMapView - Alert: Hide directions called.

实例化GoogleMapView时未指定Google Maps API密钥,原以为密钥非必需,现附上代码及运行截图请求解决。

代码实现

public class Map implements MapComponentInitializedListener, DirectionsServiceCallback {

    protected GoogleMapView mapView;
    protected GoogleMap map;
    protected Marker marker;
    protected MarkerOptions markerOptions;
    protected DirectionsService directionsService;
    protected DirectionsRenderer directionsRenderer = null;
    protected DirectionsPane directionsPane;

    public Map() {
        mapView = new GoogleMapView();
        mapView.addMapInitializedListener(this);
        markerOptions = new MarkerOptions();
    }

    @Override
    public void mapInitialized() {
        MapOptions mapOptions = new MapOptions();
        mapOptions.mapType(MapTypeIdEnum.ROADMAP).overviewMapControl(false).zoom(18).keyboardShortcuts(false);
        map = mapView.createMap(mapOptions);
        LatLong latLong = new LatLong(50.0650887, 19.9245536);
        map.setCenter(latLong);

        directionsService = new DirectionsService();
        directionsPane = mapView.getDirec();

        Marker marker1 = new Marker(markerOptions);
        Marker marker2 = new Marker(markerOptions);
        marker1.setPosition(new LatLong(50.0750887, 19.9345536));
        marker2.setPosition(new LatLong(50.0850887, 19.9445536));
        map.addMarker(marker1);

        DirectionsRequest directionsRequest = new DirectionsRequest(new LatLong(50.0750887, 19.9345536), new LatLong(50.0850887, 19.9445536), TravelModes.DRIVING, false);
        directionsRenderer = new DirectionsRenderer(true, mapView.getMap(), directionsPane);
        directionsService.getRoute(directionsRequest, this, directionsRenderer);
    }

    @Override
    public void directionsReceived(DirectionsResult results, DirectionStatus status) {
        System.out.println(results);
        System.out.println(status);
    }
}

运行效果截图

程序效果截图1
程序效果截图2


解决方案

核心问题:当前Google Maps JavaScript API强制要求使用API密钥,无密钥会导致地图加载失败,进而引发路线规划组件异常。

  1. 申请并配置API密钥

    • 登录Google Cloud控制台,创建项目后启用「Maps JavaScript API」和「Directions API」(两者均需启用,前者用于加载地图,后者用于路线规划)
    • 创建API密钥,建议限制密钥的使用范围(如绑定本地IP),避免密钥滥用
    • 在代码中传入密钥:
      方式一:实例化GoogleMapView时传入
      public Map() {
          // 替换为你的有效API密钥
          String apiKey = "YOUR_GOOGLE_MAPS_API_KEY";
          mapView = new GoogleMapView(apiKey);
          mapView.addMapInitializedListener(this);
          markerOptions = new MarkerOptions();
      }
      
      方式二:在MapOptions中设置
      MapOptions mapOptions = new MapOptions();
      mapOptions.mapType(MapTypeIdEnum.ROADMAP)
              .overviewMapControl(false)
              .zoom(18)
              .keyboardShortcuts(false)
              .key("YOUR_GOOGLE_MAPS_API_KEY"); // 添加密钥
      
  2. 验证API权限
    确保Google Cloud控制台中,你的API密钥已经启用了「Maps JavaScript API」和「Directions API」,否则即使配置了密钥也会加载失败。

  3. 查看详细错误信息
    GMapsFX基于JavaFX WebView实现,右键地图区域选择「检查」可打开Web控制台,其中会显示更具体的错误原因(如密钥无效、API未启用、配额不足等),帮助排查问题。

注:控制台的Hide directions called提示是地图加载失败后,路线渲染组件初始化异常导致的,解决密钥问题后该提示会消失。

内容的提问来源于stack exchange,提问作者Kacper Bielak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 14:45:34