Android - 上下文 - Google Maps V2 - 类型不匹配:无法从 Object 转换为 WindowManager

Posted

技术标签:

【中文标题】Android - 上下文 - Google Maps V2 - 类型不匹配:无法从 Object 转换为 WindowManager【英文标题】:Android - Context - Google Maps V2 - Type mismatch: cannot convert from Object to WindowManager 【发布时间】:2013-04-30 15:43:34 【问题描述】:

我正在尝试使用 getSystemService() 方法获取 WindowManager。这是我的代码:

private int calculateZoomLevel()  
   int ht, screenWidth; 
   DisplayMetrics displayMetrics = new DisplayMetrics();
   WindowManager winManager;
   winManager = Context.getSystemService(Context.WINDOW_SERVICE);
   winManager.getDefaultDisplay().getMetrics(displayMetrics);

   ht = displayMetrics.heightPixels;
   screenWidth = displayMetrics.widthPixels;

   double equatorLength = 40075004; 
   double widthInPixels = screenWidth;
   double metersPerPixel = equatorLength / 256; 
   int zoomLevel = 1; 
   while ((metersPerPixel * widthInPixels) > 1000)  
       metersPerPixel /= 2; 
       ++zoomLevel;
   
   Log.i(TAG, "zoom level is: "+ zoomLevel);
   return zoomLevel;

具有以下代码的行:

winManager = Context.getSystemService(Context.WINDOW_SERVICE);

我得到的错误是:类型不匹配:无法从 Object 转换为 WindowManager

这段代码不是假设返回一个WindowManager 类型吗?如何解决此问题,以便获得 WindowManager 的返回类型?

【问题讨论】:

【参考方案1】:

你应该更简单地使用getWindowManager(),这是一个Activity类的方法。

【讨论】:

【参考方案2】:

你需要像这样投射它->

winManager = (WindowManager) Context.getSystemService(Context.WINDOW_SERVICE);

【讨论】:

【参考方案3】:

查看您的代码,我想知道为什么您不只是在您的 GoogleMap 对象上使用getCameraPosition()? The CameraPosition object has a public field called zoom 告诉您当前的缩放级别。

【讨论】:

以上是关于Android - 上下文 - Google Maps V2 - 类型不匹配:无法从 Object 转换为 WindowManager的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio Google Maps 在探索地图时不断返回用户位置

通过带有 Spring Boot 后端的 Google Sign-In 登录 Android

炒股软件编辑公式中:语法错.位置--行:21;列:64;相关上下文,不知道怎么回事?

“Maps SDK for Android v.3.0.0 BETA”能否在没有 Google Play 服务的设备上运行?

无法编译使用Google Services Analytics对本机项目做出反应

Failed to resolve com.android.support:support-annotations 26.0.1