【问题标题】:is it possible to move the map on touch in blackberry by using MapField.是否可以使用 MapField 在黑莓中移动地图。
【发布时间】:2011-12-19 05:58:55
【问题描述】:

我正在使用 mapField 创建自定义地图。我正在使用此链接中的代码。 How to show more than one location in Blackberry MapField?。 但是地图位置是固定的。我无法像在谷歌地图中那样拖动地图,也无法像在调用地图时那样拖动地图

 public void execute(ReadOnlyCommandMetadata metadata, Object context) 
        {
            Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, new MapsArguments());
        }

【问题讨论】:

  • 我只是在 JDE 模拟器上尝试一下。 6.0版

标签: blackberry maps


【解决方案1】:

这里有一些代码可以让你走上正确的道路。我是从我的一个有一些特殊要求的项目中取出它的,所以可能会在不经意间留下一些残留物。那里会有一些未定义的变量——它们是在类中声明的成员变量,并且都应该以下划线开头。这也是扩展MapField 的类的一部分,因此您必须创建一个自定义地图类,然后使用它而不是默认的。

protected boolean touchEvent(TouchEvent message) {
    boolean ret =  super.touchEvent(message);

    //mark that we're starting to interact
    if(message.getEvent() == TouchEvent.DOWN) {
        _startTouchTracking = true;
        _clicking = true;

        _touchX = message.getX(1);
        _touchY = message.getY(1);

    }
    //user is wanting to move the map
    else if(message.getEvent() == TouchEvent.MOVE) {
        int dx = _touchX - message.getX(1);
        int dy = _touchY - message.getY(1);
        _clicking = false;
        _touchX = message.getX(1);
        _touchY = message.getY(1);

        //perform checks to make sure we don't move outside of the map's range
        int lat = getLatitude() - dy*(int)MathUtilities.pow(2, (double)getZoom());
        if(lat < -9000000) {
            lat = -9000000;
        }
        else if (lat > 9000000) {
            lat = 9000000;
        }
        int lon = getLongitude() + dx*(int)MathUtilities.pow(2, (double)getZoom());
        if(lon < -18000000) {
            lon = -18000000;
        }
        else if (lon > 18000000) {
            lon = 18000000;
        }

        moveTo(lat, lon);
    }
    //if the person just touches and releases, we want to move to that spot
    else if (message.getEvent() == TouchEvent.UNCLICK && _clicking) {
        int dx = message.getX(1) - getWidth()/2;
        int dy = message.getY(1) - getHeight()/2;
        move(dx, dy);
        _clicking = false;
    }
    //touch has been released
    else if (message.getEvent() == TouchEvent.UP) {
        _startTouchTracking = false;
    }
    //we handled the click
    return true;
}

如前所述,这可能需要针对您的使用进行调整,但总的来说应该可以帮助您入门。 MathUtilities.pow() 调用是我根据缩放级别提出适当运动量的方法。

编辑评论

让位图随地图移动:

protected Coordinates _bitmapCoordinates;
protected Bitmap _bitmap;
public YourMapField() {
    //we're going to put the bitmap at -38.43, 20.32
    _bitmapCoordinates = new Coordinates(-38.43, 20.32, 0.0);
    _bitmap = YOUR_CODE_TO_GET_THE_BITMAP;
}

protected void paint(Graphics g) {
    super.paint(g);
    XYPoint placeToPaintBitmap = new XYPoint();
    convertWorldToField(_bitmapCoordinates, placeToPaintBitmap);

    //perform a check here to make sure that field will be seen. This code would depend
    //on how you're painting the image. Just check the placeToPaintBitmap.x and placeToPaintBitmap.y
    //against 0 and the map's width and height, along with some adjustment for how you paint
    if(bitmap will be visible on the screen) {

        //The code I have here is drawing the bitmap from the top left of the image, but if
        //you need to draw from some other place you may have to offset the x and y 
        g.drawBitmap(placeToPaintBitmap.x, placeToPaintBitmap.y, _bitmap.getWidth(), _bitmap.getHeight(), 0, 0);
    }
}

我没有测试任何代码,所以它可能有问题,但应该给你一个大致的想法。

【讨论】:

  • 嗨,利润.. 它移动 mapField 但仅移动到特定点,当我尝试再次移动屏幕时它没有移动
  • 嘿,谢谢.. 它的工作 f9 nw.. 我还想知道一件事。我将位图放在地图的顶部。但它是固定的,我希望它与地图一起移动.. 请帮助
  • 是的,我把它放在某个坐标上。你能解释一下这个 placeToPaintTheBitmap 是什么吗?
  • 嘿,你能帮我写一些我是黑莓新手的代码
猜你喜欢
  • 1970-01-01
  • 2011-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多