Created
October 10, 2019 16:15
-
-
Save Redth/36dfcd8f4a91c85f7117b92efadf8c5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Android.App; | |
using Android.OS; | |
using Android.Runtime; | |
using Android.Support.Design.Widget; | |
using Android.Support.V7.App; | |
using Android.Views; | |
using Android.Widget; | |
// <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> | |
[assembly: UsesPermission(global::Android.Manifest.Permission.AccessFineLocation)] | |
namespace MapboxSimple | |
{ | |
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)] | |
public class MainActivity : AppCompatActivity, Codes.Redth.Ioninterop.IMapWrapperCallback | |
{ | |
Codes.Redth.Ioninterop.MapWrapper mapWrapper; | |
protected override void OnCreate(Bundle savedInstanceState) | |
{ | |
base.OnCreate(savedInstanceState); | |
mapWrapper = new Codes.Redth.Ioninterop.MapWrapper(this, this); | |
mapWrapper.Init("pk.eyJ1IjoicmVkdGgiLCJhIjoiNTQ1NjJiM2YxMjE1MmRkNTU3YmUyOTE5NjM2YzMzZDgifQ.QWjXXdDJw7eTToGFIdZh4g"); | |
Xamarin.Essentials.Platform.Init(this, savedInstanceState); | |
SetContentView(Resource.Layout.activity_main); | |
mapWrapper.OnCreate(savedInstanceState, Resource.Id.mapView); | |
} | |
protected override void OnResume() | |
{ | |
base.OnResume(); | |
mapWrapper.OnResume(); | |
} | |
protected override void OnStart() | |
{ | |
base.OnStart(); | |
mapWrapper.OnStart(); | |
} | |
protected override void OnPause() | |
{ | |
base.OnPause(); | |
mapWrapper.OnPause(); | |
} | |
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) | |
{ | |
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults); | |
base.OnRequestPermissionsResult(requestCode, permissions, grantResults); | |
} | |
public void MapReady() | |
{ | |
mapWrapper.AddMarker(42.051620, -82.599159, "Redth", "Redth Lives Here (ish)"); | |
Console.WriteLine("Map ready!"); | |
} | |
public void MarkerClicked(string title) | |
{ | |
Toast.MakeText(this, title, ToastLength.Short).Show(); | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package codes.redth.ioninterop; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import androidx.annotation.NonNull; | |
import com.mapbox.mapboxsdk.Mapbox; | |
import com.mapbox.mapboxsdk.annotations.Marker; | |
import com.mapbox.mapboxsdk.annotations.MarkerOptions; | |
import com.mapbox.mapboxsdk.geometry.LatLng; | |
import com.mapbox.mapboxsdk.maps.MapView; | |
import com.mapbox.mapboxsdk.maps.MapboxMap; | |
import com.mapbox.mapboxsdk.maps.MapboxMapOptions; | |
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; | |
import com.mapbox.mapboxsdk.maps.Style; | |
public class MapWrapper { | |
private final Activity parentActivity; | |
private final MapWrapperCallback callbackListener; | |
private MapView mapView; | |
private MapboxMap map; | |
public MapWrapper(Activity activity, MapWrapperCallback listener) | |
{ | |
parentActivity = activity; | |
callbackListener = listener; | |
} | |
public void init(String mapboxAccessToken) | |
{ | |
Mapbox.getInstance(parentActivity, mapboxAccessToken); | |
} | |
public void onCreate(Bundle savedInstanceState, int mapViewResourceId) | |
{ | |
mapView = (MapView)parentActivity.findViewById(mapViewResourceId); | |
mapView.onCreate(savedInstanceState); | |
mapView.getMapAsync(new OnMapReadyCallback() { | |
@Override | |
public void onMapReady(@NonNull MapboxMap mapboxMap) { | |
map = mapboxMap; | |
mapboxMap.setStyle(Style.DARK, new Style.OnStyleLoaded() { | |
@Override | |
public void onStyleLoaded(@NonNull Style style) { | |
callbackListener.mapReady(); | |
} | |
}); | |
mapboxMap.setOnMarkerClickListener(new MapboxMap.OnMarkerClickListener() { | |
@Override | |
public boolean onMarkerClick(@NonNull Marker marker) { | |
callbackListener.markerClicked(marker.getTitle()); | |
return true; | |
} | |
}); | |
} | |
}); | |
} | |
public void addMarker(double lat, double lng, String title, String snippet) | |
{ | |
map.addMarker(new MarkerOptions() | |
.position(new LatLng(lat, lng)) | |
.title(title) | |
.setSnippet(snippet)); | |
} | |
public void onStart() | |
{ | |
mapView.onStart(); | |
} | |
public void onResume() | |
{ | |
mapView.onResume(); | |
} | |
public void onPause() | |
{ | |
mapView.onPause(); | |
} | |
public void onStop() | |
{ | |
mapView.onStop(); | |
} | |
public void onLowMemory() | |
{ | |
mapView.onLowMemory(); | |
} | |
public void onDestory() | |
{ | |
mapView.onDestroy(); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package codes.redth.ioninterop; | |
import com.mapbox.mapboxsdk.maps.MapboxMap; | |
public interface MapWrapperCallback { | |
void mapReady(); | |
void markerClicked(String title); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment