Skip to content

Commit

Permalink
implement Mapbox
Browse files Browse the repository at this point in the history
  • Loading branch information
johan12345 committed Aug 6, 2020
1 parent 8892f63 commit 61c95e6
Show file tree
Hide file tree
Showing 42 changed files with 1,924 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private MapContainerView createMap() {
String[] classes = new String[]{
"com.car2go.maps.google.MapView",
"com.car2go.maps.baidu.MapView",
"com.car2go.maps.mapbox.MapView",
"com.car2go.maps.osm.MapView"
};
for (String name : classes) {
Expand Down
1 change: 1 addition & 0 deletions anymaps-mapbox/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
40 changes: 40 additions & 0 deletions anymaps-mapbox/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2015 Daimler AG / Moovel GmbH
*
* All rights reserved
*/
ext {
name = 'AnyMaps - Mapbox'
artifactId = 'anymaps.mapbox'
description = 'Mapbox version of AnyMaps'
}

apply plugin: 'com.android.library'
apply plugin: 'maven'

android {
compileSdkVersion 29

defaultConfig {
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
}
}
}

dependencies {
implementation project(':anymaps-base')

implementation('com.mapbox.mapboxsdk:mapbox-android-sdk:9.2.1') {
exclude group: 'com.mapbox.mapboxsdk', module: 'mapbox-android-accounts'
}
implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-annotation-v9:0.9.0-SNAPSHOT'

testImplementation 'junit:junit:4.13'
}
7 changes: 7 additions & 0 deletions anymaps-mapbox/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!--
~ Copyright (c) 2015 Daimler AG / Moovel GmbH
~
~ All rights reserved
-->

<manifest package="com.car2go.maps.mapbox"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2015 Daimler AG / Moovel GmbH
*
* All rights reserved
*/

package com.car2go.maps.mapbox;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import com.car2go.maps.mapbox.adapter.BitmapDescriptorAdapter;
import com.car2go.maps.model.BitmapDescriptor;
import com.mapbox.mapboxsdk.exceptions.TooManyIconsException;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.utils.BitmapUtils;

import java.util.HashMap;

import androidx.annotation.DrawableRes;

/**
* Creates instances of {@link com.car2go.maps.model.BitmapDescriptor}
*/
public class BitmapDescriptorFactory implements com.car2go.maps.BitmapDescriptorFactory {

private final Context context;
private MapboxMap map;
private static final String ICON_ID_PREFIX = "com.car2go.maps.mapbox.icon_";
private int nextId = 0;
public final HashMap<String, Bitmap> images;

public BitmapDescriptorFactory(Context context, MapboxMap map) {
this.context = context;
this.map = map;
this.images = new HashMap<>();
}

@Override
public BitmapDescriptor fromBitmap(Bitmap bitmap) {
if (nextId < 0) {
throw new TooManyIconsException();
}
String id = ICON_ID_PREFIX + ++nextId;
images.put(id, bitmap);
if (map.getStyle() != null) {
map.getStyle().addImage(id, bitmap);
}
return new BitmapDescriptorAdapter(id, bitmap);
}

@Override
public BitmapDescriptor fromResource(@DrawableRes int resourceId) {
Drawable drawable = BitmapUtils.getDrawableFromRes(context, resourceId);
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
return fromBitmap(bitmapDrawable.getBitmap());
} else {
throw new IllegalArgumentException("Failed to decode image. The resource provided must be a Bitmap.");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2015 Daimler AG / Moovel GmbH
*
* All rights reserved
*/

package com.car2go.maps.mapbox;

import com.car2go.maps.CameraUpdate;
import com.car2go.maps.mapbox.adapter.AnyMapAdapter;
import com.car2go.maps.mapbox.adapter.CameraUpdateAdapter;
import com.car2go.maps.model.LatLng;
import com.car2go.maps.model.LatLngBounds;

/**
* Creates {@link CameraUpdate} objects which can be used to update map camera
*/
public class CameraUpdateFactory implements com.car2go.maps.CameraUpdateFactory {

private final AnyMapAdapter anyMapAdapter;

public CameraUpdateFactory(AnyMapAdapter anyMapAdapter) {
this.anyMapAdapter = anyMapAdapter;
}

@Override
public CameraUpdate newLatLngZoom(LatLng latLng, float zoomLevel) {
com.mapbox.mapboxsdk.geometry.LatLng googleLatLng = anyMapAdapter.map(latLng);

return new CameraUpdateAdapter(
com.mapbox.mapboxsdk.camera.CameraUpdateFactory.newLatLngZoom(
googleLatLng,
zoomLevel
)
);
}

@Override
public CameraUpdate newLatLngBounds(LatLngBounds bounds, int padding) {
com.mapbox.mapboxsdk.geometry.LatLngBounds googleBounds = anyMapAdapter.map(bounds);

return new CameraUpdateAdapter(
com.mapbox.mapboxsdk.camera.CameraUpdateFactory.newLatLngBounds(
googleBounds,
padding
)
);
}

@Override
public CameraUpdate zoomTo(float zoomLevel) {
return new CameraUpdateAdapter(
com.mapbox.mapboxsdk.camera.CameraUpdateFactory.zoomTo(zoomLevel)
);
}

}
107 changes: 107 additions & 0 deletions anymaps-mapbox/src/main/java/com/car2go/maps/mapbox/MapView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2015 Daimler AG / Moovel GmbH
*
* All rights reserved
*/

package com.car2go.maps.mapbox;

import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;

import com.car2go.maps.AnyMap;
import com.car2go.maps.MapContainerView;
import com.car2go.maps.OnMapReadyCallback;
import com.car2go.maps.mapbox.adapter.MapboxMapAdapter;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.Style;

import androidx.annotation.NonNull;

/**
* @see com.car2go.maps.MapContainerView
*/
public class MapView extends MapContainerView {

private com.mapbox.mapboxsdk.maps.MapView mapView;

private AnyMap map;

public MapView(Context context) {
super(context);

initView(context, null);
}

public MapView(Context context, AttributeSet attrs) {
super(context, attrs);

initView(context, attrs);
}

private void initView(Context context, AttributeSet attrs) {
mapView = new com.mapbox.mapboxsdk.maps.MapView(context, attrs);

addView(mapView);
}

@Override
public void getMapAsync(final OnMapReadyCallback callback) {
if (map != null) {
callback.onMapReady(map);
return;
}

mapView.getMapAsync(new com.mapbox.mapboxsdk.maps.OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
if (map == null) {
final MapboxMapAdapter map = new MapboxMapAdapter(mapboxMap, mapView, getContext());
map.callback = new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
MapView.this.map = map;
callback.onMapReady(map);
}
};
}
}
});
}

