Android - EBADF (Bad file number) OnClickInfoMarker - java

I have searched related issues but can't find it. I create an InfoWindowMarker which shows Picture, Name and Address. Then i create OnInfoWindowClickListener that will showing Latitude and Longitude of selected Marker. But when i click the Info Window i got this error message.
Logcat:
com.xxxxxxxx.xxxxxxxxxx E/Zygote﹕ Zygote: error closing descriptor
libcore.io.ErrnoException: close failed: EBADF (Bad file number)
at libcore.io.Posix.close(Native Method)
at libcore.io.BlockGuardOs.close(BlockGuardOs.java:75)
at com.android.internal.os.ZygoteInit.closeServerSocket(ZygoteInit.java:221)
at com.android.internal.os.ZygoteConnection.handleChildProc(ZygoteConnection.java:879)
at com.android.internal.os.ZygoteConnection.runOnce(ZygoteConnection.java:242)
at com.android.internal.os.ZygoteInit.runSelectLoop(ZygoteInit.java:713)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:649)
at dalvik.system.NativeStart.main(Native Method)
InfoWindowAdapter:
public GoogleMap.InfoWindowAdapter CustomMarkerInfo = new GoogleMap.InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
ViewGroup viewGroup = (ViewGroup)getLayoutInflater().inflate(R.layout.custom_info_window, null);
ImageView ivRow = (ImageView)viewGroup.findViewById(R.id.ivRowImage);
TextView locationName = (TextView)viewGroup.findViewById(R.id.tvRowTitle);
TextView locationAddress = (TextView)viewGroup.findViewById(R.id.tvRowTitle2);
Bitmap locationImage;
locationName.setText(marker.getTitle());
locationAddress.setText(marker.getSnippet());
URL imageURL;
try {
imageURL = new URL(markerInfo.get(marker.getId()));
locationImage = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());
ivRow.setImageBitmap(locationImage);
}catch (Exception e){
e.printStackTrace();
}
return viewGroup;
}
#Override
public View getInfoContents(Marker marker) {return null;}
};
OnInfoWindowClickListener:
private GoogleMap.OnInfoWindowClickListener onInfoWindowClickListener = new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
LatLng myLatLng = myLatLng();
Toast.makeText(getApplicationContext(),myLatLng.toString(),Toast.LENGTH_LONG).show();
}
};
private final LocationListener locationListener = new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override
public void onProviderEnabled(String provider) {}
#Override
public void onProviderDisabled(String provider) {}
#Override
public void onLocationChanged(Location newLocation) {
}
};
private LatLng myLatLng(){
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider;
Location location;
LatLng myLatLng = null;
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
provider = LocationManager.GPS_PROVIDER;
Toast.makeText(getApplicationContext(),"GPS tidak aktif.",Toast.LENGTH_LONG).show();
}else if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
provider = LocationManager.NETWORK_PROVIDER;
}else{
provider = LocationManager.PASSIVE_PROVIDER;
}
if(locationManager.getLastKnownLocation(provider) == null){
locationManager.requestLocationUpdates(provider,2000,10,locationListener);
location = locationManager.getLastKnownLocation(provider);
if(location == null){
Toast.makeText(getApplicationContext(),"Tidak dapat menemukan lokasi saat ini, coba lagi.",Toast.LENGTH_LONG).show();
}else{
location = locationManager.getLastKnownLocation(provider);
myLatLng = new LatLng(location.getLatitude(),location.getLongitude());
}
}else{
location = locationManager.getLastKnownLocation(provider);
myLatLng = new LatLng(location.getLatitude(),location.getLongitude());
}
return myLatLng == null ? myLatLng() : myLatLng;
}
Thanks for any solution.
Update: Markers created from loaded JSON. If it is could causing this issue.

