1

I am using mapField to create a custom map.I am using the code in this link. How to show more than one location in Blackberry MapField?. But the map position is fixed. i am not able to drag the map as we can do in google maps or when we invoke the maps like

 public void execute(ReadOnlyCommandMetadata metadata, Object context) 
        {
            Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, new MapsArguments());
        }
Community
  • 1
  • 1
Swati
  • 1,179
  • 9
  • 28

1 Answers1

3

Here's some code that should get you going on the correct path. I've taken it from a project of mine that had some special requirements, so there could be some remnants of that left in there inadvertently. There will be some undefined variables in there -- they're member variables that are declared in the class and should all start with an underscore. This is also part of a class that extends MapField, so you would have to create a custom map class and then use that rather than the default.

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;
}

As said, this might need tweaking for your use, but in general should get you started. The MathUtilities.pow() calls were my way of coming up with an appropriate amount of motion depending on the zoom level.

Edit for Comments

Letting a Bitmap move with the map:

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);
    }
}

I didn't test any of that code, so it might be buggy but should give you the general idea.

Nate
  • 31,017
  • 13
  • 83
  • 207
jprofitt
  • 10,874
  • 4
  • 36
  • 46
  • 1
    hi profitt.. its moving the mapField but to a particular point only and when I try to move the screen again its not moving – Swati Dec 20 '11 at 05:39
  • I changed the line `move(lat, lon)` to `moveTo(lat, lon)`, see if that fixes it. – jprofitt Dec 20 '11 at 13:09
  • 1
    hey thanks.. its working f9 nw.. One more thing I wanted to know. I am placing the bitmap on top of the map. but it is fixed and i want it to move with the map.. Please help – Swati Dec 21 '11 at 05:33
  • If your Bitmap is being placed at a certain coordinate, you can use `MapField.convertWorldToField(Coordinates bitmapLatLon, XYPoint placeToPaintTheBitmap)` and then check if `placeToPaintTheBitmap`, which will give you an (x, y) location in pixels that relates to the same (lat, lon) location, will be viewable. – jprofitt Dec 21 '11 at 12:57
  • 1
    ya i am placing it at certain coordinate.Can you please explain what is this placeToPaintTheBitmap ? – Swati Dec 21 '11 at 13:01
  • It will be the spot you use in your `paint()` method to paint it on the screen. – jprofitt Dec 21 '11 at 13:02
  • 1
    hey can you please help me with some code I am new to blackberry – Swati Dec 21 '11 at 13:05
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6038/discussion-between-swati-and-jprofitt) – Swati Dec 21 '11 at 13:08
  • 1
    thanxx alot.. actually I am already checking the bitmap visibility but problem come when I am moving the Map or you can say dragging the Map – Swati Dec 21 '11 at 13:28
  • 1
    hey another problem is occurring.. by the time map loads the bitmap placed over the Mapfield move alone and the map doesn't move. Please help – Swati Dec 30 '11 at 03:56
  • Hmm, maybe call `invalidate()` on the map when you move. – jprofitt Dec 30 '11 at 04:46
  • 1
    hi.. i need one more help.. i have mentioned how i am drawing the bitmap can u help me how to trace the event on that bitmap. – Swati Jan 10 '12 at 13:18
  • I'm not sure what you mean by "trace the event" – jprofitt Jan 10 '12 at 13:40
  • 1
    I am mean i want to take action on click on the bitmap. Is it possible? – Swati Jan 11 '12 at 04:16
  • Yeah, you have to do some calculations in the touchEvent() to determine what the bounds of each bitmap would be on the screen using the `worldToField()` and `fieldToWorld()` methods. – jprofitt Jan 11 '12 at 13:02