我想询问一下在使用 Google Maps API v2 期间发生的一个奇怪情况。 logcat 中出现错误:
The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
我拥有的东西:
- Google map 与标记一起完美显示 - 我已从 Google 获取了相关代码。
- google-play-services.jar 库完美包含在 Eclipse 中 (项目->属性->android->添加...)。
- Eclipse 中的复选框(项目->属性->java 构建路径->订单构建路径)均已正确选中。
- 代码
GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
返回true
。 - 此代码在设备 Nexus 4 上运行,而不是在模拟器上运行。
我正在尝试调用事件,这将允许我通过此类获取当前位置:
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? .
最佳答案
如果我理解正确的话,您已经定义了位置更新方法,但尚未开始请求位置更新。
要发送位置更新请求,请在 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
找不到 Google Play 服务资源。
错误是库中的常见错误。
关于java - Google map v2 通过 Google Play 获取我的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20905957/