This might be a threading issue. Your viewGroup is returned before your image is load from the URL. An easy way to solve the problem is to use the third party image loading library Picasso.
Sample code:
ImageView imageView = (ImageView)myContentsView.findViewById(R.id.ivRowImage);
if (not_first_time_showing_info_window) {
Picasso.with(getApplicationContext()).load("YOUR_IMG_URL").into(imageView);
} else { // if it's the first time, load the image with the callback set
not_first_time_showing_info_window=true;
Picasso.with(getApplicationContext()).load("YOUR_IMG_URL").into(imageView,new InfoWindowRefresher(marker));
}
And then you can have a private helper method to make sure the asynchronous shown in the first click:
private class InfoWindowRefresher implements Callback {
private Marker markerToRefresh;
private InfoWindowRefresher(Marker markerToRefresh) {
this.markerToRefresh = markerToRefresh;
}
#Override
public void onSuccess() {
markerToRefresh.showInfoWindow();
}
#Override
public void onError() {}
}
You can refer to this GitHub page for complete implementation: https://github.com/jbj88817/GoogleMap-InfoWindow-android/blob/master/app/src/main/java/com/bjiang/map_ex/MainActivity.java
If you really want to do it with ImageLoader, you can check out this post: http://androidfreakers.blogspot.com/2013/08/display-custom-info-window-with.html
(But it is more complicated compared to using Picasso to load from URL)

Related

Am I missing something on retrieving GPS Current Location?

I recently began developing an app, and I've spent the last few days working on the GPS part to obtain the user's address.
The issue is that if I run the app in Android Studio Emulator and change the address on the map, it appears correctly on a Toast and in the String variable I created to store the address, as shown in the screenshot below, but if I build the APK and run it on a Smartphone, the Toast does not appear and the String variable remains empty.
Is there something in my code that I'm forgetting? I'll paste the code for someone to review below.
public class FragmentT1_Agora extends Fragment {
View view;
LocationManager locationManager;
String moradaAtual; // Current Address
private Button btEnviar;
public FragmentT1_Agora() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_t1_agora, container, false);
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 100);
}
locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 3, mLocationListener);
return view;
}
private final LocationListener mLocationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
Toast.makeText(getContext(), "" + location.getLatitude() + "," + location.getLongitude(), Toast.LENGTH_LONG).show();
try {
Geocoder geocoder = new Geocoder(getContext(), Locale.getDefault());
List<Address> moradas = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
moradaAtual = moradas.get(0).getAddressLine(0);
Toast.makeText(getContext(), "Morada: " + moradaAtual, Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
};
}
I've left out the code that isn't related to the GPS.
Screenshot:

User's location not correct

I'm trying to get the user's current location for my weather application but i get the location of some "US" area. I've tried multiples tutorials but failed to get the correct current location.
It returns me:
latitude: 37.421998333333335
longitude: -122.08400000000002
The location is "Mountain View,CA,US"
The code I've used is below:
GpsTracker.java
public class GpsTracker implements LocationListener {
Context context;
public GpsTracker(Context c)
{
context = c;
}
public Location getLocation()
{
if(ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
Toast.makeText(context,"Permission not granted",Toast.LENGTH_SHORT).show();
return null;
}
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean isGpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(isGpsEnabled)
{
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);
Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
return l;
}
else{
Toast.makeText(context,"Enable GPS",Toast.LENGTH_SHORT).show();
}
return null;
}
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
}
In my main class I'm getting the location like this:
public void getLocation()
{
gpsTracker = new GpsTracker(getApplicationContext());
Location l = gpsTracker.getLocation();
if(l != null)
{
lat = l.getLatitude();
lon = l.getLongitude();
}
Log.d("User's location","Latitude"+lat);
Log.d("User's location","Longitude"+lon);
}
The Location that you need is returned in the method onLocationChanged.
So you could assign it to a variable and then return it:
private Location lastLocation;
#Override
public void onLocationChanged(Location location) {
lastLocation = location;
}
public Location getLastLocation() {
return lastLocation;
}
Call requestLocationUpdates in the onCreate of your activity and then call getLastLocation when you need to show it.
Or you can implement a callback to notify the activity when the location changes.
You are probably getting the last known location and using that tool is probably handing back this address. Consider using a better tool or just FusedLocationProviderClient. However, I like SmartLocation library personally.
https://github.com/mrmans0n/smart-location-lib
It gives fallback providers in the event of one unavailable. There is a little learning curve to use the library, but once you implement it, it works nicely.

