目录

功能体验

展示地图

// create MapView
public class MapViewDemoActivity extends BaseActivity implements OnMapReadyCallback {
    private static final String TAG = "MapViewDemoActivity";
    private MapView mMapView;
    private HuaweiMap hMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate: ");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_view_demo);
        mMapView = findViewById(R.id.mapview_mapviewdemo);
        mMapView.onCreate(null);
        mMapView.getMapAsync(this);
    }
    @RequiresPermission(allOf = {ACCESS_FINE_LOCATION, ACCESS_WIFI_STATE})
    @Override
    public void onMapReady(HuaweiMap map) {
        Log.i(TAG, "onMapReady: ");
        hMap = map;
        hMap.setMyLocationEnabled(true);
        hMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.893478, 2.334595), 10));
    }
    @Override
    protected void onStart() {
        super.onStart();
        mMapView.onStart();
    }
    @Override
    protected void onStop() {
        super.onStop();
        mMapView.onStop();
    }
    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }
    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }
}
  

地图交互

// gesture related
public class GestureDemoActivity extends BaseActivity implements OnMapReadyCallback {
    private static final String TAG = "GestureDemoActivity";
    private SupportMapFragment mSupportMapFragment;
    private HuaweiMap hMap;
    private UiSettings mUiSettings;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gesture_demo);
        mSupportMapFragment =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapfragment_gesturedemo);
        mSupportMapFragment.getMapAsync(this);
    }
    @RequiresPermission(allOf = {ACCESS_FINE_LOCATION, ACCESS_WIFI_STATE})
    @Override
    public void onMapReady(HuaweiMap paramHuaweiMap) {
        hMap = paramHuaweiMap;
        hMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.893478, 2.334595), 10));
        hMap.setMyLocationEnabled(false);
        hMap.getUiSettings().setCompassEnabled(false);
        hMap.getUiSettings().setZoomControlsEnabled(false);
        hMap.getUiSettings().setMyLocationButtonEnabled(false);
        mUiSettings = hMap.getUiSettings();
    }
    // Enable or disable the zoom controls buttons.
    public void setZoomButtonsEnabled(View v) {
        mUiSettings.setZoomControlsEnabled(((CheckBox) v).isChecked());
    }
    // Enable or disable the my-location layer.
    public void setMyLocationLayerEnabled(View v) {
        if (PermissionChecker.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            hMap.setMyLocationEnabled(((CheckBox) v).isChecked());
        }
    }
    // Enable or disable zoom gestures.
    public void setZoomGesturesEnabled(View v) {
        mUiSettings.setZoomGesturesEnabled(((CheckBox) v).isChecked());
    }
    // Enable or disable rotate gestures.
    public void setRotateGesturesEnabled(View v) {
        mUiSettings.setRotateGesturesEnabled(((CheckBox) v).isChecked());
    }
}

添加标记

// Marker related
public class MarkerDemoActivity extends BaseActivity implements OnMapReadyCallback {
    private static final String TAG = "MarkerDemoActivity";
    private static final LatLng PARIS = new LatLng(48.893478, 2.334595);
    private SupportMapFragment mSupportMapFragment;
    private HuaweiMap hMap;
    private Marker mParis;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_marker_demo);
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.mapfragment_markerdemo);
        if (fragment instanceof SupportMapFragment) {
            mSupportMapFragment = (SupportMapFragment) fragment;
            mSupportMapFragment.getMapAsync(this);
        }
    }
    @SuppressLint("MissingPermission")
    @Override
    public void onMapReady(HuaweiMap paramHuaweiMap) {
        hMap = paramHuaweiMap;
        hMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.893478, 2.334595), 14));
    }
    // Add a marker to the map
    public void addMarker(View view) {
        if (mParis == null) {
            mParis =
                    hMap.addMarker(new MarkerOptions().position(PARIS).title("paris").snippet("hello").clusterable(true));
        }
    }
    // Remove the marker from the map
    public void deleteMarker(View view) {
        if (null != mParis) {
            mParis.remove();
            mParis = null;
        }
    }
}

多边形

// Polygon related
public class PolygonDemoActivity extends BaseActivity implements OnMapReadyCallback {
    private static final String TAG = "PolygonDemoActivity";
    private SupportMapFragment mSupportMapFragment;
    private HuaweiMap hMap;
    private Polygon mPolygon;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_polygon_demo);
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.mapfragment_polygondemo);
        if (fragment instanceof SupportMapFragment) {
            mSupportMapFragment = (SupportMapFragment) fragment;
            mSupportMapFragment.getMapAsync(this);
        }
    }
    @RequiresPermission(allOf = {ACCESS_FINE_LOCATION, ACCESS_WIFI_STATE})
    @Override
    public void onMapReady(HuaweiMap paramHuaweiMap) {
        Log.i(TAG, "onMapReady: ");
        hMap = paramHuaweiMap;
        hMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.893478, 2.334595), 10));
    }
    // Add a polygon to the map
    public void addPolygon(View view) {
        List<PatternItem> gonPattern = new ArrayList<>();

        PatternItem patternItemDash = new PatternItem(0, 200);
        gonPattern.add(patternItemDash);
        PatternItem patternItemDot = new PatternItem(1, 10);
        gonPattern.add(patternItemDot);
        PatternItem patternItemGap = new PatternItem(2, 10);
        gonPattern.add(patternItemGap);
        mPolygon = hMap
                .addPolygon(new PolygonOptions().addAll(MapUtils.createRectangle(new LatLng(48.893478, 2.334595), 0.1, 0.1))
                        .fillColor(Color.GREEN)
                        .strokeColor(Color.RED)
                        .strokeWidth(20)
                        .visible(true)
                        .zIndex(10L)
                        .geodesic(true)
                        .clickable(true)
                        .strokePattern(gonPattern)
                        .strokeJointType(ROUND));
    }
    // Remove the polygon from the map
    public void removePolygon(View view) {
        if (null != mPolygon) {
            mPolygon.remove();
        }
    }
}

定义地图样式

// StyleMap related
public class StyleMapDemoActivity extends BaseActivity implements OnMapReadyCallback {
    private SupportMapFragment mSupportMapFragment;
    private HuaweiMap hMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_style_map_demo);
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.mapfragment_stylemapdemo);
        if (fragment instanceof SupportMapFragment) {
            mSupportMapFragment = (SupportMapFragment) fragment;
            mSupportMapFragment.getMapAsync(this);
        }
    }

    @RequiresPermission(allOf = {ACCESS_FINE_LOCATION, ACCESS_WIFI_STATE})
    @Override
    public void onMapReady(HuaweiMap huaweiMap) {
        hMap = huaweiMap;
        hMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.893478, 2.334595), 10));
    }

    public void simple(View view) {
        hMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(this, R.raw.mapstyle_simple));
    }
    public void night(View view) {
        hMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(this, R.raw.mapstyle_night));
    }
    public void grayScale(View view) {
        hMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(this, R.raw.mapstyle_grayscale));
    }
    public void defaultStyle(View view) {
        hMap.setMapStyle(null);
    }
}