Careers | Blog | Sitemap  
   
   
  Wanna try working with us?  
  email: info@hibiscustech.com  
     
 

Archive for February, 2012

BlackBerry App Development - Overriding of Menu button

Wednesday, February 15th, 2012

By using this piece of code you can override the blackberry default menu. What you all need to get is the ID of a particular item in the code. The code given below is to override the close button (in this case id is 9). On click of close you can perform your own task.

====================================================================

protected void onMenuDismissed(Menu menu) {

super.onMenuDismissed(menu);

if (menu.getSelectedItem().getId() == 9) {

//TODO Something

//System.exit(0);

}

}

BlackBerry Application Development - Geo Coding

Wednesday, February 15th, 2012

By using Locator class one can get the location information of a given latitude and longitude. But for OS 7 if one uses locator class, it will give an exception. So in that case, one can use Google APIs to get the location address. The code given below is all about how to use the Google APIs to get complete address details of a given latitude and longitude.

===================================================================

public class Geocoder {

private static String networkString = “”;

private double[] coord = new double[2];

private String name = “”;

public double[] getAddressCordinate(String name) {

name = Split(name);

String url = “http://maps.googleapis.com/maps/api/geocode/xml?address=”

+ name + “&sensor=true”;

try {

if (getnetworkString()) {

HttpConnection connection = (HttpConnection) Connector.open(url

+ networkString);

connection.setRequestMethod(HttpConnection.GET);

InputStream istream = connection.openInputStream();

SAXParserFactory spf = SAXParserFactory.newInstance();

SAXParser sp = spf.newSAXParser();

sp.parse(istream, new LocationParser(this, false));

istream.close();

connection.close();

}

} catch (Exception e) {

}

return coord;

}

private String Split(String name) {

String newName = “”;

for (int i = 0; i < name.length(); i++) {

if (name.charAt(i) == ‘ ‘)

newName += “%20″;

else

newName += name.charAt(i);

}

return newName;

}

private boolean getnetworkString() {

if (DeviceInfo.isSimulator()) {

networkString = “;deviceside=true;ConnectionTimeout=20000″;

return true;

}

else if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {

networkString = “;interface=wifi”;

return true;

}

else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {

String carrierUid = getCarrierBIBSUid();

if (carrierUid == null) {

networkString = “;deviceside=true”;

return true;

} else {

networkString = “;deviceside=false;connectionUID=” + carrierUid

+ “;ConnectionType=mds-public”;

return true;

}

}

else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {

networkString = “;deviceside=false”;

return true;

}

else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE) {

return false;

}

else {

networkString = “;deviceside=true”;

return true;

}

}

private String getCarrierBIBSUid() {

ServiceRecord[] records = ServiceBook.getSB().getRecords();

int currentRecord;

for (currentRecord = 0; currentRecord < records.length; currentRecord++) {

if (records[currentRecord].getCid().toLowerCase().equals(“ippp”)) {

if (records[currentRecord].getName().toLowerCase()

.indexOf(“bibs”) >= 0) {

return records[currentRecord].getUid();

}

}

}

return null;

}

public void upDateLocation(double latitude, double longitude) {

coord[0] = latitude;

coord[1] = longitude;

}

public String reverseGeocode(double lat, double lng) {

String url = “http://maps.google.com/maps/api/geocode/xml?sensor=false&latlng=”

+ lat + “,” + lng;

try {

if (getnetworkString()) {

HttpConnection connection = (HttpConnection) Connector.open(url

+ networkString);

connection.setRequestMethod(HttpConnection.GET);

InputStream istream = connection.openInputStream();

SAXParserFactory spf = SAXParserFactory.newInstance();

SAXParser sp = spf.newSAXParser();

sp.parse(istream, new LocationParser(this, true));

istream.close();

connection.close();

}

} catch (Exception e) {

}

return name;

}

public void setName(String name) {

this.name = name;

}

}

public class LocationParser extends DefaultHandler {

private boolean getName = false;

private boolean lat = false;

private boolean longi = false;

private double latitude = 0;

private double longitude = 0;

Geocoder geocoder;

private String address = “”;

private boolean addressfound = false;

public LocationParser(Geocoder geocoder, boolean getName) {

this.getName = getName;

this.geocoder = geocoder;

}

public void startDocument() throws SAXException {

super.startDocument();

}

public void startElement(String uri, String localName, String qName,

Attributes attributes) throws SAXException {

super.startElement(uri, localName, qName, attributes);

if (localName.equals(“lat”))

lat = true;

if (localName.equals(“lng”))

longi = true;

if (localName.equals(“formatted_address”))

addressfound = true;

}

public void characters(char[] ch, int start, int length)

throws SAXException {

super.characters(ch, start, length);

if (lat) {

lat = false;

latitude = Double.parseDouble(new String(ch, start, length));

}

if (longi) {

longi = false;

longitude = Double.parseDouble(new String(ch, start, length));

}

if (addressfound) {

if (address.equals(“”))

address = new String(ch, start, length);

addressfound = false;

}

}

public void endElement(String uri, String localName, String qName)

throws SAXException {

super.endElement(uri, localName, qName);

}

public void endDocument() throws SAXException {

super.endDocument();

if (getName == false)

geocoder.upDateLocation(latitude, longitude);

else

geocoder.setName(address);

}

}