Google map object showing NPE on older devices

I am using this piece of code to initialize the google map object:
googleMap = ( (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
It works fine on newer devices , but shows NPE on older devices.Please help!!
This is how I solved the NPE of google maps.
public class WBMapFragment extends LogoBaseFragment implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {
private GoogleMap map;
private MapView mapView;
#Inject
private MapManager mapManager;
/*
* Define a request code to send to Google Play services This code is
* returned in Activity.onActivityResult
*/
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private LocationClient mLocationClient;
#Inject
private IUserLocationProvider userLocation;
private BitmapDescriptor mapPin;
private BitmapDescriptor mapPinUsed;
private SupportMapFragment supportMapFragment;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true); //< -- very important line
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//I needed this for when I return to the fragment that contained the map
getChildFragmentManager().putFragment(outState, "supportFragment", supportMapFragment);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_map, null);
if (savedInstanceState == null) {
if (supportMapFragment == null) {
supportMapFragment = SupportMapFragment.newInstance();
getChildFragmentManager().beginTransaction().add(R.id.mapCont, supportMapFragment,SupportMapFragment.class.getSimpleName()).commit();
}
}
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState !=null){
supportMapFragment = (SupportMapFragment) getChildFragmentManager().getFragment(savedInstanceState, "supportFragment");
map = null; //<very important to nullify the map, because the new instance of the map will not be the same
}
mLocationClient = new LocationClient(getActivity(), this, this);
}
/*
* Called when the Activity becomes visible.
*/
#Override
public void onStart() {
super.onStart();
// Connect the client.
if (isGooglePlayServicesAvailable()) {
mLocationClient.connect();
initMap();
}
}
public void initMap() {
if (map == null) {
map = (GoogleMap) supportMapFragment.getMap();
if (map == null) {
//this call is made several times, and on first tried map can be null
return;
}
}
map.getUiSettings().setCompassEnabled(false);
map.getUiSettings().setZoomControlsEnabled(false);
map.setOnMyLocationChangeListener(new OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
if (location != null) {
userLocation.saveLocation(location);
}
}
});
}
#Override
public void onResume() {
super.onResume();
initMap();
updateAnnotations();
}
/*
* Called when the Activity is no longer visible.
*/
#Override
public void onStop() {
// Disconnecting the client invalidates it.
mLocationClient.disconnect();
super.onStop();
}
/*
* Handle results returned to the FragmentActivity by Google Play services
*/
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Decide what to do based on the original request code
switch (requestCode) {
case CONNECTION_FAILURE_RESOLUTION_REQUEST:
/*
* If the result code is Activity.RESULT_OK, try to connect again
*/
switch (resultCode) {
case Activity.RESULT_OK:
mLocationClient.connect();
break;
}
}
}
private boolean isGooglePlayServicesAvailable() {
// Check that Google Play services is available
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
// If Google Play services is available
if (ConnectionResult.SUCCESS == resultCode) {
// In debug mode, log the status
Log.d("Location Updates", "Google Play services is available.");
MapsInitializer.initialize(getActivity().getApplicationContext());
mapPin = BitmapDescriptorFactory.fromResource(R.drawable.envelope);
mapPinUsed = BitmapDescriptorFactory.fromResource(R.drawable.envelope_open);
return true;
} else {
//here google suggests to display an error dialog, but i found that this only cause problems , better show a toast instead
// Get the error dialog from Google Play services
// Dialog errorDialog =
// GooglePlayServicesUtil.getErrorDialog(resultCode, getActivity(),
// CONNECTION_FAILURE_RESOLUTION_REQUEST);
// If Google Play services can provide an error dialog
// if (errorDialog != null) {
// Create a new DialogFragment for the error dialog
// ErrorDialogFragment errorFragment = new ErrorDialogFragment();
// errorFragment.setDialog(errorDialog);
// errorFragment.show(getActivity().getSupportFragmentManager(),
// "Location Updates");
// }
return false;
}
}
/*
* Called by Location Services when the request to connect the client
* finishes successfully. At this point, you can request the current
* location or start periodic updates
*/
#Override
public void onConnected(Bundle dataBundle) {
// Display the connection status
Location location = mLocationClient.getLastLocation();
if (location != null) {
updateAnnotations();
} else {
// makeText( "Current location was null, enable GPS on emulator!",
// Toast.LENGTH_SHORT);
}
}
private Marker addMarker(GoogleMap map, double lat, double lon, String title, String snippet, boolean isWon) {
MarkerOptions options = new MarkerOptions().position(new LatLng(lat, lon)).title(title).snippet(snippet);
if (isWon) {
options.icon(mapPinUsed);
} else {
options.icon(mapPin);
}
Marker marker = map.addMarker(options);
return marker;
}
private void addMarkerToMap(GoogleMap map, WBMarker beacon) {
addMarker(map, beacon.location.Lat, beacon.location.Lng, beacon.name, beacon.address, beacon.isWon);
}
private void updateAnnotations() {
mapManager.getBeaconData(new IBeaconDataReceivedListener() {
#Override
public void onBeaconDataReceived(List<WBMarker> data) {
if (data.size() > 0) {
map.clear();
}
Location loc = userLocation.getLocation();
LatLng userTarget = new LatLng(loc.getLatitude(), loc.getLongitude());
LatLngBounds.Builder builder = new LatLngBounds.Builder();
List<WBMarker> ret = closeAnnotations(data);
for (WBMarker beacon : data) {
addMarkerToMap(map, beacon);
}
builder.include(userTarget);
CameraUpdate update;
if (ret.size() == 0) {
update = CameraUpdateFactory.newCameraPosition(new CameraPosition(userTarget, 17, 0, loc.getBearing()));
} else {
for (WBMarker beacon : ret) {
builder.include(new LatLng(beacon.location.Lat, beacon.location.Lng));
}
update = CameraUpdateFactory.newLatLngBounds(builder.build(), 50);
}
map.animateCamera(update);
}
});
}
private List<WBMarker> closeAnnotations(List<WBMarker> data) {
Location userLoc = userLocation.getLocation();
List<WBMarker> ret = new ArrayList<WBMarker>();
for (WBMarker beacon : data) {
Location loc = beacon.location.getLocation();
float dist = loc.distanceTo(userLoc);
if (dist < 50000.0f) {
ret.add(beacon);
}
}
return ret;
}
/*
* Called by Location Services if the connection to the location client
* drops because of an error.
*/
#Override
public void onDisconnected() {
// Display the connection status
Toast.makeText(getActivity(),"Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show();
}
/*
* Called by Location Services if the attempt to Location Services fails.
*/
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
/*
* Google Play services can resolve some errors it detects. If the error
* has a resolution, try sending an Intent to start a Google Play
* services activity that can resolve error.
*/
if (connectionResult.hasResolution()) {
try {
// Start an Activity that tries to resolve the error
connectionResult.startResolutionForResult(getActivity(), CONNECTION_FAILURE_RESOLUTION_REQUEST);
/*
* Thrown if Google Play services canceled the original
* PendingIntent
*/
} catch (IntentSender.SendIntentException e) {
// Log the error
e.printStackTrace();
}
} else {
Toast.makeText(getActivity(), "Sorry. Location services not available to you", Toast.LENGTH_LONG).show();
}
}
}
Old answer:
I also ran into this problem. you should make this call in onResume only! as the map may not be ready before then
You should also call
MapsInitializer.initialize(getActivity().getApplicationContext());
and
GooglePlayUtils.isGooglePlayServicesAvailable(context)
before calling for the map

