Hi guys, I'm having little trouble with getting current location point.
Portion of my code:
locationDisplay.setAutoPanMode(LocationDisplay.AutoPanMode.RECENTER); locationDisplay.addLocationChangedListener(new LocationDisplay.LocationChangedListener() { @Override public void onLocationChanged(LocationDisplay.LocationChangedEvent locationChangedEvent) { // Log.i(TAG, "HERE is the Location " + Double.toString(locationChangedEvent.getLocation().getPosition().getY()) + " " // + Double.toString(locationChangedEvent.getLocation().getPosition().getX()) // ); Latitude = Double.toString(locationChangedEvent.getLocation().getPosition().getY()); Longitude = Double.toString(locationChangedEvent.getLocation().getPosition().getX()); Log.i(TAG, Latitude + " *** " + Longitude); } }); if(!locationDisplay.isStarted()) locationDisplay.startAsync(); //this line prints null Log.i(TAG,"LOCATION IS STARTED!" + Latitude + " " + Longitude);
The problem I having is that, I'm trying get the current location points. When I run the app, only this statement prints the data
Log.i(TAG, "HERE is the Location " + Double.toString(locationChangedEvent.getLocation().getPosition().getY()) + " " + Double.toString(locationChangedEvent.getLocation().getPosition().getX()) );
but,
Log.i(TAG,"LOCATION IS STARTED!" + Latitude + " " + Longitude);
this prints null on both variable. Since I have assigned the Longitude and Latitude variable on the OnLocationChanged method, why does this prints null. Is there any way I can fix this problem.
Output:
02-15 23:35:10.357 23095-23095/com.example.moham.esrimap I/progress: LOCATION IS STARTED!null null
02-15 23:35:12.314 23095-23095/? I/progress: 38.9808961 *** -125.3387306
02-15 23:35:12.315 23095-23095/? I/progress: 38.9808961 *** -125.3387306
Thanks!
Solved! Go to Solution.
Hi Yosuf,
I think the best way to ensure that you have a non-null location is to acquire it in the addLocationListener() function. For example, below I have a function called assigLocation(Point point) which I call from within the listener. You could then assign your Latitude and Longitude variables there.
Hope this helps,
Sandie
@Override protected void onResume() { super.onResume(); if (mMapView != null){ mMapView.resume(); final String longitude = null; mLocationDisplay = mMapView.getLocationDisplay(); mLocationDisplay.addLocationChangedListener(new LocationDisplay.LocationChangedListener() { @Override public void onLocationChanged(LocationDisplay.LocationChangedEvent locationChangedEvent) { LocationDataSource.Location location = mLocationDisplay.getLocation(); if (location != null) { Point point = location.getPosition(); assignLocation (point); } } }); if(!mLocationDisplay.isStarted()) mLocationDisplay.startAsync(); } } private void assignLocation(Point point) { Point myLocation = point; Log.i("MainActivity", "My device location is " + point.getX() + " " + point.getY()); // You could also assign your Longitude and Latitude values here Longitude = point.getX(); Latitude = point.getY(); }
Hi Mohamed,
Because your statement
Log.i(TAG, "HERE is the Location " + Double.toString(locationChangedEvent.getLocation().getPosition().getY()) + " " + Double.toString(locationChangedEvent.getLocation().getPosition().getX()) );
prints out the correct latitude and longitude, is there perhaps an problem with casting the double value to your Latitude and Longitude? Can you provide the model object definition for your Longitude and Latitude objects?
Thanks,
Sandie Peters
Hi Peters, thanks for your suggestion. I haven't got any errors in casting. But ill look forward into it. Thanks
Hi Mohamed,
More thoughts on this...the locationDisplay.startAsync() is an async process which hasn't completed by the time you print the log statement:
Log.i(TAG,"LOCATION IS STARTED!" + Latitude + " " + Longitude);
The Latitude and Longitude variables haven't yet been assigned.
Hopefully this helps,
Sandie
Hi peters, i'll check out this.. Do you think i will work if I did like this
if(locationDisplay.isStarted()) Log.i(TAG,"LOCATION IS STARTED!" + Latitude + " " + Longitude);
thanks.
Hi Yosuf,
I think the best way to ensure that you have a non-null location is to acquire it in the addLocationListener() function. For example, below I have a function called assigLocation(Point point) which I call from within the listener. You could then assign your Latitude and Longitude variables there.
Hope this helps,
Sandie
@Override protected void onResume() { super.onResume(); if (mMapView != null){ mMapView.resume(); final String longitude = null; mLocationDisplay = mMapView.getLocationDisplay(); mLocationDisplay.addLocationChangedListener(new LocationDisplay.LocationChangedListener() { @Override public void onLocationChanged(LocationDisplay.LocationChangedEvent locationChangedEvent) { LocationDataSource.Location location = mLocationDisplay.getLocation(); if (location != null) { Point point = location.getPosition(); assignLocation (point); } } }); if(!mLocationDisplay.isStarted()) mLocationDisplay.startAsync(); } } private void assignLocation(Point point) { Point myLocation = point; Log.i("MainActivity", "My device location is " + point.getX() + " " + point.getY()); // You could also assign your Longitude and Latitude values here Longitude = point.getX(); Latitude = point.getY(); }
Thank you soo much Sandie for your time to help me. I really appreciate it. I'm new to arcGis, thats y I had question. Thanks