GWT GoogleMaps使用样式隐藏默认图层
我正在使用GWT和GWT GoogleMaps API(v3.8.0)。 我拥有一切并且运行完美。
不过,我想要禁用GoogleMaps附带的一些默认功能,例如街道名称,点击餐馆的功能等。基本上,我希望有一个非常准确的地图图层,可以添加自己的自定义图层至。
我以为我可以用Styles做到这一点。 我试图使用一个MapTypeStyler,关闭我想禁用的任何类型的MapTypeStyle(在这个测试用例中,MapTypeStyle.ROAD)。
以下是我试图运行的测试代码:
package com.test.client;
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.core.client.JsArray;
import com.google.gwt.dom.client.Document;
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;
import com.google.maps.gwt.client.MapTypeStyle;
import com.google.maps.gwt.client.MapTypeStyleElementType;
import com.google.maps.gwt.client.MapTypeStyleFeatureType;
import com.google.maps.gwt.client.MapTypeStyler;
public class GwtTest implements EntryPoint {
@Override
public void onModuleLoad() {
AjaxLoaderOptions options = AjaxLoaderOptions.newInstance();
options.setOtherParms("sensor=false");
Runnable callback = new Runnable() {
public void run() {
createMap();
}
};
AjaxLoader.loadApi("maps", "3", callback, options);
}
public void createMap() {
JsArray<MapTypeStyle> styles = (JsArray<MapTypeStyle>) JsArray.<MapTypeStyle>createArray();
JsArray<MapTypeStyler> roadStylers = (JsArray<MapTypeStyler>) JsArray.<MapTypeStyler>createArray();
MapTypeStyler roadStyler = MapTypeStyler.visibility("off");
roadStylers.push(roadStyler);
MapTypeStyle roadStyle = MapTypeStyle.create();
roadStyle.setStylers(roadStylers);
roadStyle.setFeatureType(MapTypeStyleFeatureType.ROAD); //this is line 43
roadStyle.setElementType(MapTypeStyleElementType.ALL);
styles.push(roadStyle);
MapOptions mapOpts = MapOptions.create();
mapOpts.setZoom(4);
mapOpts.setCenter(LatLng.create(37.09024, -95.712891));
mapOpts.setMapTypeId(MapTypeId.TERRAIN);
mapOpts.setStreetViewControl(false);
mapOpts.setStyles(styles);
final GoogleMap map = GoogleMap.create(Document.get().getElementById("map_canvas"), mapOpts);
}
}
但是,当我运行它,我得到一个例外:
14:49:52.756 [ERROR] [gwttest] Uncaught exception escaped
java.lang.ExceptionInInitializerError: null
at com.test.client.GwtTest.createMap(GwtTest.java:43)
at com.test.client.GwtTest$1.run(GwtTest.java:25)
at com.google.gwt.ajaxloader.client.ExceptionHelper.runProtected(ExceptionHelper.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: null
at com.google.maps.gwt.client.MapTypeStyleFeatureType$.register(MapTypeStyleFeatureType.java:227)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
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.MapTypeStyleFeatureType$.create(MapTypeStyleFeatureType.java)
at com.google.maps.gwt.client.MapTypeStyleFeatureType$.<clinit>(MapTypeStyleFeatureType.java:39)
at com.test.client.GwtTest.createMap(GwtTest.java:43)
at com.test.client.GwtTest$1.run(GwtTest.java:25)
at com.google.gwt.ajaxloader.client.ExceptionHelper.runProtected(ExceptionHelper.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
at java.lang.Thread.run(Thread.java:745)
奇怪的是,Exception似乎是GoogleMaps内部的,所以我不知道发生了什么事情?
我在做什么样的东西显然是愚蠢的?
编辑:我也在GWT论坛上提过这个问题。
我发现一种解决方法涉及编写绕过GoogleMaps GWT API的原生JavaScript:
package com.test.client;
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.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 {
@Override
public void onModuleLoad() {
AjaxLoaderOptions options = AjaxLoaderOptions.newInstance();
options.setOtherParms("sensor=false");
Runnable callback = new Runnable() {
public void run() {
createMap();
}
};
AjaxLoader.loadApi("maps", "3", callback, options);
}
public void createMap() {
MapOptions mapOpts = MapOptions.create();
mapOpts.setZoom(4);
mapOpts.setCenter(LatLng.create(37.09024, -95.712891));
mapOpts.setMapTypeId(MapTypeId.TERRAIN);
mapOpts.setStreetViewControl(false);
GoogleMap map = GoogleMap.create(Document.get().getElementById("map_canvas"), mapOpts);
styleMap(map);
}
public native void styleMap(GoogleMap map) /*-{
map.set('styles', [
{
"featureType": "road",
"stylers": [
{ "visibility": "off" }
]
},{
"featureType": "poi",
"stylers": [
{ "visibility": "off" }
]
},{
"stylers": [
{ "invert_lightness": true }
]
}
]);
}-*/;
}
我仍然很想知道是否有纯粹的Java解决方法,但如果其他人有这个问题,这种本地方法就可以工作。 它还有额外的好处,它可以直接使用JSON,可以从GoogleMaps样式向导中导出。
链接地址: http://www.djcxy.com/p/83003.html上一篇: GWT GoogleMaps Hide Default Layers Using Styles
下一篇: Spark Streaming groupByKey and updateStateByKey implementation