Thanks in advance!
I have been trying to run the simple heremaps example apps in android studio 3.0.1 using my phone Redmi 4 (marshmallow), but everytime I face this issue ->
ERROR : Cannot initialize map with error : UNKNOWN
And then the on clicking any button the app simply crashes. I have googled a lot and have performed all the steps correctly.
- step 1.
download HERE-sdk.aar into app/libs folder
- step 2.
setting build.gradle
- step 3.
setting app id, app token, license key. The package name in the same in my app and the heremaps website.
Here is the code of AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--Developers should put application credentials here.To obtain them, please register the application
at https://developer.here.com/develop/mobile-sdks-->
<meta-data
android:name="com.here.android.maps.appid"
android:value="mpKhTK1bbxvdPstxE0qx" />
<meta-data
android:name="com.here.android.maps.apptoken"
android:value="_QF2rIfGgk_-Z0LYOQduAw" />
<meta-data
android:name="com.here.android.maps.license.key"
android:value="c/2m8I4SQmHNH2N45CkDnWNtsN/58aK+8cSfK9XzFsGWAHt7WXFDBnNRYr8uEaKKT4zCyLTZhsIsz29z/sgfHikrOyX61on3n8wFIZqCnOJKeeDPmUHTNfOu2HzVmY/fwIbih+N6kw6K1QJpvaZHYsPbtbb+VvQKTIJH+uPq66Cp5bdBGWreF/CipyFb160g7kCXw0y1V8SvJ23R0ueQG/nM59lHtfFzEuboLNTfvesNwfDGmIx04A+PVS42hWKFEyldoSr64nrpuPcoBCKCcfUIW/ctXU1UYzAn8Hi0mCn6wZRAcGx/VJsU47MODzEMCbcc05wjCrtU1En89ieyt2ayAF0h2jdeANfmKrE0LcPLNYkE9K199/JiKM68PxAbP7YlvFy0uIKRK54EXxZ2FC1XVcAMtOy51QNfAScRtSsJ2a2JiIZEyUtBC0K6Kqtbl3I6eUiNilhb9M9CkRK8gYsalkRnYQJ+iNxyDIdTGI6Y3hUVgmRfHnI/MDtDeqtAEMpgqKFDTvYWvl31vIOZ4S5Xpk0TfwQFctOyywgLT/oMc6rZpPF/YibDQQWLjDi9moXPggbGPNTJMb+m88X7NRrcqYVYxIHyvwKsAAXzftPhW9c8u92X9cjCZwbEmnZ4wOY+gO99+4qA5A0rha0hP1WyVEo0Lep1W/8AebDXTpY=" />
<!--Developers should always provide custom values for each of {YOUR_LABEL_NAME} and {YOUR_INTENT_NAME}.
Do not reuse HERE SDK defaults.-->
<meta-data
android:name="INTENT_NAME"
android:value="android.intent.action.MAIN" />
<service
android:name="com.here.android.mpa.service.MapService"
android:label="@string/app_name"
android:process="global.Here.Map.Service.v2"
android:exported="true" >
<intent-filter>
<action android:name="com.here.android.mpa.service.MapService" >
</action>
</intent-filter>
</service>
</application>
code for MainActivity.java:
`package com.here.android.example.routing;
import java.util.ArrayList;
import java.util.List;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
/**
- Main activity which launches map view and handles Android run-time requesting permission.
*/
public class MainActivity extends AppCompatActivity {
private final static int REQUEST_CODE_ASK_PERMISSIONS = 1;
private MapFragmentView m_mapFragmentView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermissions();
}
private void requestPermissions() {
final List<String> requiredSDKPermissions = new ArrayList<String>();
requiredSDKPermissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
requiredSDKPermissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
requiredSDKPermissions.add(Manifest.permission.INTERNET);
requiredSDKPermissions.add(Manifest.permission.ACCESS_WIFI_STATE);
requiredSDKPermissions.add(Manifest.permission.ACCESS_NETWORK_STATE);
ActivityCompat.requestPermissions(this,
requiredSDKPermissions.toArray(new String[requiredSDKPermissions.size()]),
REQUEST_CODE_ASK_PERMISSIONS);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS: {
for (int index = 0; index < permissions.length; index++) {
if (grantResults[index] != PackageManager.PERMISSION_GRANTED) {
/**
* If the user turned down the permission request in the past and chose the
* Don't ask again option in the permission request system dialog.
*/
if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
permissions[index])) {
Toast.makeText(this,
"Required permission " + permissions[index] + " not granted. "
+ "Please go to settings and turn on for sample app",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this,
"Required permission " + permissions[index] + " not granted",
Toast.LENGTH_LONG).show();
}
}
}
/**
* All permission requests are being handled.Create map fragment view.Please note
* the HERE SDK requires all permissions defined above to operate properly.
*/
m_mapFragmentView = new MapFragmentView(this);
break;
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}`
code for build.gradle:
`apply plugin: 'com.android.application'
android {
compileSdkVersion 23
defaultConfig {
applicationId 'com.here.android.example.routing'
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile(name:'HERE-sdk', ext:'aar')
implementation(name: 'HERE-sdk', ext: 'aar')
implementation 'com.google.code.gson:gson:2.2.4'
implementation 'com.android.support:appcompat-v7:23.4.0'
}`
code for MapFragmentView.java:
`package com.here.android.example.routing;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.here.android.mpa.common.GeoBoundingBox;
import com.here.android.mpa.common.GeoCoordinate;
import com.here.android.mpa.common.OnEngineInitListener;
import com.here.android.mpa.mapping.Map;
import com.here.android.mpa.mapping.MapFragment;
import com.here.android.mpa.mapping.MapRoute;
import com.here.android.mpa.routing.CoreRouter;
import com.here.android.mpa.routing.RouteOptions;
import com.here.android.mpa.routing.RoutePlan;
import com.here.android.mpa.routing.RouteResult;
import com.here.android.mpa.routing.RouteWaypoint;
import com.here.android.mpa.routing.Router;
import com.here.android.mpa.routing.RoutingError;
import java.io.File;
import java.util.List;
/**
-
This class encapsulates the properties and functionality of the Map view.A route calculation from
-
HERE Burnaby office to Langley BC is also being handled in this class
*/
public class MapFragmentView {
private MapFragment m_mapFragment;
private Button m_createRouteButton;
private Activity m_activity;
private Map m_map;
private MapRoute m_mapRoute;
public MapFragmentView(Activity activity) {
m_activity = activity;
initMapFragment();
/*
* We use a button in this example to control the route calculation
*/
initCreateRouteButton();
}
private void initMapFragment() {
/* Locate the mapFragment UI element */
m_mapFragment = (MapFragment) m_activity.getFragmentManager()
.findFragmentById(R.id.mapfragment);
// Set path of isolated disk cache
String diskCacheRoot = Environment.getExternalStorageDirectory().getPath()
+ File.separator + ".isolated-here-maps";
// Retrieve intent name from manifest
String intentName = "";
try {
ApplicationInfo ai = m_activity.getPackageManager().getApplicationInfo(m_activity.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
intentName = bundle.getString("INTENT_NAME");
} catch (PackageManager.NameNotFoundException e) {
Log.e(this.getClass().toString(), "Failed to find intent name, NameNotFound: " + e.getMessage());
}
boolean success = com.here.android.mpa.common.MapSettings.setIsolatedDiskCacheRootPath(diskCacheRoot, intentName);
if (!success) {
// Setting the isolated disk cache was not successful, please check if the path is valid and
// ensure that it does not match the default location
// (getExternalStorageDirectory()/.here-maps).
// Also, ensure the provided intent name does not match the default intent name.
Toast.makeText(m_activity,
"ERROR: No success!",
Toast.LENGTH_LONG).show();
} else {
if (m_mapFragment != null) {
/* Initialize the MapFragment, results will be given via the called back. */
m_mapFragment.init( new OnEngineInitListener() {
@Override
public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
if (error == Error.NONE) {
/* get the map object */
m_map = m_mapFragment.getMap();
/*
* Set the map center to the 4350 Still Creek Dr Burnaby BC (no animation).
*/
m_map.setCenter(new GeoCoordinate(49.259149, -123.008555, 0.0),
Map.Animation.NONE);
/* Set the zoom level to the average between min and max zoom level. */
m_map.setZoomLevel((m_map.getMaxZoomLevel() + m_map.getMinZoomLevel()) / 2);
} else {
Toast.makeText(m_activity,
"ERROR: Cannot initialize Map with error " + error,
Toast.LENGTH_LONG).show();
}
}
});
}
}
}
private void initCreateRouteButton() {
m_createRouteButton = (Button) m_activity.findViewById(R.id.button);
m_createRouteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*
* Clear map if previous results are still on map,otherwise proceed to creating
* route
*/
if (m_map != null && m_mapRoute != null) {
m_map.removeMapObject(m_mapRoute);
m_mapRoute = null;
} else {
/*
* The route calculation requires local map data.Unless there is pre-downloaded
* map data on device by utilizing MapLoader APIs, it's not recommended to
* trigger the route calculation immediately after the MapEngine is
* initialized.The INSUFFICIENT_MAP_DATA error code may be returned by
* CoreRouter in this case.
*
*/
createRoute();
}
}
});
}
/* Creates a route from 4350 Still Creek Dr to Langley BC with highways disallowed /
private void createRoute() {
/ Initialize a CoreRouter */
CoreRouter coreRouter = new CoreRouter();
/* Initialize a RoutePlan */
RoutePlan routePlan = new RoutePlan();
/*
* Initialize a RouteOption.HERE SDK allow users to define their own parameters for the
* route calculation,including transport modes,route types and route restrictions etc.Please
* refer to API doc for full list of APIs
*/
RouteOptions routeOptions = new RouteOptions();
/* Other transport modes are also available e.g Pedestrian */
routeOptions.setTransportMode(RouteOptions.TransportMode.CAR);
/* Disable highway in this route. */
routeOptions.setHighwaysAllowed(false);
/* Calculate the shortest route available. */
routeOptions.setRouteType(RouteOptions.Type.SHORTEST);
/* Calculate 1 route. */
routeOptions.setRouteCount(1);
/* Finally set the route option */
routePlan.setRouteOptions(routeOptions);
/* Define waypoints for the route */
/* START: 4350 Still Creek Dr */
RouteWaypoint startPoint = new RouteWaypoint(new GeoCoordinate(49.259149, -123.008555));
/* END: Langley BC */
RouteWaypoint destination = new RouteWaypoint(new GeoCoordinate(49.073640, -122.559549));
/* Add both waypoints to the route plan */
routePlan.addWaypoint(startPoint);
routePlan.addWaypoint(destination);
/* Trigger the route calculation,results will be called back via the listener */
coreRouter.calculateRoute(routePlan,
new Router.Listener<List<RouteResult>, RoutingError>() {
@Override
public void onProgress(int i) {
/* The calculation progress can be retrieved in this callback. */
}
@Override
public void onCalculateRouteFinished(List<RouteResult> routeResults,
RoutingError routingError) {
/* Calculation is done.Let's handle the result */
if (routingError == RoutingError.NONE) {
if (routeResults.get(0).getRoute() != null) {
/* Create a MapRoute so that it can be placed on the map */
m_mapRoute = new MapRoute(routeResults.get(0).getRoute());
/* Show the maneuver number on top of the route */
m_mapRoute.setManeuverNumberVisible(true);
/* Add the MapRoute to the map */
m_map.addMapObject(m_mapRoute);
/*
* We may also want to make sure the map view is orientated properly
* so the entire route can be easily seen.
*/
GeoBoundingBox gbb = routeResults.get(0).getRoute()
.getBoundingBox();
m_map.zoomTo(gbb, Map.Animation.NONE,
Map.MOVE_PRESERVE_ORIENTATION);
} else {
Toast.makeText(m_activity,
"Error:route results returned is not valid",
Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(m_activity,
"Error:route calculation returned error code: " + routingError,
Toast.LENGTH_LONG).show();
}
}
});
}
}`
I am frustrated as I don't see any error here. Any help will be appreciated:)