Google Maps in Codename One

1,120 views
Skip to first unread message

lonel...@gmail.com

unread,
Nov 22, 2013, 5:26:53 AM11/22/13
to codenameone...@googlegroups.com
Dear all,

I have tried to implement google maps in codename one by following the MapsDemo tutorial. But the result is google denied the request, I don't know why but maybe google reject the key, the problem is I have include my google maps key as an argument. Is there something wrong with my code or maybe anyone can give me a working example about implementing google maps in codename one? this is my code:


try
        {
            final Form map=new Form("Map");
            map.setLayout(new BorderLayout());
            map.setScrollable(false);
            final MapComponent mc=new MapComponent();
            Location loc=LocationManager.getLocationManager().getCurrentLocation();
            putMeOnMap(mc);
            map.addComponent(BorderLayout.CENTER, mc);
            Command back=new Command("Back"){
                public void actionPerformed(ActionEvent ev)
                {
                    c.getComponentForm().showBack();
                }
            };
            map.addCommand(back);
            map.setBackCommand(back);
           
            ConnectionRequest req=new ConnectionRequest(){
                protected void readResponse(InputStream input)throws IOException{
                    JSONParser p=new JSONParser();
                    Hashtable h=p.parse(new InputStreamReader(input));
                    String response=(String)h.get("status");
                    if(response.equals("REQUEST_DENIED"))
                    {
                        System.out.println("make sure to obtain a key from "
                                + "https://developers.google.com/maps/documentation/places/");
                        Dialog.show("Info", "make sure to obtain an application key from "
                                + "google places api's"
                                , "Ok", null);
                        return;
                    }
                   
                    final Vector v=(Vector)h.get("results");
                    Image im=Image.createImage("/red_pin.png");
                    PointsLayer pl=new PointsLayer();
                    pl.setPointIcon(im);
                    pl.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent evt)
                        {
                            PointLayer p=(PointLayer)evt.getSource();
                            System.out.println("pressed "+p);
                            Dialog.show("Details", ""+p.getName(), "Ok",null);
                        }
                    });
                   
                    for(int i=0;i<v.size();i++)
                    {
                        Hashtable entry=(Hashtable)v.elementAt(i);
                        Hashtable geo=(Hashtable)entry.get("geometry");
                        Hashtable loc=(Hashtable)geo.get("location");
                        Double lat=(Double)loc.get("lat");
                        Double lng=(Double)loc.get("lng");
                        PointLayer point=new PointLayer(new Coord(lat.doubleValue(),lng.doubleValue()),(String)entry.get("name"),null);
                        pl.addPoint(point);   
                    }
                   
                    mc.addLayer(pl);
                    map.show();
                    mc.zoomToLayers();
                }
            };
            req.setUrl("https://maps.googleapis.com/maps/api/place/search/json");
            req.setPost(false);
            req.addArgument("location", ""+loc.getLatitude()+","+loc.getLongitude());
            req.addArgument("radius", "500");
            req.addArgument("types", "food");
            req.addArgument("sensor", "false");
           
            String key="My Key";
            req.addArgument("key", key);
            NetworkManager.getInstance().addToQueue(req);
           
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }


Thanks for your help...

Best Regards

Gareth Murfin

unread,
Nov 22, 2013, 6:43:24 AM11/22/13
to codenameone...@googlegroups.com, lonel...@gmail.com
if(response.equals("REQUEST_DENIED"))
                    {
                        System.out.println("make sure to obtain a key from "
                                + "https://developers.google.com/maps/documentation/places/");
                        Dialog.show("Info", "make sure to obtain an application key from "
                                + "google places api's"
                                , "Ok", null);
                        return;
                    }
                    

this is the bit that checks the API key, what is printed out in your console do you see  "make sure to obtain an application key from google places api's" when you run your app? Or any error at all?

Shai Almog

unread,
Nov 22, 2013, 2:42:34 PM11/22/13
to codenameone...@googlegroups.com, lonel...@gmail.com
A google map works like this:
MapComponent mc = new MapComponent(new GoogleMapsProvider("... your key here"), currentMap.getCenter(), currentMap.getZoomLevel());

What you are failing on is the places query. I'm guessing it requires a different key from the Maps key.
You need to obtain a Map API key from Google.

