creating a map to detect your current position

343 views
Skip to first unread message

chisom dike Onwuegbuzie

unread,
Oct 20, 2013, 1:12:42 PM10/20/13
to codenameone...@googlegroups.com
please someone should help me look into this code i edited it a little from the developers guide to match the form i created in gui builder.  please i need the help i can get please.


protected void beforeMap(Form f) {

try {
//get the current location from the Location API
Location loc = LocationManager.getLocationManager().getCurrentLocation();
Coord lastLocation = new Coord(loc.getLatitude(), loc.getLongtitude());

final InfiniteProgress progress = new InfiniteProgress();
final Dialog dlg =progress.showInifiniteBlocking();

final ConnectionRequest req = new ConnectionRequest() {
@Override
protected void readResponse(InputStream input) throws IOException {
JSONParser p = new JSONParser();
Hashtable h = p.parse(new InputStreamReader(input));
// "status" : "REQUEST_DENIED"
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");
Image img = Image.createImage("/blue_pin.png");

PointLayer point = new PointLayer(new Coord(lat.doubleValue(),
lng.doubleValue()),
(String) entry.get("name"), img);
point.setIcon(img);
point.setDisplayName(true);
pl.addPoint(point);
}

findMc().addLayer(pl);
findMc().zoomToLayers();
}
};
req.setUrl("https://maps.googleapis.com/maps/api/place/search/json");
req.setPost(false);
req.addArgument("location", "" + loc.getLatitude() + "," + loc.getLongtitude());
req.addArgument("radius", "500");
req.addArgument("types", "food");
req.addArgument("sensor", "false");
//get your own key from https://developers.google.com/maps/documentation/places/
//and replace it here.
String key = "yourAPIKey";
req.addArgument("key", key);
NetworkManager.getInstance().addToQueue(req);
}
catch (IOException ex) {
}
}

Shai Almog

unread,
Oct 20, 2013, 2:11:06 PM10/20/13
to codenameone...@googlegroups.com
What's the issue you are experiencing?
Notice that all the code in readResponse() is incorrect since that method is invoked on a separate thread. You should write GUI code only on the EDT and move all that code to postResponse(); You should also use find*() cautiously since the form might have changed, either pass in the form or check that the value isn't null.

Shai Almog

unread,
Oct 20, 2013, 2:12:28 PM10/20/13
to codenameone...@googlegroups.com
Oh and getLocation() is incorrect.
You need to bind a location listener and wait for a location to be available. You should search this form where we give ample examples of proper usage of the API.

ebelin...@agilastic.de

unread,
Oct 20, 2013, 4:01:38 PM10/20/13
to codenameone...@googlegroups.com
i.e. look at this snipped:

LocationManager l = LocationManager.getLocationManager(); 
l.setLocationListener(new LocationListener() {
                    public void locationUpdated(Location loc) {                        
                        if (Math.abs(loc.getLongitude())  > 0) {
                            Storage.getInstance().writeObject("Altitude", loc.getAltitude());
                            Storage.getInstance().writeObject("Latitude", loc.getLatitude());
                            Storage.getInstance().writeObject("Longitude", loc.getLongitude());

                            System.out.println(Storage.getInstance().readObject("Altitude").toString());
                            System.out.println(Storage.getInstance().readObject("Latitude").toString());
                            System.out.println(Storage.getInstance().readObject("Longitude").toString());
                            System.out.println("UPDATE ");
                        }


                    }

                    public void providerStateChanged(int newState) {
                    }
                }); 

chisom dike Onwuegbuzie

unread,
Oct 20, 2013, 4:49:41 PM10/20/13
to codenameone...@googlegroups.com
shai thank you for your response but im kind of new to this, please can you relay it to me in code?

chisom dike Onwuegbuzie

unread,
Oct 20, 2013, 4:51:07 PM10/20/13
to codenameone...@googlegroups.com, ebelin...@agilastic.de
thanks ebelin but still need more eye opener on this 

Shai Almog

unread,
Oct 21, 2013, 2:02:09 AM10/21/13
to codenameone...@googlegroups.com, ebelin...@agilastic.de
Please be more specific. I can't guess what you don't understand.

