Installation

Reminder:

Bear in mind that this tutorial only scratches the surface of what Android SDK does.

Installing Android SDK is quite simple and straightforward.

SDK in Jcenter:

We have 3 .aar file, one of them is necessary that called sdk-common. To use our services, you have to add sdk-service. 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_services:2.0.4'


Go to app Gradle and add following library dependency to your gradlle and sync.



  dependencies {
      implementation 'com.squareup.okhttp3:okhttp:3.11.0'
      implementation 'com.google.code.gson:gson:2.8.5'
      implementation 'io.reactivex:rxandroid:1.2.1'
      implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
      implementation 'ir.map.sdk:sdk_common:2.0.0'
      implementation 'ir.map.sdk:sdk_services:2.0.4'
    }
                    

You have to initialize our sdk in your Application class like below:



    import android.app.Application;
    import ir.map.sdk_services.ServiceSDK;

    public class AppController extends Application {

       @Override
       public void onCreate() {
           super.onCreate();

           ServiceSDK.init(this);
       }
    }
  

Tips:

  • Manually Sync the project. If sync fails please Use some VPN in order to sucessfully build the project dependency
  • Once setup is succesfull follow the tutorial below

Map Services

Reverse Geocode:

Follow the below code in order to get reverse geocode:



  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.widget.TextView;
  import ir.map.sdk_service.ServiceHelper;
  import ir.map.sdk_service.models.MaptexError;
  import ir.map.sdk_service.models.MaptexReverse;
  import ir.map.sdk_service.models.base.ResponseListener;

  public class MainActivity extends AppCompatActivity implements ResponseListener<MaptexReverse> {

      private TextView responseText;
      private double lattitude, longitude;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          responseText = (TextView) findViewById(R.id.response_text);
          latitude = 35.6964895;
          longitude = 51.279745;

          new ServiceHelper().getReverseGeoInfo(latitude, longitude, MainActivity.this);
      }

      @Override
      public void onSuccess(MaptexReverse response) {
          responseText.setText(response.addressCompact);
      }

      @Override
      public void onError(MaptexError maptexError) {
      }
  }


										

Go to your activity layout (Ex:main_activity.xml) file and paste the content

Change your own Activity code to this:



  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import ir.map.sdk_service.ServiceHelper;
  import ir.map.sdk_service.models.MaptexError;
  import ir.map.sdk_service.models.MaptexSearchItem;
  import ir.map.sdk_service.models.MaptexSearchResponse;
  import ir.map.sdk_service.models.base.ResponseListener;

  public class MainActivity extends AppCompatActivity implements ResponseListener<MaptexSearchResponse> {

      private EditText textSearch;
      private Button buttonSearch;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          textSearch = (EditText) findViewById(R.id.text_search);
          buttonSearch = (Button) findViewById(R.id.button_search);
          buttonSearch.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  new ServiceHelper()
                          .search(textSearch.getText().toString(), 35.6964895, 51.279745, null, MainActivity.this);
              }
          });
      }

      @Override
      public void onError(MaptexError maptexError) {
      }

      @Override
      public void onSuccess(MaptexSearchResponse response) {
          for (MaptexSearchItem item : response.getValues()){
             // mMap.addMarker(new MaptexMarkerOptions().title(item.text)
               //       .position(new MaptexLatLng(item.coordinate.latitude, item.coordinate.longitude)));
          }
      }
  }

										

Go to your activity layout (Ex:main_activity.xml) file and paste the content

Route in Map:

