Use Google Map in Blackberry application

戏子无情 提交于 2019-11-27 04:42:20

Recently I had an idea to use Google Maps website from Browser.Field but it's not possible since GMaps are based on JavaScript and it's badly supported by Blackberry native Browser.

Actually there are 2 ways of using Google Maps on Blackberry:

Here is a little example:

The Form to view the Google Maps Static image:

public class frmMap extends Form implements CommandListener {

    Command _back;
    MIDlet midlet;
    Form dis;

    public frmMap(String title, ImageItem img, MIDlet m, Form d){
        super(null);

        this.midlet = m;
        this.dis = d;

        _back = new Command("Back", Command.BACK, 1);
        addCommand(_back);
        append(img);
        setCommandListener(this);        
    }

    public void commandAction(Command c, Displayable d) {
        if(c == _back){
            Display.getDisplay(midlet).setCurrent(dis);
        }
    }

}

The class inet class to download the static image:

public class INETclass implements Runnable {

    private String _location = null;
    private HttpConnection inet;
    private Pispaal _m;
    public String url = null;

    public INETclass(String location, Pispaal m){
        _location = location;
        _m = m;        
    }

    public void run() {
        try
        {
            //Setup the connection
            inet = (HttpConnection)Connector.open(url);
            inet.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            int rc = inet.getResponseCode();

            //Responsecode controleren
            if(rc == HttpConnection.HTTP_OK){
                //Open input stream to read the respone
                DataInputStream is = new DataInputStream(inet.openInputStream());                
                StringBuffer sb = new StringBuffer();

                int ch;
                long len = -1;
                byte[] buffer = null;
                if(_location == null){
                    len = is.available();
                }

                if(len != -1){
                    if(_location == null){
                        buffer = IOUtilities.streamToBytes(is);
                    }else{
                        while((ch = is.read()) != -1){
                            sb.append((char)ch);
                        }
                    }
                }
                is.close();

                if(_location == null){
                    _m.OnINETComplete(buffer);
                }else{
                    _m.Alert(sb.toString());
                }
            }else{
                _m.Alert("URL " + url + " geeft response code: " + rc);
                try
                {
                    inet.close();
                }catch(Exception e){
                    _m.Alert("Error: " + e.getMessage());
                }
            }

        }
        catch(Exception e)
        {
            _m.Alert("Error: " + e.getMessage());
            System.out.println("Error: " + e.getMessage());
        }
        finally
        {
            try
            {
                if(inet != null){ inet.close(); }  
                Thread.currentThread().join(); //Making sure this thread dies
            }catch(Exception e){
                _m.Alert("Error: " + e.getMessage());
                System.out.println("Error: " + e.getMessage());
            }
        }
    }
}

The Button action that starts the download and the callback action that loads the form to view the image

public void commandAction(Command c, Displayable d) {
        synchronized(c){
            String loc = _location.getText();
            if(loc.indexOf(",") > 0){
                //if(c == _strCommand){                
                    //INETclass inet = new INETclass(loc, this);
                    //Thread tInet = new Thread(inet);
                    //tInet.start();
                    //Alert("Locatie word doorgestuurd. Even geduld");
                //}else 
                if(c == _mapView){
                    INETclass inet = new INETclass(null, this);
                    inet.url = "http://www.qeueq.com/gmap.php?location=" + this.lat + "," + this.lon + "&size=" + this.width + "x" + this.height + ";deviceside=true";
                    Thread tInet = new Thread(inet);
                    tInet.start();
                }
            }else{
                Alert("GPS locatie is nog niet beschikbaar.");
            }
        }
    }

public void UpdateLocation(double lat, double lon){
       String location = lat + "," + lon;
       this.lat = lat;
       this.lon = lon;
       synchronized(location){
           _location.setText(location);
            INETclass inet = new INETclass(location, this);
            Thread tInet = new Thread(inet);
            tInet.start();           
       } 
    }

Refine and edit the code so it fits your needs. Took me some time to get it right.

It is possible now to use Google Maps instead of BlackBerry maps with our own data like in the image.

If you're looking to use google maps to show your own locations/markers you can invoke google maps using ApplicationDescriptor from your application. Check for google maps on device using CodeModuleManager.getModuleHandle("GoogleMaps"); it returns an integer where non zero means it is available. Then you can add locations in your KML file, you can even customize location pointers using KML file tags.

The example as linked by Max allows a single marker only. So a KML file becomes necessary if multiple markers are to be added.

You may look at the simple tutorial here for beginners.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!