【问题标题】:How to set google map loading tiles color?如何设置谷歌地图加载瓷砖颜色?
【发布时间】:2017-06-10 10:48:15
【问题描述】:

深色风格的地图上的明亮加载图块看起来不太好。有没有办法改变加载瓷砖的颜色?

【问题讨论】:

  • 您是否尝试过简单地更改片段的背景颜色?
  • @MatPag 我试过了,但没有帮助
  • 我担心实际上无法更改加载磁贴的颜色...:\
  • GMapOptions.backgroundColor,2.119引入,可以改变tile加载前出现的tile的背景颜色。

标签: android google-maps


【解决方案1】:

你可以用GoogleMap.setMapStyle换地图的主题,这样试试,

try {
            // Customise the styling of the base map using a JSON object defined
            // in a raw resource file.
            boolean success = googleMap.setMapStyle(
                    MapStyleOptions.loadRawResourceStyle(
                            this, R.raw.style_json));

            if (!success) {
                Log.e(TAG, "Style parsing failed.");
            }
        } catch (Resources.NotFoundException e) {
            Log.e(TAG, "Can't find style. Error: ", e);
        }

您可以从此official map style site 获取 json 文件,并将该 json 文件放入您的应用程序原始文件夹中

在 android 中设置地图样式的完整说明,您可以参考 official site

示例style_json

[
  {
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#212121"
      }
    ]
  },
  {
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#757575"
      }
    ]
  },
  {
    "elementType": "labels.text.stroke",
    "stylers": [
      {
        "color": "#212121"
      }
    ]
  },
  {
    "featureType": "administrative.country",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#9e9e9e"
      }
    ]
  },
  {
    "featureType": "administrative.locality",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#bdbdbd"
      }
    ]
  },
  {
    "featureType": "poi",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#757575"
      }
    ]
  },
  {
    "featureType": "poi.park",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#181818"
      }
    ]
  },
  {
    "featureType": "poi.park",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#616161"
      }
    ]
  },
  {
    "featureType": "poi.park",
    "elementType": "labels.text.stroke",
    "stylers": [
      {
        "color": "#1b1b1b"
      }
    ]
  },
  {
    "featureType": "road",
    "elementType": "geometry.fill",
    "stylers": [
      {
        "color": "#2c2c2c"
      }
    ]
  },
  {
    "featureType": "road",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#8a8a8a"
      }
    ]
  },
  {
    "featureType": "road.arterial",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#373737"
      }
    ]
  },
  {
    "featureType": "road.highway",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#3c3c3c"
      }
    ]
  },
  {
    "featureType": "road.highway.controlled_access",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#4e4e4e"
      }
    ]
  },
  {
    "featureType": "road.local",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#616161"
      }
    ]
  },
  {
    "featureType": "transit",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#757575"
      }
    ]
  },
  {
    "featureType": "water",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#000000"
      }
    ]
  },
  {
    "featureType": "water",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#3d3d3d"
      }
    ]
  }
]

