MyGCanvas.java
资源名称:GMapsTest.rar [点击查看]
上传用户:hygd004
上传日期:2022-07-01
资源大小:246k
文件大小:3k
源码类别:
J2ME
开发平台:
Java
- import javax.microedition.lcdui.Canvas;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
- //Download by http://www.codefans.net
- import java.util.Vector;
- /**
- * @author funny
- *
- * 用以下URL求城市的经纬度,把经纬度放入函数retrieveStaticImage运算
- * http://maps.google.com/maps/geo?q=shanghai&output=csv&key=ABQIAAAAxXXppGwQikP5W03kMkXwTBRwZeWwBJuDeBDs7Xe14alLLfNA1RSipLkOi1Os9IQgWszFCqI6nbQUwA
- *
- * 用以下URL输出城市地图
- * http://maps.google.com/staticmap?center=31.2243531,121.4759159&format=png32&zoom=8&size=320x240&key=ABQIAAAAxXXppGwQikP5W03kMkXwTBRwZeWwBJuDeBDs7Xe14alLLfNA1RSipLkOi1Os9IQgWszFCqI6nbQUwA
- */
- public class MyGCanvas extends Canvas implements Runnable {
- private GoogleMaps gMap=null;
- private String apiKey="ABQIAAAAxXXppGwQikP5W03kMkXwTBRwZeWwBJuDeBDs7Xe14alLLfNA1RSipLkOi1Os9IQgWszFCqI6nbQUwA";
- //常州的经纬度
- private double lon=31.188179526512172;//31.7558405;
- private double lat=121.43672347068786;//119.9392140;
- //放大的倍数
- private int zoom=16;
- //调整位置的经纬度
- double[] cs;
- Image mapImg;
- Vector m_cmd;
- public MyGCanvas()
- {
- gMap=new GoogleMaps(apiKey);
- cs=new double[]{lon,lat};
- m_cmd = new Vector(1);
- new Thread(this).start();
- }
- protected void paint(Graphics g) {
- g.setColor(0xffffff);
- g.fillRect(0,0,getWidth(),getHeight());
- if(mapImg!=null)
- g.drawImage(mapImg,(getWidth()-256)/2,(getHeight()-256)/2,20);
- else
- {
- g.setColor(0);
- g.drawString("waiting.....", 40, 60, 20);
- }
- g.setColor(0xff0000);
- final int cs = 3; // crosshair size
- g.drawLine(getWidth()/2 - cs, getHeight()/2, getWidth()/2 + cs, getHeight()/2);
- g.drawLine(getWidth()/2, getHeight()/2 - cs, getWidth()/2, getHeight()/2 + cs);
- }
- protected synchronized void getURL()
- {
- m_cmd.addElement(new Object());
- notify();
- }
- public void run()
- {
- while(true)
- {
- if(m_cmd.size()==0)
- {
- try
- {
- synchronized(this)
- {
- wait();
- }
- }catch (InterruptedException e){}
- }
- try
- {
- mapImg=gMap.retrieveStaticImage(256,256, lon,lat, zoom, "png");
- m_cmd.removeElementAt(0);
- repaint();
- } catch (Exception e)
- {
- }
- }
- }
- protected void keyPressed(int keycode) {
- /*System.out.println(keycode);
- flag=true;*/
- //switch (this.getGameAction(keycode)) {
- String actionName=this.getKeyName(keycode);
- int action=this.getGameAction(keycode);
- if(action==Canvas.UP){
- lat=MapUtil.adjust(lat, lon, 0, -30, zoom)[0];
- lon=MapUtil.adjust(lat, lon, 0, -30, zoom)[1];
- }
- else if(action==Canvas.DOWN){
- lat=MapUtil.adjust(lat, lon, 0, 30, zoom)[0];
- lon=MapUtil.adjust(lat, lon, 0, 30, zoom)[1];
- }
- else if(action==Canvas.LEFT){
- lat=MapUtil.adjust(lat, lon, -30, 0, zoom)[0];
- lon=MapUtil.adjust(lat, lon, -30, 0, zoom)[1];
- }
- else if(action==Canvas.RIGHT){
- lat=MapUtil.adjust(lat, lon, 30, 0, zoom)[0];
- lon=MapUtil.adjust(lat, lon, 30, 0, zoom)[1];
- }
- else if(actionName.equals("1")||actionName.equals("*")){
- if(zoom<17)
- zoom++;
- }
- else if(actionName.equals("3")||actionName.equals("#")){
- if(zoom>1)
- zoom--;
- }
- //lat=cs[0];
- //lon=cs[1];
- //cs=MapUtil.adjust(lat, lon, 30, 0, zoom--);
- getURL();
- }
- }