Google Maps v2 my location with Google Play

I would like to ask about a strange situation, that happened during usage of Google Maps API v2.
There is an error in logcat that says:
The Google Play services resources were not found. Check your project
configuration to ensure that the resources are included.
The things that I have:
Google Map is displayed perfectly along with markers - I have acquired relevant code from Google.
google-play-services.jar library is included perfectly with Eclipse
(project->properties->android->add...).
The checkboxes in Eclipse (project->properties->java build path->order build path) are all checked properly.
The code
GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
returns true.
This code is running at a device, Nexus 4, not emulator.
I am trying to invoke event, that would allow me to get current position by this class:
public class FindMyLocationManager implements LocationListener, LocationSource
{
private OnLocationChangedListener mListener;
private LocationManager locationManager;
private GoogleMap map;
private Context ctx;
private int intervalTime;
private int intervalDistance;
public void setMap(GoogleMap map)
{
this.map = map;
}
public int getIntervalTime()
{
return intervalTime;
}
public void setIntervalTime(int intervalTime)
{
this.intervalTime = intervalTime;
}
public int getIntervalDistance()
{
return intervalDistance;
}
public void setIntervalDistance(int intervalDistance)
{
this.intervalDistance = intervalDistance;
}
public FindMyLocationManager(Context mContext)
{
this.ctx = mContext;
locationManager = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
}
#Override
public void activate(OnLocationChangedListener listener)
{
mListener = listener;
isGooglePlayOk(); //returns true
if(isGPSAvailable())
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, intervalTime, intervalDistance, this);
}
else if(isCompassAvailable())
{
Log.d("DEBUG", "No GPS here");
}
else
{
Log.d("DEBUG", "Nothing here");
}
}
private boolean isGPSAvailable()
{
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
public boolean isGooglePlayOk()
{
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(ctx);
if(isAvailable == ConnectionResult.SUCCESS)
{
Toast.makeText(ctx, "Can connect to Goolge Play", Toast.LENGTH_SHORT).show();
return true;
}
else if(GooglePlayServicesUtil.isUserRecoverableError(isAvailable))
{
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, (Activity)ctx, 9001);
dialog.show();
}
else
{
Toast.makeText(ctx, "Can't connect to Goolge Play", Toast.LENGTH_SHORT).show();
}
return false;
}
private boolean isCompassAvailable()
{
PackageManager pm =
ctx.getPackageManager();
return pm.hasSystemFeature(PackageManager.FEATURE_SENSOR_COMPASS);
}
#Override
public void deactivate()
{
locationManager.removeUpdates((android.location.LocationListener)this);
mListener = null;
}
public void restart()
{
locationManager.removeUpdates((android.location.LocationListener)this);
if(isGPSAvailable())
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, intervalTime, intervalDistance, this);
}
else if(isCompassAvailable())
{
Log.d("DEBUG", "No GPS");
}
else
{
Log.d("DEBUG", "Nothing at all");
}
}
// the compiler never enters here
#Override
public void onLocationChanged(Location location)
{
Toast.makeText(this.ctx, location.getLatitude() + " " + location.getLongitude(), Toast.LENGTH_LONG).show();
if(mListener != null)
{
mListener.onLocationChanged(location);
}
map.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
}
#Override
public void onProviderDisabled(String arg0)
{
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String arg0)
{
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2)
{
// TODO Auto-generated method stub
}
}
And here is the usage of above code:
// this method is called in many places in the program, like onCreate of my view with map or onResume
private void setUpMapIfNeeded()
{
if(map == null)
{
map = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
if(map != null)
{
map.setMyLocationEnabled(true);
locationManager.setMap(map);
locationManager.setIntervalDistance(0);
locationManager.setIntervalTime(0);
map.setLocationSource(locationManager); //Here I apply the object from above class
//if(currentModel != null)
//currentModel = getCurrentModel(); TODO
//moveCameraInstantly(map.);
focusCamera();
fillMapWithMarkers(FindMyApplication.MAP_MARKER_MODELS);
}
}
}
UPDATE
So it seems that the error itself is harmless, but I still don't get the onLocationChanged event.
UPDATE 2
This code is based on How to get My Location changed event with Google Maps android API v2? .
If i understand correctly you have defined the location update method, but have not started requesting the location updates.
To send the request for location updates, create a location client and a request in onCreate():
protected void onCreate(Bundle savedInstanceState) {
...
mLocationClient = new LocationClient(this, this, this);
mLocationRequest = LocationRequest.create();
}
Then connect it in onStart():
protected void onStart() {
...
mLocationClient.connect();
}
Then make the update request in onConnected():
public void onConnected(Bundle dataBundle) {
...
mLocationClient.requestLocationUpdates(mLocationRequest, this);
}
Here is a complete guide on how to do this correctly:
http://developer.android.com/training/location/receive-location-updates.html#StartUpdates
The Google Play services resources were not found. error is a common bug in the library.
if the map shows like it should then you did everything correctly, there is something in the library that is causing the problem that google needs to fix. I get this error in my app even when I dont use google maps.
since you have google play service you should be using the new location API and not the old one.
http://developer.android.com/training/location/index.html
EDIT
Also this piece of code seems suspicious:
if(map == null) {
map = <something new>;
if(map != null) {
<do thing>
}
}
Is your map always null before entering this method?
EDIT2
map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
Log.d("DEBUG", "setOnMyLocationChangeListener");
setUpMapIfNeeded();
}
});
if it doesn't work, then try also:
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
#Override
public onCameraChange(CameraPosition cameraPosition) {
Log.d("DEBUG", "setOnCameraChangeListener");
setUpMapIfNeeded();
}
});
I need log results on this.