【讨论】:

    【解决方案2】:

    概念:

    Tile 是一个矩形结构,它保存图像数据。 Tile (link) 由 TileProvider (link) 提供。 TileProvider 是一个为TileOverlay (link) 提供平铺图像的类。

    注意:叠加层本质上是透明的,它有 x、y 和 z 轴。它就像你面前的透明玻璃。

    TileProvider 在 TileOverlay 后面提供 Tile,就像您将书放在透明玻璃下方一样。在TileOverlay的z轴上,有GroundOverlaysCirclesPolylinesPolygons这样的层集。这意味着GroundOverlays 在最后一层,并且是最不透明的。您提供自定义图像并更改 GroundOverlay 中的颜色。您还可以维护所有其他顶层的透明度、可见性等属性。通过这种方式,您可以更改瓷砖颜色。

    现在,您的主要任务是创建自定义的 GroundOverlay。 您的GroundOverlay 图像以BitmapDescriptor 的形式提供。您可以使用BitmapDescriptorFactory 创建您的自定义BitmapDescriptor 图像,如下所示。

    BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.current_position_tennis_ball) 
    

    /* 你看,current_position_tennis_ball drawable resource,现在,你用作你的Tile 颜色 */

    实施:

    注意:MOON_MAP_URL_FORMAT 是enter code here jpg 图像,tileProvider 引用该图像(实际上是自定义图像)

    private static final String MOON_MAP_URL_FORMAT =
                    "http://mw1.google.com/mw-planetary/lunar/lunarmaps_v1/clem_bw/%d/%d/%d.jpg";
    

    /* 提供上面明亮的 jpg */

        TileProvider tileProvider /* ultimately it is an image */ = new UrlTileProvider(256, 256) {
                    @Override
                    public synchronized URL getTileUrl(int x, int y, int zoom) {
                        // The moon tile coordinate system is reversed.  This is not normal.
                        int reversedY = (1 << zoom) - y - 1;
                        String s = String.format(Locale.US, MOON_MAP_URL_FORMAT, zoom, x, reversedY);
                        URL url = null;
                        try {
                            url = new URL(s);
                        } catch (MalformedURLException e) {
                            throw new AssertionError(e);
                        }
                    return url;
                }
            };
    

    你可以看到完整的源代码here

    【讨论】:

    • 嗯,谢谢,这听起来很有希望。但有些事情并没有完全奏效。图像(我替换了它的 url,因为月亮给出了 404)出现在地图的顶部,尽管在图钉下方。在您添加的源中,添加了这样的:'mMoonTiles = map.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));',而不是GroundOverlayOptions,它似乎没有使用tileProvider
    【解决方案3】:

    试试这个

    map = new GMap2(document.getElementById("map"), { backgroundColor: "#000000" });
    

    【讨论】:

    • 感谢您的回答,但我正在为 android 编写本机应用程序
    【解决方案4】:

    您在评论中说“我正在为 android 编写本机应用程序”,但这并未反映在您的标签(android、google-maps)或标题或问题中。
    如果你使用的是 Ionic native 4.0 Framework ionicframework 那么:

     setBackgroundColor(color); //Specifies the background color of the app.
    

    【讨论】:

    • “原生应用程序(native app)是为在特定平台或设备上使用而开发的应用程序。” - 我使用 Android SDK 在 Kotlin 中编写我的应用程序
    • @raver 原生应用程序是在设备的 CPU 上运行而无需翻译的二进制文件,即 Java 在任何 Android 设备上都不是原生的(因为它是翻译的)库(即 .so)是原生的, 和可执行文件一样。 Jar 文件不是原生的。如果你说的是真的,那么一切都是可以在设备上运行的原生的,因此这个术语毫无意义。
    • 那么如何调用不使用 ionic 等框架编写的应用程序?
    • @raver 原生一词被过度使用,对不同的人有不同的含义,当你使用它时,它必须传达特定的含义,否则不要使用它。在我的狭义定义中,Iionic 不是原生的(您不必为每个 cpu 编译它)。见howtogeek.com/163759/…
    • 你没有回答我之前的问题:)。 “原生安卓应用”比较常用,我会坚持下去
    【解决方案5】:

    在您的应用程序raw folder 中创建mapLabelStyle.json。将此 json 样式复制到该文件中。

    [
      {
        "elementType": "labels",
        "stylers": [
          {
            "lightness": 5
          },
          {
            "visibility": "simplified"
          }
        ]
      },
      {
        "elementType": "labels.text",
        "stylers": [
          {
            "color": "#400040"           //You can change color of your choice
          }
        ]
      }
    ]
    

    然后使用下面的代码设置样式。

    GoogleMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(this,R.raw.mapLabelStyle));
    

    【讨论】:

    • 感谢您抽出宝贵时间,但这会改变,正如您的代码中所述,标签的颜色,而不是图块的颜色。
    • @Jakob。不确定改变颜色。但是设置透明度可能会解决您的问题。在地图上叠加透明图块会很有用,这样用户可以在叠加图块下方看到基本地图。 mTileOverlay = map.addTileOverlay(new TileOverlayOptions() .tileProvider(new UrlTileProvider() {...}) .transparency(0.5f)); 然后mTileOverlay.setTransparency(0.5f - mTileOverlay.getTransparency());。它将在0.5f0.0f 之间切换图块叠加层的透明度
    【解决方案6】:

    我也遇到了同样的问题,通过片段 XML res 解决了这个问题:

        <fragment
        android:id="@+id/fragment_google_map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:backgroundColor="@color/primaryBlue"/>
    

    【讨论】:

      猜你喜欢
      • 2012-08-23
      • 1970-01-01
      • 1970-01-01
      • 2014-06-27
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多