像Uber这样的Android Google地图样式
时间:2020-02-23 14:28:57 来源:igfitidea点击:
今天,我们将了解如何在Android应用程序中实现Google地图样式。
说明
到目前为止,我们已经在许多教程中讨论和实现了Google Maps。
以下是它们的列表:
- Android Google Maps示例教程
- Android Google Maps API集成
- Android Google Maps当前位置,夜间模式功能
- Android Google Map –两点之间的绘图路线
- Android Google Map Street View示例
- Google静态地图Android
Android Studio提供了Google Maps的默认模板。
让我们使用它。
如之前的教程所述,您需要添加API密钥才能通过Google Cloud Console使用Google Maps。
要设置地图样式,我们只需对Google Map实例执行以下操作。
mMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(
this, "jsonPathGoesHere");
项目结构
Android Google Maps Style Project
原始文件夹JSON文件中提供了地图样式。
您可以自定义它们或者从Web浏览各种地图样式。
代码
下面给出了activity_maps.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:tools="https://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity"
<Button
android:id="@+id/btnChangeStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="CHANGE MAP STYLE"
</FrameLayout>
SupportMapFragment用于在我们的活动布局中展示地图。
下面给出了MapsActivity.java类的代码:
package com.theitroad.androidgooglemapsstyling;
import android.content.res.Resources;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MapStyleOptions;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private Button button;
int[] rawArray = {R.raw.assasin_creed, R.raw.uber_style, R.raw.game_style, R.raw.vintage_style};
int currentIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
button = findViewById(R.id.btnChangeStyle);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setMapStyle();
}
});
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
setMapStyle();
}
private void setMapStyle() {
if (currentIndex == rawArray.length && rawArray.length != 0) {
currentIndex = 0;
}
try {
mMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
this, rawArray[currentIndex++]));
} catch (Resources.NotFoundException e) {
Log.e("MapsActivity", "Cannot find style.", e);
}
}
}
地图准备好后,就会触发onMapReady。
其中,我们设置了一个虚拟位置标记。
setMapStyle用于切换存储在数组中的各种地图样式。

