我想问一下在使用Google Maps API v2时发生的奇怪情况。 logcat中有一个错误:
找不到Google Play服务资源。检查你的项目 配置以确保包含资源。
我拥有的东西:
GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
返回true
。我正在尝试调用事件,这将允许我通过此类获取当前位置:
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
}
}
这是以上代码的用法:
// 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);
}
}
}
更新
所以似乎错误本身是无害的,但我仍然没有得到onLocationChanged
事件。
更新2
此代码基于How to get My Location changed event with Google Maps android API v2?。
答案 0 :(得分:1)
如果我理解正确,您已定义了位置更新方法,但尚未开始请求位置更新。
要发送位置更新请求,请在onCreate()中创建位置客户端和请求:
protected void onCreate(Bundle savedInstanceState) {
...
mLocationClient = new LocationClient(this, this, this);
mLocationRequest = LocationRequest.create();
}
然后在onStart()中连接它:
protected void onStart() {
...
mLocationClient.connect();
}
然后在onConnected()中发出更新请求:
public void onConnected(Bundle dataBundle) {
...
mLocationClient.requestLocationUpdates(mLocationRequest, this);
}
以下是有关如何正确执行此操作的完整指南: http://developer.android.com/training/location/receive-location-updates.html#StartUpdates
The Google Play services resources were not found.
错误是库中常见的错误。
答案 1 :(得分:0)
修改强> 这段代码似乎也很可疑:
if(map == null) {
map = <something new>;
if(map != null) {
<do thing>
}
}
在输入此方法之前,您的地图是否始终为空?
<强> EDIT2 强>
map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
Log.d("DEBUG", "setOnMyLocationChangeListener");
setUpMapIfNeeded();
}
});
如果不起作用,请尝试:
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public onCameraChange(CameraPosition cameraPosition) {
Log.d("DEBUG", "setOnCameraChangeListener");
setUpMapIfNeeded();
}
});
我需要这个日志结果。
答案 2 :(得分:0)
如果地图显示它应该然后你做了一切正确,库中有一些东西导致谷歌需要修复的问题。即使我不使用谷歌地图,我的应用程序也会收到此错误。
由于你有谷歌播放服务,你应该使用新的位置API,而不是旧的。