Question

I accessed google map in J2ME by using following url:

String url = "http://maps.google.com/maps/api/staticmap?center="+lat+","+lon+"&zoom="+zoom+"&size=" + width + "x" + height+"&maptype=roadmap"+"&markers=color:red|label:A|" + lat + "," + lon+"&sensor=true";

I want to show Snippet using this url.

Please let me know where to put snippet code in the url and what will be url code for it?

Kind Regards,

Parmanand

Was it helpful?

Solution

You could download your image using code like this:

 private Image getImage(String url) {
        ContentConnection c = null;
        DataInputStream dis = null;
        try {
            try {
                c = (ContentConnection) Connector.open(url);
                int len = (int) c.getLength();
                dis = c.openDataInputStream();
                if (len > 0) {
                    byte[] data = new byte[len];
                    dis.readFully(data);
                    im = Image.createImage(data, 0, data.length);
                }
            } catch (IOException ioe) {
                // Failed to read the url. Can't do anything about it, just don't
                // update the image.
            } finally {
                // Regardless of whether we are successful, we need to close
                // Connections behind us. Basic Housekeeping.
                if (dis != null) {
                    dis.close();
                }
                if (c != null) {
                    c.close();
                }
            }
        } catch (IOException ioe) {
            // closure of connections may fail, nothing we can do about it.
        }
    }

The Image can be displayed in a Form as an ImageItem for example:

  ImageItem imgItem = 
         new ImageItem("Default: ",  getImage(url),     
                       Item.LAYOUT_CENTER, null,    
                       Item.BUTTON);

By the way, the above code snippet should only be used for a single static map Image - don't be tempted to override Canvas.paint() and use this to update the map dynamically - the amount of data traffic required is to do this is highly inefficient, and alternative solutions (as described in this question here) are available.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top