2013-04-23 67 views
0

我在J2ME訪問谷歌地圖通過以下網址:顯示片段在谷歌地圖上J2ME應用

字符串URL = 「http://maps.google.com/maps/api/staticmap?center= 」+緯度+「,」 +經度+」 &變焦= 「+變焦+」 &大小= 「+ width +」x「+ height +」& maptype =路線圖「+」& markers = color:red | label | A |「 + lat +「,」+ lon +「& sensor = true」;

我想使用此URL顯示片段。

請讓我知道將代碼段代碼放在網址中的位置以及代碼是什麼?

親切的問候,

Parmanand

回答

0

你可以使用這樣的代碼下載圖像:

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

Image可以顯示在一個FormImageItem例如:

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

順便說一下,上面的代碼片段應該只用於單一的靜態地圖圖片 - 不要試圖覆蓋Canvas.paint()並使用它來動態更新地圖 - 所需的數據流量的數量是highly inefficient,以及其他解決方案(如此問題here中所述)。