Blackberry App Development - Map - Achieving map pin click for non touch devices - scrolling of map for touch devices.

Wednesday, February 15th, 2012

One can draw pin over the map and click on each pin to perform a particular task like going to the subsequent screen, view address etc. It’s easy to achieve this task on touch devices but performing same task in non touch device has its own difficulties associated with it. To achieve this, one can use the code given below.

We are showing a small circle (a PNG image) in the centre of the map. One need to scroll top / bottom or right / left and place a pin in the centre of the map then on click you will be able to perform a particular task .

Now coming to touch device, if you are using your own map class, I have read in many forum and blog that it’s not possible and very difficult to scroll the map. Here is a simple solution to achieve this. You can scroll the entire map by using the code given below.

Blackberry Non Touch Device Mapping Pins

Blackberry Non Touch Device Mapping Pins

===================================================================

public class MapScreen extends MainScreen implements

FocusChangeListener, FieldChangeListener {

private DemoMapField _mapField;;

public MapScreen(){

// TODO something.

}

protected boolean keyChar(char c, int status, int time) {

if (c == ‘!’) {// Zoom In

_mapField.setZoom(Math.max(_mapField.getZoom() - 1,

_mapField.getMinZoom()));

}

return true;

} else if (c == ‘@’) {// Zoom out

_mapField.setZoom(Math.min(_mapField.getZoom() + 1,

_mapField.getMaxZoom()));

}

return true;

}

return super.keyChar(c, status, time);

}

protected boolean navigationMovement(int dx, int dy, int status, int time)    {

return _mapField.navigationMovement(dx, dy, status, time, this);

}

return true;

}

protected boolean navigationClick(int status, int time) {

// Call : _mapField.getHighlightedLocation();

//TODO Something

return true;

}

protected boolean touchEvent(TouchEvent message) {

boolean isConsumed = false;

try{

if (message.getEvent() == TouchEvent.CLICK) {

super.touchEvent(message);

_ mapField.setFocus();

HandleMapClick(message);

}

}

}catch (Exception e) {

}

try{

TouchGesture touchGesture = message.getGesture();

if (touchGesture!=null && touchGesture.getEvent() == TouchGesture.SWIPE)

{

if (touchGesture.getSwipeDirection() == TouchGesture.SWIPE_EAST) {

for (int index = 0; index < 6; index++)

navigationMovement(-index, 0, 1, 0);

return true;

}

if (touchGesture.getSwipeDirection() == TouchGesture.SWIPE_NORTH) {

for (int index = 0; index < 6; index++)

navigationMovement(0, index, 1, 0);

return true;

}

if (touchGesture.getSwipeDirection() == TouchGesture.SWIPE_SOUTH) {

for (int index = 0; index < 6; index++)

navigationMovement(0, -index, 1, 0);

return true;

}

if (touchGesture.getSwipeDirection() == TouchGesture.SWIPE_WEST) {

for (int index = 0; index < 6; index++)

navigationMovement(index, 0, 1, 0);

return true;

}

}

}catch (Throwable e) {

}

return isConsumed;

}

private void HandleMapClick(TouchEvent message) {

try {

_mapField.setClick(message);

// Call : _mapField.getHighlightedLocation();

//TODO Something

} catch (Exception e) {

}

}

***************************************************************************

