【问题标题】:GWT Google Maps: setCenter: not a LatLng or LatLngLiteral: in property lat: not a numberGWT Google 地图:setCenter:不是 LatLng 或 LatLngLiteral:在属性 lat:不是数字
【发布时间】:2014-10-09 16:30:51
【问题描述】:

我正在使用 GWT(2.61 版)和 gwt-maps(3.8.0 版)通过 GWT 显示 Google 地图。

基础功能似乎可行,但现在我正在尝试运行 gwt-maps zip 附带的 CitySimple 示例。 Here 它是直接使用 JavaScript,但我正在尝试通过 GWT 来实现。

但是,当我运行示例时,我在启动时收到以下错误:

com.google.gwt.core.client.JavaScriptException: (InvalidValueError) @com.google.maps.gwt.client.Circle::create(Lcom/google/maps/gwt/client/CircleOptions;)([JavaScript object(17)]): setCenter: not a LatLng or LatLngLiteral: in property lat: not a number
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:249)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:576)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:284)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.maps.gwt.client.Circle$.create(Circle.java)
    at com.test.client.GwtTest.renderMap(GwtTest.java:80)

地图出现了,但没有一个城市圈出现。似乎认为我的 LatLng 类型中的 lat 值不是数字,这在 GWT 中是不可能的,因为 Java 只会采用双精度值。

这是我正在运行的 GWT 代码:

package com.test.client;

import java.util.HashMap;

import com.google.gwt.ajaxloader.client.AjaxLoader;
import com.google.gwt.ajaxloader.client.AjaxLoader.AjaxLoaderOptions;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Document;
import com.google.maps.gwt.client.Circle;
import com.google.maps.gwt.client.CircleOptions;
import com.google.maps.gwt.client.GoogleMap;
import com.google.maps.gwt.client.LatLng;
import com.google.maps.gwt.client.MapOptions;
import com.google.maps.gwt.client.MapTypeId;

public class GwtTest implements EntryPoint {

    static class City {
        LatLng center;
        Long population;

        City(LatLng center, Long population) {
            this.center = center;
            this.population = population;
        }
    }

    private static java.util.Map<String, City> citymap = new HashMap<String, City>();
    static {
        citymap.put("Chicago", new City(LatLng.create(41.878113, -87.629798), 2842518L));
        citymap.put("New York", new City(LatLng.create(40.714352, -74.005973), 8143197L));
        citymap.put("Los Angeles", new City(LatLng.create(34.052234, -118.243684), 3844829L));
    }

    @Override
    public void onModuleLoad() {
        AjaxLoaderOptions options = AjaxLoaderOptions.newInstance();
        options.setOtherParms("sensor=false");
        Runnable callback = new Runnable() {
            public void run() {
                renderMap();
            }
        };
        AjaxLoader.loadApi("maps", "3", callback, options);
    }

    public static void renderMap() {
        MapOptions mapOpts = MapOptions.create();
        mapOpts.setZoom(4);
        mapOpts.setCenter(LatLng.create(37.09024, -95.712891));
        mapOpts.setMapTypeId(MapTypeId.TERRAIN);
        GoogleMap map = GoogleMap.create(Document.get().getElementById("map_canvas"), mapOpts);

        for (String cityName : citymap.keySet()) {

            System.out.println("City: " + cityName);

            City city = citymap.get(cityName);

            // Construct the circle for each value in citymap. Scale population by 20.
            CircleOptions populationOptions = CircleOptions.create();
            populationOptions.setStrokeColor("#ff0000");
            populationOptions.setStrokeOpacity(0.8);
            populationOptions.setStrokeWeight(2);
            populationOptions.setFillColor("#ff0000");
            populationOptions.setFillOpacity(0.35);
            populationOptions.setMap(map);

            System.out.println("Lat: " + city.center.lat());
            System.out.println("Lng: " + city.center.lng());

            populationOptions.setCenter(city.center);

            populationOptions.setRadius(city.population / 20);
            Circle.create(populationOptions);
        }
    }
}

如您所见,所有的 LatLng 值都有其 lats 和 lngs 的数字,所以这个错误对我来说没有任何意义。

为了完整起见,这是我的 html 文件:

<!doctype html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">                                               
    <link type="text/css" rel="stylesheet" href="GwtTest.css">                               
    <title>GWT Test</title>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript" language="javascript"
        src="gwttest/gwttest.nocache.js"></script>
</head>
<body>
    <p>Testing</p>
    <div style="width:500px; height:500px">
        <div id="map_canvas" style="width: 100%; height: 100%;">Map is loading...</div>
    </div>
</body>
</html>

这是我的 GWT 模块 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  When updating your version of GWT, you should also update this DTD reference,
  so that your app can take advantage of the latest GWT module capabilities.
-->
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.6.1//EN"
  "file:///C:/Users/kworkman/Desktop/gwt-2.6.1/gwt-module.dtd">
<module rename-to='gwttest'>
  <!-- Inherit the core Web Toolkit stuff.                        -->
  <inherits name='com.google.gwt.user.User'/>
  <inherits name="com.google.maps.gwt.GoogleMaps" />
  <inherits name="com.google.gwt.ajaxloader.AjaxLoader" />

  <!-- Inherit the default GWT style sheet.  You can change       -->
  <!-- the theme of your GWT application by uncommenting          -->
  <!-- any one of the following lines.                            -->
  <inherits name='com.google.gwt.user.theme.clean.Clean'/>
  <!-- <inherits name='com.google.gwt.user.theme.standard.Standard'/> -->
  <!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
  <!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/>     -->

  <!-- Other module inherits                                      -->

  <!-- Specify the app entry point class.                         -->
  <entry-point class='com.test.client.GwtTest'/>

  <!-- Specify the paths for translatable code                    -->
  <source path='client'/>
  <source path='shared'/>

  <!-- allow Super Dev Mode -->
  <add-linker name="xsiframe"/>
</module>

谁能看到我做错了什么?我还是 GWT 的新手,所以如果我做了一些愚蠢的事情,我不会感到震惊,但是这个错误似乎直接与代码相矛盾,所以我有点茫然。

我已经用谷歌搜索了这个错误,但我看到的每个问题都是由人们将字符串而不是数字传递给 LatLng 类型引起的,我没有这样做。

【问题讨论】:

  • 您下载了两次地图 API:一次在您的 html 文件 (
  • @philfr49 感谢您指出这一点。我仍然不知道我在这里做什么,所以每个小指针都有帮助!

标签: java javascript google-maps gwt google-maps-api-3


【解决方案1】:

我认为您使用 LatLng 类型的代码是在加载 api 之前执行的。 一旦 api 完全加载,尝试初始化你的 citymap 对象:

Runnable callback = new Runnable() {
    public void run() {
       citymap.put("Chicago", new City(LatLng.create(41.878113, -87.629798), 2842518L));
       citymap.put("New York", new City(LatLng.create(40.714352, -74.005973), 8143197L));
       citymap.put("Los Angeles", new City(LatLng.create(34.052234, -118.243684), 3844829L));

       renderMap();
    }
};

这是因为 LatLng 对象是 JavaScriptObject 类型,并且会使用 api 中的 javascript 函数。

另请参阅我关于您两次加载 api 的评论。

【讨论】:

  • 这正是问题所在。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2017-12-06
  • 1970-01-01
  • 2016-01-03
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
  • 1970-01-01
  • 2018-04-06
相关资源
最近更新 更多