DreamOnJava
DreamOnJava

Reputation: 655

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:

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? .

Upvotes: 0

Views: 1185

Answers (3)

Igor K
Igor K

Reputation: 470

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.

Upvotes: 0

nillas
nillas

Reputation: 431

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.

Upvotes: 1

tyczj
tyczj

Reputation: 73751

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

Upvotes: 0

Related Questions