lonel...@gmail.com

unread,
Nov 26, 2013, 10:42:30 PM11/26/13
to codenameone...@googlegroups.com, lonel...@gmail.com
Thanks for your reply,

I Have check my key and nothing wrong with that. I have tried your suggestion below but the respond is still same. make sure to obtain an application key from google places api's. Can you provide me with a working example about Google Maps? is there a relation between Google Maps API version with the code? Please help me with this, I have stuck here for days..

Best Regards

lonel...@gmail.com

unread,
Nov 26, 2013, 10:45:28 PM11/26/13
to codenameone...@googlegroups.com, lonel...@gmail.com
Thanks for your reply, the console write "make sure to obtain an application key from google places api's", but i am very sure there is nothing wrong with my google maps Key, can you help me or provide a working example about google maps in codename one?

Best Regards

Shai Almog

unread,
Nov 27, 2013, 1:38:00 AM11/27/13
to codenameone...@googlegroups.com, lonel...@gmail.com

fromyo...@gmail.com

unread,
Jul 25, 2014, 7:57:54 PM7/25/14
to codenameone...@googlegroups.com, lonel...@gmail.com


On Wednesday, November 27, 2013 2:38:00 PM UTC+8, Shai Almog wrote:


I'm facing the same problem

I created many keys for android but no keys are working

Shai Almog

unread,
Jul 26, 2014, 3:17:12 AM7/26/14
to codenameone...@googlegroups.com, lonel...@gmail.com, fromyo...@gmail.com

1)Make sure your google project has static maps enabled.

2)Make sure you haven't exceeded the limit, if so add your billing to the google project.


vannansoftwa...@gmail.com

unread,
Jul 26, 2014, 9:33:35 AM7/26/14
to codenameone...@googlegroups.com, lonel...@gmail.com, fromyo...@gmail.com
Hi


  I made has static maps enable then tried same message appeared
Are they any other API also should be enabled ? I enabled only two Places and Static maps ?

I have some more questions

1) When I try to run in Simulator while running in Net beans do I need to use Android Key or iOS Key generated ?
2) Do I have to set any thing in Build Xml file in Net beans ?
3) But without setting anything any in Build xml Codename one key is working fine in Simulator so is that created for Android or iOS ?
4) While using The Simulator the Simulator getting a fixed location as it is detected by GPS  is my assumption correct ?

Kindly Assist me.
Thanks for your reply.
S.A.Arulvannan
Screenshot from 2014-07-26 21:22:29.png

Shai Almog

unread,
Jul 26, 2014, 10:11:10 AM7/26/14
to codenameone...@googlegroups.com, lonel...@gmail.com, fromyo...@gmail.com, vannansoftwa...@gmail.com
Hi,
did you place the map in any particular way?
Do you do something special in your app?
Does it work for you with the Map demo or the native map demo?
The other questions are unrelated to this old thread that wasn't started by you. Please try not to revive old threads, I only answered because it seemed relevant but since it seems that what you are experiencing is completely different you should start a new thread.

vannansoftwa...@gmail.com

unread,
Jul 26, 2014, 10:42:15 AM7/26/14
to codenameone...@googlegroups.com, lonel...@gmail.com, fromyo...@gmail.com, vannansoftwa...@gmail.com
Hi

   Thanks , I asked other questions to understand the way it works clearly. I just enter places and set range then It will create new MapComponent instance then it will do a query as it does for the example in Codename one demo. Yep it is working fine with codename one Demo's Key. The question I had is Google says every Query is matched with the Key and Application number then it will respond , But in our came my Apps name is different it began with com.seaaav... for this app how query is working fine and google is responding fine I attached the Screen Shot which is queried by the GoogleAPI key which I found in MapDemo.
Screenshot from 2014-07-26 22:34:54.png

Shai Almog

unread,
Jul 27, 2014, 2:12:46 AM7/27/14
to codenameone...@googlegroups.com, lonel...@gmail.com, fromyo...@gmail.com, vannansoftwa...@gmail.com
Hi,
please don't use our key for production since we will invalidate it.
If this works with our key then your key/configuration has a problem.
Reply all
Reply to author
Forward
0 new messages