public class DemoMapField extends MapField{

private Vector _allLocations = new Vector();

private MapLocation _highlightedLocation;

private Bitmap bmpCursor = Bitmap.getBitmapResource(“highlighted circle image”);

private Bitmap bmpPinRed = Bitmap.getBitmapResource(“an png image- pin”);

private Bitmap bmpPinBlue = Bitmap.getBitmapResource(“an png image-pin “);

protected void paint(Graphics g) {

g.setDrawingStyle(Graphics.DRAWSTYLE_AAPOLYGONS, true);

super.paint(g);

if (!Touchscreen.isSupported()) {

determineSelectedLocation(new XYPoint(getWidth() >> 1,

getHeight() >> 1));

g.drawBitmap((getWidth() - bmpCursor.getWidth()) >> 1,

(getHeight() - bmpCursor.getHeight()) >> 1, getWidth(),

getHeight(), bmpCursor, 0, 0);

}

if (this.currentLocation != null) {

MapLocation currentSite = new MapLocation(this.currentLocation[0],

this.currentLocation[1], “”, “”, “”, “”);

drawSite(g, currentSite, bmpPinRed);

}

for (int count = 0; count < _allLocations.size(); count++) {

MapLocation currentSite = (MapLocation) _allLocations

.elementAt(count);

drawSite(g, currentSite, bmpPinBlue);

}

g.setColor(Color.BLACK);

g.drawText(“Use ‘!’ to zoom in”, 1,

g.getFont().getHeight() + 2);

g.drawText(“Use ‘@’ to zoom out”, 1,

(g.getFont().getHeight() * 2) + 4);

}

public void determineSelectedLocation(XYPoint colisionWith) {

if (_allLocations == null || _allLocations.size() == 0)

return;

XYPoint[] locationsXY = new XYPoint[_allLocations.size()];

for (int count = 0; count < locationsXY.length; count++) {

MapLocation currentSite = (MapLocation) _allLocations

.elementAt(count);

Coordinates co = new Coordinates(currentSite.getLatitude(),

currentSite.getLongitude(), 0);

XYPoint p = new XYPoint();

convertWorldToField(co, p);

locationsXY[count] = p;

}

int closerIndex = 0;

int closerValue = getDiff(colisionWith, locationsXY[0]);

for (int i = 1; i < locationsXY.length; i++) {

int diff = getDiff(colisionWith, locationsXY[i]);

if (diff < closerValue) {

closerValue = diff;

closerIndex = i;

}

}

if (closerValue <= 60) {

_highlightedLocation = (MapLocation) _allLocations

.elementAt(closerIndex);

} else {

_highlightedLocation = null;

}

}

private int getDiff(XYPoint p1, XYPoint p2) {

return Math.abs(Math.abs(p1.x) - Math.abs(p2.x))

+ Math.abs(Math.abs(p1.y) - Math.abs(p2.y));

}

void drawSite(Graphics g, MapLocation currentSite, Bitmap bmp) {

Coordinates co = new Coordinates(currentSite.getLatitude(),

currentSite.getLongitude(), 0);

XYPoint point = new XYPoint();

convertWorldToField(co, point);

if (!(point.x < 0 || point.x > getWidth() || point.y < 0 || point.y > getHeight())) {

g.drawBitmap(point.x - 5, point.y - bmp.getHeight() + 10,

bmp.getWidth(), bmp.getHeight(), bmp, 0, 0);

}

}

public void setZoom(int zoom) {

super.setZoom(zoom);

}

public boolean navigationMovement(int dx, int dy, int s, int t,

MainScreen mainScreen) {

int zoom = getZoom();

int latitude = getLatitude() - ((dy << 3) << zoom); // << 3 is

int longitude = getLongitude() + ((dx << 3) << zoom);

{

moveTo(latitude, longitude);

}

return true;

}

private boolean FindMaximum(double latitude, int dx, int dy, int zoom) {

double minLat = Double.MAX_VALUE;

for (int i = 0; i < _allLocations.size(); i++) {

MapLocation location = (MapLocation) _allLocations.elementAt(i);

if (location.getLatitude() < minLat)

minLat = location.getLatitude();

}

if (minLat < latitude)

return false;

else

return true;

}

private boolean FindMinimum(double latitude, int dx, int dy, int zoom) {

double maxLat = Double.MIN_VALUE;

for (int i = 0; i < _allLocations.size(); i++) {

MapLocation location = (MapLocation) _allLocations.elementAt(i);

if (location.getLatitude() > maxLat)

maxLat = location.getLatitude();

}

if (maxLat > latitude)

return false;

else

return true;

}

protected boolean touchEvent(TouchEvent message) {

if (message.getEvent() == TouchEvent.CLICK) {

int x = message.getX(1) - getLeft();

int y = message.getY(1) - getTop();

determineSelectedLocation(new XYPoint(x, y));

return true;

}

return super.touchEvent(message);

}

void addSite(MapLocation site) {

_allLocations.addElement(site);

}

public void setLocations(Vector allLocations) {

this._allLocations = allLocations;

}

public MapLocation getHighlightedLocation() {

return _highlightedLocation;

}

private double[] currentLocation = null;

public void setCurrentLocation(double[] currentLocation) {

this.currentLocation = currentLocation;

}

public void setClick(TouchEvent message) {

touchEvent(message);

}

}


  Home | About Us | Products | Portfolio | Work with us | Careers | Blog | Sitemap