Reminder:
Bear in mind that this tutorial only scratches the surface of what Android SDK does.
Bear in mind that this tutorial only scratches the surface of what Android SDK does.
Installing Android SDK is quite simple and straightforward.
We have 3 .aar file, one of them is necessary that called sdk-common. To use our map, you have to add sdk-map. we added it to Jcenter, so you can add this lib by adding below line to dependencies in your build.gradle file.
* implementation 'ir.map.sdk:sdk_map:2.0.4'
Go to app Gradle and add following library dependency to your gradlle and sync.
dependencies {
implementation 'com.google.android.gms:play-services-maps:15.0.1'
implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
implementation 'com.google.maps.android:android-maps-utils:0.5'
implementation 'ir.map.sdk:sdk_map:2.0.4'
implementation 'ir.map.sdk:sdk_common:2.0.0'
}
You have to initialize our sdk in your Application class like below:
import android.app.Application;
import ir.map.sdk_map.MapSDK;
public class AppController extends Application {
@Override
public void onCreate() {
super.onCreate();
MapSDK.init(this);
}
}
Creating a basic map is pretty easy. You begin with calling the Android SDK
1. Go to your activity layout (Ex:main_activity.xml) file and paste the content
2. Go to Manifest file and add following permissions.
3. Create an application class in your src (main) folder and extend it from Application(or MultidexApplication) class in project.
import android.app.Application;
import ir.map.sdk_map.MapSDK;
public class AppController extends Application
{
@Override
public void onCreate() {
super.onCreate();
MapSDK.init(this); //TODO Add this line when you import sdk-map
}
}
4. Do not forget to add application class name in manifest file
<application
.....
android:name=".AppController"
android:allowBackup="true"
android:largeHeap="true">
.....
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value=" YOUR API KEY " />
......
</application>
Build the project and run it, Enjoy your Map view
In order to set the current location enable in your map view follow the below steps:
1. Go to your own Activity java file and paste the content
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import ir.map.sdk_map.wrapper.MaptexMap;
import ir.map.sdk_map.wrapper.OnMaptexReadyCallback;
import ir.map.sdk_map.wrapper.SupportMaptexFragment;
public class MainActivity extends AppCompatActivity {
private MaptexMap maptexMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((SupportMaptexFragment) getSupportFragmentManager().findFragmentById(R.id.myMapView))
.getMaptexAsync(new OnMaptexReadyCallback() {
@Override
public void onMaptexReady(MaptexMap map) {
maptexMap = map;
if (maptexMap != null) { // Checks if we were successful in obtaining the map
//region get permissions
//TODO : This block of code is necessary for android level 6.0 or high
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
//endregion get permissions
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
maptexMap.setMyLocationEnabled(true);
}
}
});
}
}
In order to apply zoom in your map view follow the below steps:
1. Go to your own Activity java file and paste the content
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import ir.map.sdk_common.MaptexLatLng;
import ir.map.sdk_map.wrapper.MaptexCameraUpdateFactory;
import ir.map.sdk_map.wrapper.MaptexMap;
import ir.map.sdk_map.wrapper.OnMaptexReadyCallback;
import ir.map.sdk_map.wrapper.SupportMaptexFragment;
public class MainActivity extends AppCompatActivity {
//Change the following constants for getting your desired location
public static final double FOCUSED_LATITUDE = 35.699773;
public static final double FOCUSED_LONGITUDE = 51.337888;
public static final int FOCUSED_ZOOM_LEVEL = 15;
private MaptexMap maptexMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//TODO Zoom in Specific Location
((SupportMaptexFragment) getSupportFragmentManager().findFragmentById(R.id.myMapView))
.getMaptexAsync(new OnMaptexReadyCallback() {
@Override
public void onMaptexReady(MaptexMap map) {
maptexMap = map;
if (maptexMap != null) { // Checks if we were successful in obtaining the map
maptexMap.animateCamera(MaptexCameraUpdateFactory
.newLatLngZoom(new MaptexLatLng(FOCUSED_LATITUDE, FOCUSED_LONGITUDE),
FOCUSED_ZOOM_LEVEL));
}
}
});
}
}
After creating a map, you can create a marker like this:
Change your own Activity with the following code
* Add any drawable ic_marker for showing marker.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import ir.map.sdk_common.MaptexLatLng;
import ir.map.sdk_map.wrapper.MaptexBitmapDescriptorFactory;
import ir.map.sdk_map.wrapper.MaptexMap;
import ir.map.sdk_map.wrapper.MaptexMarker;
import ir.map.sdk_map.wrapper.MaptexMarkerOptions;
import ir.map.sdk_map.wrapper.OnMaptexReadyCallback;
import ir.map.sdk_map.wrapper.SupportMaptexFragment;
public class MainActivity extends AppCompatActivity {
private static final MaptexLatLng TEHRAN = new MaptexLatLng(35.699773, 51.337888);
private MaptexMap maptexMap;
private MaptexMarker mTehran;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((SupportMaptexFragment)getSupportFragmentManager().findFragmentById(R.id.myMapView))
.getMaptexAsync(new OnMaptexReadyCallback() {
@Override
public void onMaptexReady(MaptexMap map) {
maptexMap = map;
if (maptexMap != null) { // Checks if we were successful in obtaining the map
//TODO : Don't forget add a drawable named : ic_marker
mTehran = maptexMap.addMarker(new MaptexMarkerOptions()
.position(TEHRAN).title("Tehran").snippet("Population: 8,627,300")
.icon(MaptexBitmapDescriptorFactory.fromResource(R.drawable.ic_marker)));
}
}
});
}
}
Change your own Activity code to this:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import ir.map.sdk_common.MaptexLatLng;
import ir.map.sdk_map.wrapper.MaptexMap;
import ir.map.sdk_map.wrapper.MaptexPolylineOptions;
import ir.map.sdk_map.wrapper.OnMaptexReadyCallback;
import ir.map.sdk_map.wrapper.SupportMaptexFragment;
public class MainActivity extends AppCompatActivity {
private static final MaptexLatLng ARAK = new MaptexLatLng(34.0954, 49.7013);
private static final MaptexLatLng SHIRAZ = new MaptexLatLng(29.591, 52.5837);
private static final MaptexLatLng RASHT = new MaptexLatLng(37.2682, 49.5891);
private MaptexMap maptexMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((SupportMaptexFragment) getSupportFragmentManager().findFragmentById(R.id.myMapView))
.getMaptexAsync(new OnMaptexReadyCallback() {
@Override
public void onMaptexReady(MaptexMap map) {
maptexMap = map;
if (maptexMap != null) // Checks if we were successful in obtaining the map
maptexMap.addPolyline((new MaptexPolylineOptions()).add(ARAK, SHIRAZ, RASHT));
}
});
}
}
After creating a map, you can create a polygon like this:
import ir.map.sdk_common.MaptexLatLng;
import ir.map.sdk_map.wrapper.MaptexMap;
import ir.map.sdk_map.wrapper.MaptexPolygonOptions;
import ir.map.sdk_map.wrapper.OnMaptexReadyCallback;
import ir.map.sdk_map.wrapper.SupportMaptexFragment;
public class MainActivity extends AppCompatActivity {
private static final MaptexLatLng ARAK = new MaptexLatLng(34.0954, 49.7013);
private static final MaptexLatLng SHIRAZ = new MaptexLatLng(29.591, 52.5837);
private static final MaptexLatLng MASHHAD = new MaptexLatLng(36.2605, 59.6168);
private static final MaptexLatLng RASHT = new MaptexLatLng(37.2682, 49.5891);
private List polygonPointList = new ArrayList<>();
private MaptexMap maptexMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((SupportMaptexFragment) getSupportFragmentManager().findFragmentById(R.id.myMapView))
.getMaptexAsync(new OnMaptexReadyCallback() {
@Override
public void onMaptexReady(MaptexMap map) {
maptexMap = map;
if (maptexMap != null) { // Checks if we were successful in obtaining the map
polygonPointList.add(ARAK);
polygonPointList.add(SHIRAZ);
polygonPointList.add(MASHHAD);
polygonPointList.add(RASHT);
maptexMap.addPolygon(new MaptexPolygonOptions()
.addAll(polygonPointList)
.fillColor(Color.TRANSPARENT)
.strokeColor(Color.BLUE)
.strokeWidth(5));
}
}
});
}
}