Android onLocationChanged and MainActivity class

I have the following code:
public class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
// called when the listener is notified with a location update from the GPS
Log.d("Latitude", Double.toString(loc.getLatitude()));
Log.d("Longitude", Double.toString(loc.getLongitude()));
}
#Override
public void onProviderDisabled(String provider) {
// called when the GPS provider is turned off (user turning off the GPS on the phone)
}
#Override
public void onProviderEnabled(String provider) {
// called when the GPS provider is turned on (user turning on the GPS on the phone)
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
and in my MainActivity
LocationListener locationListener = new MyLocationListener();
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Now, all I want is to receive the current position of the device ONCE to the MainActivity class (get altitude and longitude variables to use later in the application).
A. how do I stop receiving the location after a single time? The function lm.removeUpdates(listener) can only be called in the MainActivity class.
B. basically the same. How do I connect between the MyLocationListener class and the MainActivity one?
Sorry, I'm a newbie to Android and Java development.
And thanks!
You may use the following sample code:
public class LocationGetter {
private final Context context;
private Location location = null;
private final Cordinate gotLocationLock = new Cordinate();
private final LocationResult locationResult = new LocationResult() {
#Override
public void gotLocation(Location location) {
synchronized (gotLocationLock) {
LocationGetter.this.location = location;
gotLocationLock.notifyAll();
Looper.myLooper().quit();
}
}
};
public LocationGetter(Context context) {
if (context == null)
throw new IllegalArgumentException("context == null");
this.context = context;
}
public void getLocation(int maxWaitingTime, int updateTimeout) {
try {
final int updateTimeoutPar = updateTimeout;
synchronized (gotLocationLock) {
new Thread() {
public void run() {
Looper.prepare();
LocationResolver locationResolver = new LocationResolver();
locationResolver.prepare();
locationResolver.getLocation(context, locationResult, updateTimeoutPar);
Looper.loop();
}
}.start();
gotLocationLock.wait(maxWaitingTime);
}
} catch (InterruptedException e1) {
e1.printStackTrace();
}
gteAddress ();
}
public double getLatitude() {
return location.getLatitude();
}
public double getLongitude() {
return location.getLongitude();
}
In your activity use:
_locationGetter=new LocationGetter(context);
_locationGetter.getLocation(200000000, 10000000);
_locationGetter.getLongitude();
_locationGetter.getLatitude();
You can also use LocationManager.removeUpdates after obtining the coordinates (and possibly checking if the coordinates are sufficient for your needs):
#Override
public void onLocationChanged(Location loc) {
// called when the listener is notified with a location update from the GPS
Log.d("Latitude", Double.toString(loc.getLatitude()));
Log.d("Longitude", Double.toString(loc.getLongitude()));
lm.removeUpdates(this);
}

Categories