- sheva.wen
- 等级: 初级会员
- 性别:
- 文章: 37
- 积分: 79
- 来自: 呼和浩特
|
下面要写的东西是一个使用GWT编写的在有限区域内通过拖动查看整个地图的简单实现。
一些细节:
- 为什么能拖?
com.google.gwt.user.client.ui.MouseListener 通过实现这个接口,可以实现UI对象对鼠标的Enter,Down,Leave,Move,Up动作的支持。
com.google.gwt.user.client.ui.FocusPanel 并不是每个UI对象都能够实现MouseListener接口的,可以通过添加到FocusPanel来实现。
- 怎么才能显示地图的局部,或者说怎么把其他部分藏起来?
通过样式:
java 代码
- DOM.setStyleAttribute(viewPortArea.getElement(), "overflow", "hidden");
-
- DOM.setStyleAttribute(viewPortArea.getElement(), "position","relative");
设置显示区域的"overflow"属性为"hidden"及"position"属性为"relative",图片可以实现局部显示显示框内。
代码:
java 代码
- package cn.gov.imwb.client;
-
- import com.google.gwt.user.client.DOM;
- import com.google.gwt.user.client.Event;
- import com.google.gwt.user.client.ui.AbsolutePanel;
- import com.google.gwt.user.client.ui.Composite;
- import com.google.gwt.user.client.ui.FocusPanel;
- import com.google.gwt.user.client.ui.Image;
- import com.google.gwt.user.client.ui.MouseListener;
- import com.google.gwt.user.client.ui.Widget;
-
-
-
-
-
- public class MapViewerPanel extends Composite {
- public int areaHeight;
-
- public int areaWidth;
-
- public int centerLeft;
-
- public int centerTop;
-
- public int imageHeight;
-
- public String imageUrl;
-
- public int imageWidth;
-
- public int maxLeft;
-
- public int maxTop;
-
- public int minLeft;
-
- public int minTop;
-
- private Image image;
-
- private FocusPanel mouseListenerContainer;
-
- public double showPositionLeftRatio;
-
- public double showPositionTopRatio;
-
- private int startLeft;
-
- private int startTop;
-
- private int startX;
-
- private int startY;
-
- private int state = 0;
-
- private AbsolutePanel viewArea;
-
- private AbsolutePanel viewPortArea = new AbsolutePanel();
-
-
-
-
-
-
-
-
-
-
-
- public MapViewerPanel(int areaWidth, int areaHeight, String imageUrl,
- int imageWidth, int imageHeight, double showPositionLeftRatio,
- double showPositionTopRatio) {
- mouseListenerContainer = new FocusPanel(viewPortArea);
- viewArea = new AbsolutePanel();
-
-
-
- image = new Image(){
-
-
-
- public void onBrowserEvent (Event event)
- {
- DOM.eventPreventDefault(event);
- super.onBrowserEvent(event);
- }
-
- };
- viewArea.add(image);
- image.setPixelSize(imageWidth, imageHeight);
- image.setUrl(imageUrl);
-
- viewPortArea.setPixelSize(areaWidth, areaHeight);
- viewPortArea.addStyleName("viewportViewer");
- DOM.setStyleAttribute(viewPortArea.getElement(), "overflow", "hidden");
-
- DOM.setStyleAttribute(viewPortArea.getElement(), "position",
- "relative");
- initWidget(mouseListenerContainer);
-
-
-
- this.showPositionLeftRatio = showPositionLeftRatio;
- this.showPositionTopRatio = showPositionTopRatio;
- this.areaWidth = areaWidth;
- this.areaHeight = areaHeight;
- this.imageUrl = imageUrl;
- this.imageWidth = imageWidth;
- this.imageHeight = imageHeight;
-
-
-
-
-
- maxLeft = imageWidth - areaWidth;
- maxTop = imageHeight - areaHeight;
- minLeft = -(imageWidth - areaWidth);
- minTop = -(imageHeight - areaHeight);
-
-
-
-
- centerLeft = (int) ((areaWidth - imageWidth) * showPositionLeftRatio);
- centerTop = (int) ((areaHeight - imageHeight) * showPositionTopRatio);
-
- viewPortArea.add(viewArea, centerLeft, centerTop);
-
-
-
- MouseListener listener = new MouseListener(){
-
- public void onMouseDown(Widget sender, int x, int y) {
- state = 1;
- startX = x;
- startY = y;
- startLeft = viewPortArea.getWidgetLeft(viewArea);
- startTop = viewPortArea.getWidgetTop(viewArea);
-
- }
-
- public void onMouseEnter(Widget sender) {
-
- }
-
- public void onMouseLeave(Widget sender) {
- state = 0;
- DOM.setStyleAttribute(mouseListenerContainer.getElement(), "cursor", "default");
- }
-
- public void onMouseMove(Widget sender, int x, int y) {
- if(state == 1){
- DOM.setStyleAttribute(mouseListenerContainer.getElement(), "cursor", "move");
- if(viewPortArea.getWidgetLeft(viewArea) >= minLeft
- && viewPortArea.getWidgetLeft(viewArea) <= maxLeft
- && viewPortArea.getWidgetTop(viewArea) >= minTop
- && viewPortArea.getWidgetTop(viewArea) <= maxTop){
- viewPortArea.setWidgetPosition(viewArea,startLeft + (x - startX), startTop + (y - startY));
- }
- }
- }
-
- public void onMouseUp(Widget sender, int x, int y) {
-
- if(viewPortArea.getWidgetLeft(viewArea) < minLeft){
- viewPortArea.setWidgetPosition(viewArea, minLeft, viewPortArea.getWidgetTop(viewArea));
- }
- if(viewPortArea.getWidgetTop(viewArea) < minTop){
- viewPortArea.setWidgetPosition(viewArea, viewPortArea.getWidgetLeft(viewArea), minTop);
- }
- if(viewPortArea.getWidgetLeft(viewArea) > 0){
- viewPortArea.setWidgetPosition(viewArea, 0, viewPortArea.getWidgetTop(viewArea));
- }
- if(viewPortArea.getWidgetTop(viewArea) > 0){
- viewPortArea.setWidgetPosition(viewArea, viewPortArea.getWidgetLeft(viewArea), 0);
- }
- state = 0;
- DOM.setStyleAttribute(mouseListenerContainer.getElement(), "cursor", "default");
- }
-
- };
- mouseListenerContainer.addMouseListener(listener);
-
- }
-
- }
代码很容易理解,这只是一个很简易的实现,至少还有以下需要完善:
- 地图分块载入
- 支持鼠标滚轮的缩放
- “鹰眼”功能。
感兴趣的跟进一下:)
- MapViewer.rar (745.6 KB)
- 描述: Project(Eclipse下打开,须引入gwt-user.jar)
- 下载次数: 364
- Demo.rar (278.4 KB)
- 描述: Demo(解压后运行MapViewer.html查看效果)
- 下载次数: 489
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|
- liuxyu
- 等级: 初级会员
- 性别:
- 文章: 2
- 积分: 30
- 来自: 杭州
|
用GWT做google map我有兴趣。
先试了下你的demo。在滕讯浏览器下脚本错误,不能运行。IE下正常。
程序还没来得及看。
|
返回顶楼 |
|
|
- sheva.wen
- 等级: 初级会员
- 性别:
- 文章: 37
- 积分: 79
- 来自: 呼和浩特
|
感兴趣就跟进以下:)
我用TT查看过了,好像没什么问题。
把脚本错误发来看看。
|
返回顶楼 |
|
|
- liuxyu
- 等级: 初级会员
- 性别:
- 文章: 2
- 积分: 30
- 来自: 杭州
|
行: 3
字符: 39
错误: 尚未实现
代码: 0
我刚系统重装。tt版本可能有点老。2.02
不过之前的tt应该比较新
|
返回顶楼 |
|
|
- sheva.wen
- 等级: 初级会员
- 性别:
- 文章: 37
- 积分: 79
- 来自: 呼和浩特
|
@liuxyu
看不出什么,IE报错太恶心。
不想在这个问题上花时间了。
|
返回顶楼 |
|
|
- 纯情小火鸡
- 等级: 初级会员
- 性别:
- 文章: 33
- 积分: 40
- 来自: 上海
|
请问楼主 还有没有实现其他的功能了?比如加载地图图片 赢眼等的啊 能留下你的联系方式吗?
|
返回顶楼 |
|
|
- 纯情小火鸡
- 等级: 初级会员
- 性别:
- 文章: 33
- 积分: 40
- 来自: 上海
|
sheva.wen 写道 下面要写的东西是一个使用GWT编写的在有限区域内通过拖动查看整个地图的简单实现。 一些细节: - 为什么能拖?
com.google.gwt.user.client.ui.MouseListener 通过实现这个接口,可以实现UI对象对鼠标的Enter,Down,Leave,Move,Up动作的支持。 com.google.gwt.user.client.ui.FocusPanel 并不是每个UI对象都能够实现MouseListener接口的,可以通过添加到FocusPanel来实现。 - 怎么才能显示地图的局部,或者说怎么把其他部分藏起来?
通过样式: java 代码 - DOM.setStyleAttribute(viewPortArea.getElement(), "overflow", "hidden");
-
- DOM.setStyleAttribute(viewPortArea.getElement(), "position","relative");
设置显示区域的"overflow"属性为"hidden"及"position"属性为"relative",图片可以实现局部显示显示框内。
代码: java 代码 - package cn.gov.imwb.client;
-
- import com.google.gwt.user.client.DOM;
- import com.google.gwt.user.client.Event;
- import com.google.gwt.user.client.ui.AbsolutePanel;
- import com.google.gwt.user.client.ui.Composite;
- import com.google.gwt.user.client.ui.FocusPanel;
- import com.google.gwt.user.client.ui.Image;
- import com.google.gwt.user.client.ui.MouseListener;
- import com.google.gwt.user.client.ui.Widget;
-
-
-
-
-
- public class MapViewerPanel extends Composite {
- public int areaHeight;
-
- public int areaWidth;
-
- public int centerLeft;
-
- public int centerTop;
-
- public int imageHeight;
-
- public String imageUrl;
-
- public int imageWidth;
-
- public int maxLeft;
-
- public int maxTop;
-
- public int minLeft;
-
- public int minTop;
-
- private Image image;
-
- private FocusPanel mouseListenerContainer;
-
- public double showPositionLeftRatio;
-
- public double showPositionTopRatio;
-
- private int startLeft;
-
- private int startTop;
-
- private int startX;
-
- private int startY;
-
- private int state = 0;
-
- private AbsolutePanel viewArea;
-
- private AbsolutePanel viewPortArea = new AbsolutePanel();
-
-
-
-
-
-
-
-
-
-
-
- public MapViewerPanel(int areaWidth, int areaHeight, String imageUrl,
- int imageWidth, int imageHeight, double showPositionLeftRatio,
- double showPositionTopRatio) {
- mouseListenerContainer = new FocusPanel(viewPortArea);
- viewArea = new AbsolutePanel();
-
-
-
- image = new Image(){
-
-
-
- public void onBrowserEvent (Event event)
- {
- DOM.eventPreventDefault(event);
- super.onBrowserEvent(event);
- }
-
- };
- viewArea.add(image);
- image.setPixelSize(imageWidth, imageHeight);
- image.setUrl(imageUrl);
-
- viewPortArea.setPixelSize(areaWidth, areaHeight);
- viewPortArea.addStyleName("viewportViewer");
- DOM.setStyleAttribute(viewPortArea.getElement(), "overflow", "hidden");
-
- DOM.setStyleAttribute(viewPortArea.getElement(), "position",
- "relative");
- initWidget(mouseListenerContainer);
-
-
-
- this.showPositionLeftRatio = showPositionLeftRatio;
- this.showPositionTopRatio = showPositionTopRatio;
- this.areaWidth = areaWidth;
- this.areaHeight = areaHeight;
- this.imageUrl = imageUrl;
- this.imageWidth = imageWidth;
- this.imageHeight = imageHeight;
-
-
-
-
-
- maxLeft = imageWidth - areaWidth;
- maxTop = imageHeight - areaHeight;
- minLeft = -(imageWidth - areaWidth);
- minTop = -(imageHeight - areaHeight);
-
-
-
-
- centerLeft = (int) ((areaWidth - imageWidth) * showPositionLeftRatio);
- centerTop = (int) ((areaHeight - imageHeight) * showPositionTopRatio);
-
- viewPortArea.add(viewArea, centerLeft, centerTop);
-
-
-
- MouseListener listener = new MouseListener(){
-
- public void onMouseDown(Widget sender, int x, int y) {
- state = 1;
- startX = x;
- startY = y;
- startLeft = viewPortArea.getWidgetLeft(viewArea);
- startTop = viewPortArea.getWidgetTop(viewArea);
-
- }
-
- public void onMouseEnter(Widget sender) {
-
- }
-
- public void onMouseLeave(Widget sender) {
- state = 0;
- DOM.setStyleAttribute(mouseListenerContainer.getElement(), "cursor", "default");
- }
-
- public void onMouseMove(Widget sender, int x, int y) {
- if(state == 1){
- DOM.setStyleAttribute(mouseListenerContainer.getElement(), "cursor", "move");
- if(viewPortArea.getWidgetLeft(viewArea) >= minLeft
- && viewPortArea.getWidgetLeft(viewArea) <= maxLeft
- && viewPortArea.getWidgetTop(viewArea) >= minTop
- && viewPortArea.getWidgetTop(viewArea) <= maxTop){
- viewPortArea.setWidgetPosition(viewArea,startLeft + (x - startX), startTop + (y - startY));
- }
- }
- }
-
- public void onMouseUp(Widget sender, int x, int y) {
-
- if(viewPortArea.getWidgetLeft(viewArea) < minLeft){
- viewPortArea.setWidgetPosition(viewArea, minLeft, viewPortArea.getWidgetTop(viewArea));
- }
- if(viewPortArea.getWidgetTop(viewArea) < minTop){
- viewPortArea.setWidgetPosition(viewArea, viewPortArea.getWidgetLeft(viewArea), minTop);
- }
- if(viewPortArea.getWidgetLeft(viewArea) > 0){
- viewPortArea.setWidgetPosition(viewArea, 0, viewPortArea.getWidgetTop(viewArea));
- }
- if(viewPortArea.getWidgetTop(viewArea) > 0){
- viewPortArea.setWidgetPosition(viewArea, viewPortArea.getWidgetLeft(viewArea), 0);
- }
- state = 0;
- DOM.setStyleAttribute(mouseListenerContainer.getElement(), "cursor", "default");
- }
-
- };
- mouseListenerContainer.addMouseListener(listener);
-
- }
-
- }
代码很容易理解,这只是一个很简易的实现,至少还有以下需要完善: - 地图分块载入
- 支持鼠标滚轮的缩放
- “鹰眼”功能。
感兴趣的跟进一下:)
|
返回顶楼 |
|
|