2010-11-30 18 views
0

私はGoogleマップ用のNokiaサンプルコードを使用しました。 Imageをロードして返すretrieveStaticImageメソッドがあります。ミッドレットイメージがimageItemに表示されないのはなぜですか?

のためのGoogleマップクラス

import java.io.ByteArrayOutputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Vector; 
import javax.microedition.io.Connector; 
import javax.microedition.io.HttpConnection; 
import javax.microedition.lcdui.Image; 
import javax.microedition.lcdui.ImageItem; 

public class GoogleMaps implements Runnable{ 
    private static final String URL_UNRESERVED = 
     "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + 
     "abcdefghijklmnopqrstuvwxyz" + 
     "-_.~"; 
    private static final char[] HEX = "ABCDEF".toCharArray(); 
    Image image; 
    int width; 
    int height; 
    double lat; 
    double lng; 
    int zoom; 
    String format; 
    ImageItem imagecmpt; 
    // these 2 properties will be used with map scrolling methods. You can remove them if not needed 
    public static final int offset = 268435456; 
    public static final double radius = offset/Math.PI; 

    private String apiKey = null; 

    public GoogleMaps(String key,int width, int height, double lat, double lng, int zoom, 
      String format,ImageItem img) { 
     this.width=width; 
     this.height=height; 
     this.lat=lat; 
     this.lng=lng; 
     this.zoom=zoom; 
this.format=format; 
     apiKey = key; 

    } 

    public double[] geocodeAddress(String address) throws Exception { 
     byte[] res = loadHttpFile(getGeocodeUrl(address)); 
     String[] data = split(new String(res), ','); 

     if (!data[0].equals("200")) { 
      int errorCode = Integer.parseInt(data[0]); 
      throw new Exception("Google Maps Exception: " + getGeocodeError(errorCode)); 
     } 

     return new double[] { 
       Double.parseDouble(data[2]), Double.parseDouble(data[3]) 
     }; 
    } 

    public Image retrieveStaticImage() throws IOException { 
     byte[] imageData = loadHttpFile(getMapUrl(width, height, lng, lat, zoom, format)); 
     System.out.println("Address is "+getMapUrl(width, height, lng, lat, zoom, format)); 
     for (int i=0;i<imageData.length;i++){ 
      System.out.println(imageData[i]); 
     } 
     return Image.createImage(imageData, 0, imageData.length); 
    } 

    private static String getGeocodeError(int errorCode) { 
     switch (errorCode) { 
     case 400: 
      return "Bad request"; 
     case 500: 
      return "Server error"; 
     case 601: 
      return "Missing query"; 
     case 602: 
      return "Unknown address"; 
     case 603: 
      return "Unavailable address"; 
     case 604: 
      return "Unknown directions"; 
     case 610: 
      return "Bad API key"; 
     case 620: 
      return "Too many queries"; 
     default: 
      return "Generic error"; 
     } 
    } 

    private String getGeocodeUrl(String address) { 
     return "http://maps.google.com/maps/geo?q=" + urlEncode(address) + "&output=csv&key=" 
       + apiKey; 
    } 

    private String getMapUrl(int width, int height, double lng, double lat, int zoom, String format) { 
     return "http://maps.google.com/staticmap?center=" + lat + "," + lng + "&format=" 
       + format + "&zoom=" + zoom + "&size=" + width + "x" + height + "&key=" + apiKey; 
    } 

    private static String urlEncode(String str) { 
     StringBuffer buf = new StringBuffer(); 
     byte[] bytes = null; 
     try { 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      DataOutputStream dos = new DataOutputStream(bos); 
      dos.writeUTF(str); 
      bytes = bos.toByteArray(); 
     } catch (IOException e) { 
      // ignore 
     } 
     for (int i = 2; i < bytes.length; i++) { 
      byte b = bytes[i]; 
      if (URL_UNRESERVED.indexOf(b) >= 0) { 
       buf.append((char) b); 
      } else { 
       buf.append('%').append(HEX[(b >> 4) & 0x0f]).append(HEX[b & 0x0f]); 
      } 
     } 
     return buf.toString(); 
    } 

    private static byte[] loadHttpFile(String url) throws IOException { 
     byte[] byteBuffer; 

     HttpConnection hc = (HttpConnection) Connector.open(url); 
     try { 
      hc.setRequestMethod(HttpConnection.GET); 
      InputStream is = hc.openInputStream(); 
      try { 
       int len = (int) hc.getLength(); 
       if (len > 0) { 
        byteBuffer = new byte[len]; 
        int done = 0; 
        while (done < len) { 
         done += is.read(byteBuffer, done, len - done); 
        } 
       } else { 
        ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
        byte[] buffer = new byte[512]; 
        int count; 
        while ((count = is.read(buffer)) >= 0) { 
         bos.write(buffer, 0, count); 
        } 
        byteBuffer = bos.toByteArray(); 
       } 
      } finally { 
       is.close(); 
      } 
     } finally { 
      hc.close(); 
     } 

     return byteBuffer; 
    } 

    private static String[] split(String s, int chr) { 
     Vector res = new Vector(); 

     int curr; 
     int prev = 0; 

     while ((curr = s.indexOf(chr, prev)) >= 0) { 
      res.addElement(s.substring(prev, curr)); 
      prev = curr + 1; 
     } 
     res.addElement(s.substring(prev)); 

     String[] splitted = new String[res.size()]; 
     res.copyInto(splitted); 

     return splitted; 
    } 

    public void run() { 
     try{ 
     //throw new UnsupportedOperationException("Not supported yet."); 
     imagecmpt.setImage(retrieveStaticImage()); 
     } 
     catch(Exception e){ 
      System.out.println("Error ......."+e); 
     } 
    } 
} 

コード

public void commandAction(Command command, Displayable displayable) {            
     // write pre-action user code here 
     if (displayable == form) {           
      if (command == exitCommand) {           
       // write pre-action user code here 
       exitMIDlet();           
       // write post-action user code here 
      } else if (command == okCommand) {           
       // write pre-action user code here 

       // write post-action user code here 

       GoogleMaps cMz=new GoogleMaps("ABQIAAAADEQoVqbqS5pT4ahHSLALyBT8PMUw5z7_OLJoE1lh2VQyfb-WOxTwS9t9mrSq_flhdPeVGOQrxXuCFQ",10, 10, 10, 10, 1, "roadmap", imageItem); 

       Thread th=new Thread(cMz); 
       th.run(); 
       Display display=getDisplay(); 


      }             
     }             
     // write post-action user code here 
    }  

私のため

コード...ここ は私のコードです..私彼らはにImageItemにイメージをロードしますが、私は毎回例外を取得します出力:::

Starting emulator in execution mode 
Running with storage root DefaultColorPhone 
Running with locale: English_United States.1252 
Running in the identified_third_party security domain 
Warning: To avoid potential deadlock, operations that may block, such as 
networking, should be performed in a different thread than the 
commandAction() handler. 

私は外部スレッドを使用しましたが、別のスレッドを使用する。 これをどのように並べ替えることができますか?

+0

をお試しください –

答えて

1

あなたは、また、前の回答の一部を受け入れるべき

new Thread(new Runnable(){ 
GoogleMaps cMz=new GoogleMaps("ABQIAAAADEQoVqbqS5pT4ahHSLALyBT8PMUw5z7_OLJoE1lh2VQyfb-WOxTwS9t9mrSq_flhdPeVGOQrxXuCFQ",10, 10, 10, 10, 1, "roadmap", imageItem); 


//put here action 

} 
).start(); 
関連する問題