×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Regular Contributor
    Join Date
    Jul 2008
    Posts
    97

    problem in zoom-in and zoom-out with map?

    Hi All,
    I am trying to put map from address of location fetching from google geocode and maps. I have added the zoom-in zoom-out with the map so that when user selects one command the map zooms. The default size of map is taken as 6 in my case. the zoom-in feature works ok but when i choose zoom-out with the options the map did not load and takes time...can anyone plz tell me what is wrong with this code...I have followed the following link to get maps and add zooming to maps..
    http://wiki.forum.nokia.com/index.ph...oogle_Maps_API

    below is my code:
    Code:
    public class ShowMap extends MIDlet implements CommandListener {
        
        private Display myDisplay;
        private Form mainScreen;
        private TextField addressTextField;
        private Form mapScreen;
        private Image logoImg;
        private Command searchCommand, exitCommand, backCommand,  zoomInCmd, zoomOutCmd;
        private String addr;
        public static int zoomSize;
        public void ShowMap() {
            mainScreen = new Form("Google Map Search");
            addr = null;
            zoomSize = 6;
            searchCommand = new Command("Search", Command.OK, 1);
            exitCommand = new Command("Exit", Command.EXIT, 2);
            backCommand = new Command("Back", Command.OK, 1);
            zoomInCmd = new Command("Zoom In", Command.OK, 1);
            zoomOutCmd = new Command("Zoom Out", Command.OK, 1);
            mapScreen = new Form("Google Map");
            
            addressTextField = new TextField("Enter Address", "", 255, TextField.ANY);
            try {
                logoImg = Image.createImage("/lb1.png");
            }catch (Exception  e){
                System.out.println(e.getMessage());
            }
            myDisplay = Display.getDisplay(this);
            mainScreen = new Form("Google Map Search");
            try{mainScreen.append(logoImg);}catch(Exception e){}
            addressTextField = new TextField("Enter Address", "", 255, TextField.ANY);
            
            mainScreen.append(addressTextField);
           
            mainScreen.addCommand(searchCommand);
            mainScreen.addCommand(exitCommand);
            mainScreen.setCommandListener(this);
        }
        public void startApp() {
            ShowMap();
            myDisplay.setCurrent(mainScreen);
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
            myDisplay = null;
            mainScreen = null;
            addressTextField = null;
            mapScreen = null;
        }
        public void commandAction(Command cmd, Displayable disp) {
            if (cmd == searchCommand) {
                addr = addressTextField.getString();
                Image mapImage = null;
                if (0 != (addr.trim()).length()) {
                    try {mapImage = getMapImage (addr, zoomSize);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }else {
                    
                }
                mapScreen.append(mapImage);
                mapScreen.addCommand(zoomInCmd);
                mapScreen.addCommand(zoomOutCmd);
                mapScreen.addCommand(backCommand);
                mapScreen.setCommandListener(this);
                myDisplay.setCurrent(mapScreen);       
            }else if (cmd == zoomInCmd) {
                zoomSize =+ 1;
                Image mapImage;
                mapImage = null;
                Ticker loadMsg = new Ticker ("Loading Zoom-in Map");
                mapScreen.deleteAll();
                mapScreen.setTicker(loadMsg);
                mapImage = getMapImage (addr, zoomSize);
                mapScreen.append(mapImage);
                mapScreen.setTicker(null);
                loadMsg = null;
                mapScreen.setCommandListener(this);
                myDisplay.setCurrent(mapScreen);            
            }else if (cmd == zoomOutCmd) {
                zoomSize =- 1;
                Image mapImage = null;
                mapImage = null;
                Ticker loadMsg = new Ticker ("Loading Zoom-out Map");
                mapScreen.deleteAll();
                mapImage = getMapImage (addr, zoomSize);
                mapScreen.setTicker(loadMsg);
                mapScreen.append(mapImage);
                mapScreen.setTicker(null);
                loadMsg = null;
                mapScreen.setCommandListener(this);
                myDisplay.setCurrent(mapScreen);
            }
            else if (cmd == backCommand) {
                addressTextField.setString("");
                myDisplay.setCurrent(mainScreen);
            }else if (cmd == exitCommand) {
                destroyApp(true);
                notifyDestroyed();
            }
        }
        public Image getMapImage(String address, int Zoom) {
            Image map = null;
            double[] newLatLong = null;
            try {
                GMap gMap = new GMap("My_MAP_API_KEY");
                double[] latLng = gMap.geocodeAddress(address);
                if (Zoom > 6 && Zoom < 14) {
                    newLatLong = gMap.adjust(latLng[0], latLng[1], 15, 20, Zoom);
                    map = gMap.retrieveStaticImage(240, 291, newLatLong[0], newLatLong[1], Zoom, "png32");
                }else if (Zoom > 14 || Zoom < 6) {
                    Zoom = 8;
                    map = gMap.retrieveStaticImage(240, 291, latLng[0], latLng[1], Zoom, "png32");
                }else {
                    map = gMap.retrieveStaticImage(240, 291, latLng[0], latLng[1], Zoom, "png32");
                }
         }catch (Exception ex) {
                ex.printStackTrace();
            }
            return map;
         }
    }
    .....................GMap.java......................
    import net.dclausen.microfloat.MicroDouble;
     
    public class GMap
    {
    	String apiKey = null;
     
    	//these 2 properties will be used with map scrolling methods. You can remove them if not needed
    	int offset = 268435456;
    	double radius = offset / Math.PI;
    	
    	public GMap(String apiKey)
    	{
    		this.apiKey = apiKey;
    	}
    	
    	public double[] geocodeAddress(String address) throws Exception
    	{
    		byte[] res = loadHttpFile(getGeocodeUrl(address));
    		
    		String resString = new String(res, 0, res.length);
    		
    		String[] data = split(resString, ',');
    		
    		if(data[0].compareTo("200") != 0)
    		{
    			int errorCode = Integer.parseInt(data[0]);
    			
    			throw new Exception("Google Maps Exception: " + getGeocodeError(errorCode));
    		}
    		else
    		{
    			return new double[]{
    				Double.parseDouble(data[2]),
    				Double.parseDouble(data[3])
    			};
    		}
    	}
    	public Image retrieveStaticImage(int width, int height, double lat, double lng, int zoom, String format) throws Exception
    	{
    		byte[] imageData = loadHttpFile(getMapUrl(width, height, lng, lat, zoom, format));
    		
    		return Image.createImage(imageData, 0, imageData.length);
    	}
    	
    	String getGeocodeError(int errorCode)
    	{
    		switch(errorCode)
    		{
    		case 400:
    			return "Bad request";
    		*************code removed*******
    		default:
    			return "Generic error";
    		}
    	}
    	
    	String getGeocodeUrl(String address)
    	{
    		return "http://maps.google.com/maps/geo?q=" + urlEncode(address) 
    			+ "&output=csv&key=" + apiKey;
    	}
    	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 + "&maptype=mobile" + "&key=" + apiKey;
    	}
    	String urlEncode(String str)
    	{
    		StringBuffer buf = new StringBuffer();
    		char c;
    		for(int i = 0; i < str.length(); i++)
    		{ 
    			c = str.charAt(i);
    			if ((c >= '0' && c <= '9')||
    				(c >= 'A' && c <= 'Z')||
    				(c >= 'a' && c <= 'z'))
    			{
    				buf.append(c);
    			}
    			else
    			{ buf.append("%").append(Integer.toHexString((int) str.charAt(i)));
    			}
    		} 
    		return buf.toString();
    	}
    	byte[] loadHttpFile(String url) throws Exception
    	{
    		HttpConnection hc = null;		
    		InputStream is = null;                    
    		byte[] byteBuffer = null;
    		try
    		{          
    		hc = (HttpConnection) Connector.open(url, Connector.READ);
    		hc.setRequestMethod(HttpConnection.GET);
    		int ch;
    		is = hc.openInputStream();
    		int len = (int)hc.getLength();
    		if(len > 0)
    		{    byteBuffer = new byte[len];
       		     is.read(byteBuffer);
    		}else{
    		    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    		while ((ch = is.read()) != -1){
                        bos.write(ch);
    		}
    		byteBuffer = bos.toByteArray();
    		bos.close();
    	    }
    	}
    	catch(Exception e)
    	{    e.printStackTrace();}
    	finally
    	{
    	try
    	{ if(is != null) {
              is.close();
            }
    	if(hc != null) {
             hc.close();
            }
    	}
    	catch(Exception e2)
    	{e2.printStackTrace();}
    	}
    	return byteBuffer;
    	}
    	static String[] split(String s, int chr)
    	{
    	Vector res = new Vector();		
    		int curr = 0;
    		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 double[] adjust(double lat, double lng, int deltaX, int deltaY, int z)
            {
                return new double[]{
    		XToL(LToX(lat) + (deltaX<<(21-z))),
    		YToL(LToY(lng) + (deltaY<<(21-z)))
                };
            }
            
    }
    plz review the above code and tell me how can improve this code to add zooming features...

    thanks a lot for the valuable replies.
    sanjay

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    Istanbul
    Posts
    22

    Re: problem in zoom-in and zoom-out with map?

    did you find it please help me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×