@Override
public void onCreate(Bundle savedInstanceState) {
mapView.onCreate(savedInstanceState);
}

@Override
public void onResume() {
mapView.onResume();
}

@Override
public void onPause() {
mapView.onPause();
}

@Override
public void onDestroy() {
if (map != null) {
map.setMyLocationEnabled(false);
}

mapView.onDestroy();
}

@Override
public void onLowMemory() {
mapView.onLowMemory();
}

@Override
public void onSaveInstanceState(Bundle outState) {
mapView.onSaveInstanceState(outState);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2015 Daimler AG / Moovel GmbH
*
* All rights reserved
*/

package com.car2go.maps.mapbox;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;

import com.car2go.maps.AnyMap;
import com.mapbox.mapboxsdk.Mapbox;

import java.util.Set;

import static java.util.Collections.unmodifiableSet;
import static java.util.EnumSet.of;

/**
* Initializer for Mapbox maps.
*/
public class MapsConfiguration implements com.car2go.maps.MapsConfiguration {

private static final MapsConfiguration instance = new MapsConfiguration();

private MapsConfiguration() {
}

public static MapsConfiguration getInstance() {
return instance;
}

@Override
public void initialize(Context context) {
try {
ApplicationInfo app = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
Bundle metadata = app.metaData;

Mapbox.getInstance(context, metadata.getString("com.mapbox.ACCESS_TOKEN"));
Mapbox.getTelemetry().setUserTelemetryRequestState(false);
Mapbox.getTelemetry().setDebugLoggingEnabled(false);
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(e);
}
}

@Override
public Set<AnyMap.Feature> getSupportedFeatures() {
return unmodifiableSet(of(AnyMap.Feature.MAP_TYPES, AnyMap.Feature.TRAFFIC_LAYER, AnyMap.Feature.REVEALABLE));
}


}
Loading

0 comments on commit 61c95e6

Please sign in to comment.