chisom dike Onwuegbuzie

unread,
Oct 21, 2013, 10:05:33 PM10/21/13
to codenameone...@googlegroups.com, ebelin...@agilastic.de


On Monday, October 21, 2013 7:02:09 AM UTC+1, Shai Almog wrote:
Please be more specific. I can't guess what you don't understand.

i dont fully understand the code that will get the current location of a dvice using google map

Shai Almog

unread,
Oct 22, 2013, 1:54:26 AM10/22/13
to codenameone...@googlegroups.com, ebelin...@agilastic.de
I've added a simplified method getCurrentLocationSync() to the LocationManager. It will be available in the next library update and should be easier to use.
You can search the forum for LocationManager to understand how to use this class.

ebelin...@agilastic.de

unread,
Oct 22, 2013, 2:33:25 AM10/22/13
to codenameone...@googlegroups.com
 Hey, 
take a look at my dirty sample code: 

You can also run the app on Android:

It's not perfect.... just my first app with CN1...

chisom dike Onwuegbuzie

unread,
Oct 26, 2013, 1:29:37 AM10/26/13
to codenameone...@googlegroups.com, ebelin...@agilastic.de
ok

kevin....@gmail.com

unread,
Oct 28, 2013, 1:48:34 PM10/28/13
to codenameone...@googlegroups.com, ebelin...@agilastic.de
Got the new library, and the getCurrentLocationSync() works as advertised.  I am a little confused about how I should be using it though.  When the gps in my phone is on, and I'm outdoors, it works as expected.  However, if I am indoors, and I call it, it loops infinitely, with the gps icon flashing in the status bar.  I have tried using the locationmanager flags of AVAILABLE and TEMP_UNAVAILABLE, and they don't really seem to make a difference.
What status is returned by the LocationManager if you are indoors and the gps is turned on?  What do I do to resort to WiFi geolocation if I can't get a GPS signal?

Shai Almog

unread,
Oct 29, 2013, 1:59:04 AM10/29/13
to codenameone...@googlegroups.com, ebelin...@agilastic.de, kevin....@gmail.com
Makes sense, we probably need to add a timeout option to the API.
I'll add it to the next update.

kevin....@gmail.com

unread,
Oct 29, 2013, 9:41:43 AM10/29/13
to codenameone...@googlegroups.com, ebelin...@agilastic.de, kevin....@gmail.com
If I may be so bold, an overridden function that can take a timeout as a parameter?

Does the LocationManager only use GPS?  Or does it also use WiFi location services?

Shai Almog

unread,
Oct 29, 2013, 3:09:59 PM10/29/13
to codenameone...@googlegroups.com, ebelin...@agilastic.de, kevin....@gmail.com
Overloaded method. getCurrentLocationSync(), getCurrentLocationSync(long).

I think it currently only uses GPS. We should migrate it to the new google location API's when we get the chance.

kevin....@gmail.com

unread,
Nov 18, 2013, 2:18:07 PM11/18/13
to codenameone...@googlegroups.com, ebelin...@agilastic.de, kevin....@gmail.com
I think my CN1 library is up-to-date in NetBeans, but the getCurrentLocationSync(long) function isn't there yet?  When will that update be released?

Shai Almog

unread,
Nov 19, 2013, 12:35:08 AM11/19/13
to codenameone...@googlegroups.com, ebelin...@agilastic.de, kevin....@gmail.com
I think it should be in the next update which we should release this week.

Akintayo Olusegun

unread,
Nov 19, 2013, 6:35:31 AM11/19/13
to codenameone...@googlegroups.com, ebelin...@agilastic.de, kevin....@gmail.com
Hi Shai,
Can you please reply this thread once this update is available. 

Thanks.

kevin....@gmail.com

unread,
Nov 19, 2013, 9:14:10 AM11/19/13
to codenameone...@googlegroups.com, ebelin...@agilastic.de, kevin....@gmail.com
Thanks for the update Shai, looking forward to it.
Reply all
Reply to author
Forward
0 new messages