MapirLatLng Android Services SDK Documentation

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:3.0.2'


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:3.0.1'
      implementation 'ir.map.sdk:sdk_services:3.0.2'
    }
                    

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_services.ServiceHelper;
  import ir.map.sdk_services.models.MapirError;
  import ir.map.sdk_services.models.MapirReverse;
  import ir.map.sdk_services.models.base.ResponseListener;

  public class MainActivity extends AppCompatActivity implements ResponseListener<MapirReverse> {

      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(MapirReverse response) {
          responseText.setText(response.addressCompact);
      }

      @Override
      public void onError(MapirError mapirError) {
      }
  }


										

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_services.ServiceHelper;
  import ir.map.sdk_services.models.MapirError;
  import ir.map.sdk_services.models.MapirSearchItem;
  import ir.map.sdk_services.models.MapirSearchResponse;
  import ir.map.sdk_services.models.base.ResponseListener;

  public class MainActivity extends AppCompatActivity implements ResponseListener<MapirSearchResponse> {

      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(MapirError mapirError) {
      }

      @Override
      public void onSuccess(MapirSearchResponse response) {
          for (MapirSearchItem item : response.getValues()){
             // mMap.addMarker(new MarkerOptions().title(item.text)
               //       .position(new MapirLatLng(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.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.widget.Toast;

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

  import ir.map.sdk_common.MapirLatLng;
  import ir.map.sdk_map.annotations.Polyline;
  import ir.map.sdk_map.annotations.PolylineOptions;
  import ir.map.sdk_map.camera.CameraUpdate;
  import ir.map.sdk_map.camera.CameraUpdateFactory;
  import ir.map.sdk_map.geometry.LatLng;
  import ir.map.sdk_map.geometry.LatLngBounds;
  import ir.map.sdk_map.maps.MapirMap;
  import ir.map.sdk_map.maps.OnMapReadyCallback;
  import ir.map.sdk_map.maps.SupportMapFragment;
  import ir.map.sdk_services.RouteMode;
  import ir.map.sdk_services.ServiceHelper;
  import ir.map.sdk_services.models.MapirError;
  import ir.map.sdk_services.models.MapirManeuver;
  import ir.map.sdk_services.models.MapirRouteResponse;
  import ir.map.sdk_services.models.MapirStepsItem;
  import ir.map.sdk_services.models.base.ResponseListener;
  import ir.map.sdkdemo_service.route.MapirPolyUtil;
  import ir.map.sdkdemo_service.route.MapirSphericalUtil;

  public class RouteActivity extends AppCompatActivity implements ResponseListener {

      private MapirMap mMap;

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

          setUpMap();
      }

      private void setUpMap() {
          // 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.
              ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.myMapView))
                      .getMapAsync(new OnMapReadyCallback() {
                          @Override
                          public void onMapReady(MapirMap mapirMap) {
                              mMap = mapirMap;
                              // Check if we were successful in obtaining the map.
                              if (mMap != null) {
                                  mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(35.6964895, 51.279745), 12));
                                  MapirLatLng latLng1 = new MapirLatLng(35.7413024, 51.421966);
                                  MapirLatLng latLng2 = new MapirLatLng(35.7411668, 51.4261612);
                                  new ServiceHelper()
                                          .getRouteInfo(latLng1, latLng2, RouteMode.BASIC, RouteActivity.this);
                              }
                          }
                      });

          }
      }

      @Override
      public void onSuccess(MapirRouteResponse response) {
          updateRoutingInfo(response);

      }

      @Override
      public void onError(MapirError error) {
          Toast.makeText(this, error.message, Toast.LENGTH_LONG).show();
      }


      public void updateRoutingInfo(MapirRouteResponse routingInfo) {

          Polyline mainRouteLine = null;
          Polyline alternateRouteLine = null;
          List latLngsRouteListMain;
          List latLngsRouteListAlternative;
          List mainRouteManeuver = new ArrayList<>();
          List alternateRouteManeuver = new ArrayList<>();
          List steps = new ArrayList<>();
          List mainIntersectionsPoints = new ArrayList<>();
          List 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(MapirPolyUtil.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(MapirPolyUtil.decode(routingInfo.routes.get(i).legs.get(j).steps.get(k).geometry));
                                      }
                                  }
                              }
                          }
                      }
                  }
              }

              //Create Main Line And Show It On Map
              if (latLngsRouteListMain.size() > 0) {
                  mMap.addPolyline(new PolylineOptions().width(10).addAll(latLngsRouteListMain)
                    .color(getColor(R.color.colorAccent)));

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

              //Create Alternate Line And Show It On Map
              if (latLngsRouteListAlternative.size() > 0) {
                  mMap.addPolyline(new PolylineOptions().width(5).color(getColor(R.color.mapir_alternative_color))
                    .addAll(latLngsRouteListAlternative));

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

              }

              //Draw Intersections Lines
  //            SphericalUtil.computeOffsetOrigin()
              if (mainRouteManeuver != null && mainRouteManeuver.size() > 0) {
                  for (int i = 0; i < mainRouteManeuver.size(); i++) {
                      mainIntersectionsPoints.clear();
                      fullMainIntersectionsLines.clear();
                      LatLng base = new LatLng(mainRouteManeuver.get(i).location.get(1), mainRouteManeuver.get(i).location.get(0));
                      LatLng basePrevious = MapirSphericalUtil.computeOffset(base, 5, mainRouteManeuver.get(i).bearingBefore + 180);
                      LatLng baseNext = MapirSphericalUtil.computeOffset(base, 5, 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 PolylineOptions().color(Color.YELLOW)
                                  .addAll(mainIntersectionsPoints)));
                              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_services.ServiceHelper;
  import ir.map.sdk_services.models.MapirError;
  import ir.map.sdk_services.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(MapirError mapirError) {
      }
  }

				

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