I am new to android. I am trying to develop a small app for loading a map with markers in android studio using leaflet and openstreetmap. I have read the article in this link https://asmaloney.com/2014/01/code/creating-an-interactive-map-with-leaflet-and-openstreetmap/#comment-10133 where author explains how to load the map using leaflet and openstreetmap in JavaScript. But I am trying to implement it completely in java in android studio. Is there any source code available for it to start with?
Leaflet is a free library and it helps to load more data on the map. I have implemented the google map in Android studio using Google Api. Also I have tried openstreetmap alone for loading map. Here is my java file.
package com.example.myapplication;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.preference.PreferenceManager;
import org.osmdroid.api.IMapController;
import org.osmdroid.config.Configuration;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ScaleBarOverlay;
public class MainActivity extends Activity {
private MapView map;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//initialise osmdroid configuration
Context ctx = getApplicationContext();
Configuration.getInstance().load(ctx,
PreferenceManager.getDefaultSharedPreferences(ctx));
Configuration.getInstance().setUserAgentValue(BuildConfig.APPLICATION_ID);
setContentView(R.layout.activity_main);
map = (MapView) findViewById(R.id.map);
map.setMultiTouchControls(true);
map.setBuiltInZoomControls(true);
IMapController mapController = map.getController();
mapController.setZoom(14);
mapController.setCenter(new GeoPoint(48.745, -3.455));
ScaleBarOverlay scala = new ScaleBarOverlay(map);
map.getOverlays().add(scala);
map.invalidate();
}
public void onResume() {
super.onResume();
map.onResume(); //needed for compass, my location overlays, v6.0.0
and up
}
public void onPause() {
super.onPause();
//this will refresh the osmdroid configuration on resuming.
//if you make changes to the configuration, use
//SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(this);
//Configuration.getInstance().save(this, prefs);
map.onPause(); //needed for compass, my location overlays, v6.0.0
and up
}
}
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<org.osmdroid.views.MapView
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
I expect a sample source code to understand how to start with leaflet and openstreetmap for implementing the map on android studio.