After creating a map, you can create a route like this:



  import android.graphics.Color;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.util.Log;

  import java.util.ArrayList;
  import java.util.List;

  import ir.map.sdk_common.MaptexLatLng;
  import ir.map.sdk_map.MaptexPolyUtil;
  import ir.map.sdk_map.MaptexSphericalUtil;
  import ir.map.sdk_map.wrapper.MaptexCameraUpdate;
  import ir.map.sdk_map.wrapper.MaptexCameraUpdateFactory;
  import ir.map.sdk_map.wrapper.MaptexJointType;
  import ir.map.sdk_map.wrapper.MaptexLatLngBounds;
  import ir.map.sdk_map.wrapper.MaptexMap;
  import ir.map.sdk_map.wrapper.MaptexPolyline;
  import ir.map.sdk_map.wrapper.MaptexPolylineOptions;
  import ir.map.sdk_map.wrapper.MaptexRoundCap;
  import ir.map.sdk_map.wrapper.OnMaptexReadyCallback;
  import ir.map.sdk_map.wrapper.SupportMaptexFragment;
  import ir.map.sdk_service.RouteMode;
  import ir.map.sdk_service.ServiceHelper;
  import ir.map.sdk_service.models.MaptexError;
  import ir.map.sdk_service.models.MaptexManeuver;
  import ir.map.sdk_service.models.MaptexRouteResponse;
  import ir.map.sdk_service.models.MaptexStepsItem;
  import ir.map.sdk_service.models.base.ResponseListener;

  public class MainActivity extends AppCompatActivity implements ResponseListener {

      private MaptexMap mMap;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          setUpMapIfNeeded();
      }

      @Override
      protected void onResume() {
          super.onResume();
          setUpMapIfNeeded();
      }

      private void setUpMapIfNeeded() {
          // Do a null check to confirm that we have not already instantiated the map.
          if (mMap == null)
              // Try to obtain the map wrap the SupportMapFragment.
              ((SupportMaptexFragment) getSupportFragmentManager().findFragmentById(R.id.myMapView))
                      .getMaptexAsync(new OnMaptexReadyCallback() {
                          @Override
                          public void onMaptexReady(MaptexMap var1) {
                              mMap = var1;
                              if (mMap != null) // Checks if we were successful in obtaining the map
                                  setUpMap();
                          }
                      });
      }

      private void setUpMap() {
          mMap.moveCamera(MaptexCameraUpdateFactory.newLatLngZoom(new MaptexLatLng(35.6964895, 51.279745), 12));
          MaptexLatLng latLng1 = new MaptexLatLng(35.7413024, 51.421966);
          MaptexLatLng latLng2 = new MaptexLatLng(35.7411668, 51.4261612);
          new ServiceHelper().getRouteInfo(latLng1, latLng2, RouteMode.BASIC, this);
      }

      @Override
      public void onSuccess(MaptexRouteResponse response) {
          updateRoutingInfo(response);
      }

      @Override
      public void onError(MaptexError maptexError) {
          Log.e("Error in Route : ", maptexError.message);
      }

      public void updateRoutingInfo(MaptexRouteResponse routingInfo) {

          MaptexPolyline mainRouteLine = null;
          MaptexPolyline alternateRouteLine = null;
          List<MaptexLatLng> latLngsRouteListMain;
          List<MaptexLatLng> latLngsRouteListAlternative;
          List<MaptexManeuver> mainRouteManeuver = new ArrayList<>();
          List<MaptexManeuver> alternateRouteManeuver = new ArrayList<>();
          List<MaptexStepsItem> steps = new ArrayList<>();
          List<MaptexLatLng> mainIntersectionsPoints = new ArrayList<>();
          List<MaptexPolyline> fullMainIntersectionsLines = new ArrayList<>();

          if (mainRouteLine != null)
              mainRouteLine.remove();

          if (alternateRouteLine != null)
              alternateRouteLine.remove();

          latLngsRouteListMain = new ArrayList<>();
          latLngsRouteListAlternative = new ArrayList<>();
          //Check Route Info Null
          //Check And Init Main Route If Exists
          if (routingInfo != null && routingInfo.routes != null) {
              for (int i = 0; i < routingInfo.routes.size(); i++) {
                  if (routingInfo.routes.get(i).legs != null) {
                      for (int j = 0; j < routingInfo.routes.get(i).legs.size(); j++) {
                          if (routingInfo.routes.get(i).legs.get(j).steps != null) {
                              for (int k = 0; k < routingInfo.routes.get(i).legs.get(j).steps.size(); k++) {
                                  if (routingInfo.routes.get(i).legs.get(j).steps.get(k) != null) {
                                      if (i == 0) {
                                          steps.add(routingInfo.routes.get(i).legs.get(j).steps.get(k));
                                          mainRouteManeuver.add(routingInfo.routes.get(i).legs.get(j).steps.get(k).maneuver);
                                          latLngsRouteListMain.addAll(MaptexPolyUtil.decode(routingInfo.routes.get(i).legs.get(j).steps.get(k).geometry));
                                      } else if (i == 1) {
                                          alternateRouteManeuver.add(routingInfo.routes.get(i).legs.get(j).steps.get(k).maneuver);
                                          latLngsRouteListAlternative.addAll(MaptexPolyUtil.decode(routingInfo.routes.get(i).legs.get(j).steps.get(k).geometry));
                                      }
                                  }
                              }
                          }
                      }
                  }
              }

              //Create Main Line And Show It On Map
              if (latLngsRouteListMain.size() > 0) {
                  mainRouteLine = mMap.addPolyline(new MaptexPolylineOptions().jointType(MaptexJointType.ROUND).width(30).clickable(true).addAll(latLngsRouteListMain)
                          .visible(true).color(R.color.colorAccent).zIndex(4));
                  mainRouteLine.setStartCap(new MaptexRoundCap());
                  mainRouteLine.setEndCap(new MaptexRoundCap());

                  MaptexLatLngBounds.Builder builder = new MaptexLatLngBounds.Builder();
                  for (MaptexLatLng latLng : latLngsRouteListMain) {
                      builder.include(latLng);
                  }
                  MaptexLatLngBounds bounds = builder.build();
                  int padding = 50; // offset from edges of the map in pixels
                  MaptexCameraUpdate cu = MaptexCameraUpdateFactory.newLatLngBounds(bounds, padding);
                  mMap.animateCamera(cu);
              }

              //Create Alternate Line And Show It On Map
              if (latLngsRouteListAlternative.size() > 0) {
                  alternateRouteLine = mMap.addPolyline(new MaptexPolylineOptions().jointType(MaptexJointType.ROUND).width(12).clickable(true)
                          .visible(true).color(R.color.colorPrimaryDark).zIndex(4).addAll(latLngsRouteListAlternative));
                  alternateRouteLine.setStartCap(new MaptexRoundCap());
                  alternateRouteLine.setEndCap(new MaptexRoundCap());

                  MaptexLatLngBounds.Builder builder = new MaptexLatLngBounds.Builder();
                  for (MaptexLatLng latLng : latLngsRouteListAlternative) {
                      builder.include(latLng);
                  }
                  MaptexLatLngBounds bounds = builder.build();
                  int padding = 50; // offset from edges of the map in pixels
                  MaptexCameraUpdate cu = MaptexCameraUpdateFactory.newLatLngBounds(bounds, padding);
                  mMap.animateCamera(cu);
              }

              //Draw Intersections Lines
              if (mainRouteManeuver != null && mainRouteManeuver.size() > 0) {
                  for (int i = 0; i < mainRouteManeuver.size(); i++) {
                      mainIntersectionsPoints.clear();
                      fullMainIntersectionsLines.clear();
                      MaptexLatLng base = new MaptexLatLng(mainRouteManeuver.get(i).location.get(1), mainRouteManeuver.get(i).location.get(0));
                      MaptexLatLng basePrevious = MaptexSphericalUtil.computeOffset(base, 10, mainRouteManeuver.get(i).bearingBefore + 180);
                      MaptexLatLng baseNext = MaptexSphericalUtil.computeOffset(base, 10, mainRouteManeuver.get(i).bearingAfter);

                      switch (mainRouteManeuver.get(i).type) {
                          case "depart":

                              break;
                          case "turn":
                              mainIntersectionsPoints.add(basePrevious);
                              mainIntersectionsPoints.add(base);
                              mainIntersectionsPoints.add(baseNext);
                              fullMainIntersectionsLines.add(mMap.addPolyline(
                                      new MaptexPolylineOptions().color(Color.YELLOW).startCap(new MaptexRoundCap())
                                              .endCap(new MaptexRoundCap()).width(15).jointType(MaptexJointType.ROUND)
                                              .addAll(mainIntersectionsPoints).zIndex(4)
                                              .visible(true)));
                              break;
                      }
                  }
              }
          }
      }
  }

				

Go to your activity layout (Ex:main_activity.xml) file and paste the content

Static Map:

Follow the below code to get a static map:



  import android.graphics.Bitmap;
  import android.support.annotation.Nullable;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.widget.ImageView;

  import com.nostra13.universalimageloader.core.ImageLoader;
  import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

  import ir.map.sdk_service.ServiceHelper;
  import ir.map.sdk_service.models.MaptexError;
  import ir.map.sdk_service.models.base.ResponseListener;

  public class MainActivity extends AppCompatActivity implements ResponseListener<Bitmap> {

      private ImageView staticImage;

      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          staticImage = (ImageView) findViewById(R.id.static_image);

          if (!ImageLoader.getInstance().isInited()) {
              // Create global configuration and initialize ImageLoader with this config
              ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
              ImageLoader.getInstance().init(config);
          }
          getStaticMap();

      }

      private void getStaticMap() {
          new ServiceHelper().getStaticMap(35.732527, 51.422448, 12, this);
      }

      @Override
      public void onSuccess(Bitmap response) {
          staticImage.setImageBitmap(response);
      }

      @Override
      public void onError(MaptexError maptexError) {
      }
  }

				

Go to your activity layout (Ex:main_activity.xml) file and paste the content