How to show the actual traffic mode on a map in Android

I have a web application in which we add lat and lng, and on the Internet it clearly shows the route on the map. I sent the same details, including api, lat and longitude, to android using json. But in the Android app, the map does not show the correct path.

This is my activity on the map.

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback { private GoogleMap mMap; LatLng latLng; private SharedPreferenceHelper sharedPreferenceHelper; private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; MyAlertDialogFragment newFragment; public final String BEFORE_BUS_POSTION = "BEFORE_BUS_POSTION"; public final String AFTER_BUS_POSTION = "AFTER_BUS_POSTION"; public final String CURRENT_BUS_POSTION = "CURRENT_BUS_POSTION"; public final String GREEN_COLOR_CODE = "#228b22"; public final String ORANGE_COLOR_CODE = "#C56E00"; private String[] mNavigationDrawerItemTitles; private DrawerLayout mDrawerLayout; private ListView mDrawerList; Toolbar toolbar; private CharSequence mDrawerTitle; private CharSequence mTitle; android.support.v7.app.ActionBarDrawerToggle mDrawerToggle; private ProgressDialog progress; int count = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); mTitle = mDrawerTitle = getTitle(); mNavigationDrawerItemTitles= getResources().getStringArray(R.array.navigation_drawer_items_array); .... sharedPreferenceHelper = new SharedPreferenceHelper(getApplicationContext()); if (!sharedPreferenceHelper.getBooleanSharedPreferenceName("isRegistred")) { RegisterGCM(); } sharedPreferenceHelper.WriteBooleanPreference("notification", true); /*Sharedpreference check wheather app is running for firsttime.*/ if (!sharedPreferenceHelper.getBooleanSharedPreferenceName("isfirsttime")) { sharedPreferenceHelper.WriteBooleanPreference("isfirsttime", true); sharedPreferenceHelper.WriteStringPreference("email", getIntent().getStringExtra("email")); sharedPreferenceHelper.WriteBooleanPreference("ringtone", true); sharedPreferenceHelper.WriteBooleanPreference("notification", true); } FragmentManager fm = getSupportFragmentManager(); SupportMapFragment supportMapFragment = SupportMapFragment.newInstance(); fm.beginTransaction().replace(R.id.content_frame, supportMapFragment).commit(); supportMapFragment.getMapAsync(this); } private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { selectItem(position); } } private void selectItem(int position) { AboutFragment aboutfragment = null; InfoFragment infofragment = null; switch (position) { case 0: startActivity(new Intent(this, MapsActivity.class)); break; case 1: aboutfragment = new AboutFragment(); break; case 2: infofragment= new InfoFragment(); break; default: break; } if (aboutfragment != null) { FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.content_frame, aboutfragment).commit(); mDrawerList.setItemChecked(position, true); mDrawerList.setSelection(position); setTitle(mNavigationDrawerItemTitles[position]); mDrawerLayout.closeDrawer(mDrawerList); }else if (infofragment != null) { FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.content_frame, infofragment).commit(); mDrawerList.setItemChecked(position, true); mDrawerList.setSelection(position); setTitle(mNavigationDrawerItemTitles[position]); mDrawerLayout.closeDrawer(mDrawerList); } else { Log.e("MapActivity", "Error in creating fragment"); } } @Override public boolean onOptionsItemSelected(MenuItem item) { if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } @Override public void setTitle(CharSequence title) { mTitle = title; getSupportActionBar().setTitle(mTitle); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); mDrawerToggle.syncState(); } void setupToolbar(){ toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayShowHomeEnabled(true); } void setupDrawerToggle(){ mDrawerToggle = new android.support.v7.app.ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.app_name, R.string.app_name); //This is necessary to change the icon of the Drawer Toggle upon state change. mDrawerToggle.syncState(); } private void RegisterGCM() { Log.i("HomeActivity", "This device is not supported."); if (checkPlayServices()) { // Start IntentService to register this application with GCM. Intent intent = new Intent(this, RegistrationIntentService.class); startService(intent); } } private boolean checkPlayServices() { GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); int resultCode = apiAvailability.isGooglePlayServicesAvailable(this); if (resultCode != ConnectionResult.SUCCESS) { if (apiAvailability.isUserResolvableError(resultCode)) { apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST) .show(); } else { if(sharedPreferenceHelper.getBooleanSharedPreferenceName("googleplayservice")) { showDialog(); } Log.i("HomeActivity", "This device is not supported."); } return false; } return true; } void showDialog() { newFragment = MyAlertDialogFragment.newInstance(R.string.google_play_service_not_found); newFragment.show(getSupportFragmentManager(), "dialog"); } public static class MyAlertDialogFragment extends DialogFragment { AlertDialog alert; public static MyAlertDialogFragment newInstance(int title) { MyAlertDialogFragment frag = new MyAlertDialogFragment(); Bundle args = new Bundle(); args.putInt("title", title); frag.setArguments(args); return frag; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { int title = getArguments().getInt("title"); return new AlertDialog.Builder(getActivity()) .setIcon(R.drawable.ic_info_black_24dp) .setTitle(title) .setMessage(R.string.google_play_service_not_found_message) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { ((MapsActivity)getActivity()).doPositiveClick(); } } ) .setNegativeButton("Don't show again", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { ((MapsActivity)getActivity()).doNegativeClick(); } } ).create(); } } public void doPositiveClick() { Log.i("FragmentAlertDialog", "Positive click!"); newFragment.dismiss(); } public void doNegativeClick() { newFragment.dismiss(); sharedPreferenceHelper.WriteBooleanPreference("googleplayservice", true); Log.i("FragmentAlertDialog", "Negative click!"); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; progress=new ProgressDialog(this); progress.setMessage("Map Loading"); progress.show(); progress.setCanceledOnTouchOutside(false); final Handler ha=new Handler(); ha.postDelayed(new Runnable() { @Override public void run() { //call function setLocationFromServerWithRoute(); ha.postDelayed(this, 2000); } }, 10000); // Add a marker in Sydney and move the camera /* LatLng sydney = new LatLng(-34, 151); mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));*/ } private void setLocationFromServerWithRoute() { RequestQueue queue = Volley.newRequestQueue(this); String email = new SharedPreferenceHelper(getApplicationContext()).getStringSharedPreferenceName("email"); String url = LoginActivity.URL+ "read.jsp?email="+email; Log.d("email",email); StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>(){ @Override public void onResponse(String response) { JSONArray array = new JSONArray(); Log.d("Response 10 secs",response); try { array = new JSONArray(response); mMap.clear(); for(int n = 0; n < array.length()-1; n++) { JSONObject jsonObject = array.getJSONObject(n); JSONObject jsonObjectNext = array.getJSONObject(n+1); LatLng latLng = new LatLng(jsonObject.getDouble("latitude"), jsonObject.getDouble("longitude")); LatLng latLngNext = new LatLng(jsonObjectNext.getDouble("latitude"), jsonObjectNext.getDouble("longitude")); String address = jsonObject.getString("routename"); Log.d("Response",response); if(jsonObjectNext.getString("busPosition").equals(CURRENT_BUS_POSTION) && n < array.length()-2 ) { jsonObjectNext = array.getJSONObject(n+2); latLngNext = new LatLng(jsonObjectNext.getDouble("latitude"), jsonObjectNext.getDouble("longitude")); } if(jsonObject.getString("busPosition").equals(CURRENT_BUS_POSTION)){ mMap.addMarker(new MarkerOptions().position(latLng).title("" + address) .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_blue))); Log.d("CURRENT_BUS_POSTION","REACHED"); }else if(jsonObject.getString("busPosition").equals(AFTER_BUS_POSTION)){ if(jsonObject.getBoolean("isBusStop")) { mMap.addMarker(new MarkerOptions().position(latLng).title("" + address) .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_orange))); } mMap.addPolyline(new PolylineOptions() .add(latLng,latLngNext) .width(5) .color(Color.parseColor(ORANGE_COLOR_CODE))); }else if(jsonObject.getString("busPosition").equals(BEFORE_BUS_POSTION)){ if(jsonObject.getBoolean("isBusStop")) { mMap.addMarker(new MarkerOptions().position(latLng).title("" + address) .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_green))); } mMap.addPolyline(new PolylineOptions() .add(latLng,latLngNext) .width(5) .color(Color.parseColor(GREEN_COLOR_CODE))); } } JSONObject jsonObject = array.getJSONObject(array.length() - 1); LatLng latLng = new LatLng(jsonObject.getDouble("latitude"), jsonObject.getDouble("longitude")); String address = jsonObject.getString("routename"); if (jsonObject.getString("busPosition").equals(CURRENT_BUS_POSTION)) { mMap.addMarker(new MarkerOptions().position(latLng).title("" + address) .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_blue))); } else if (jsonObject.getString("busPosition").equals(AFTER_BUS_POSTION) && jsonObject.getBoolean("isBusStop")) { mMap.addMarker(new MarkerOptions().position(latLng).title("" + address) .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_orange))); } else if (jsonObject.getString("busPosition").equals(BEFORE_BUS_POSTION) && jsonObject.getBoolean("isBusStop")) { mMap.addMarker(new MarkerOptions().position(latLng).title("" + address) .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_green))); } ; if(count < 2) { Log.v("count",""+count); mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); mMap.animateCamera(CameraUpdateFactory.zoomTo(16.0f)); if(count == 1){ progress.hide(); } } count ++; } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getApplicationContext(),"Error:"+error.getMessage(),Toast.LENGTH_SHORT).show(); } }); queue.add(stringRequest); } private void setLocationFromServer() { RequestQueue queue = Volley.newRequestQueue(this); String email = new SharedPreferenceHelper(getApplicationContext()).getStringSharedPreferenceName("email"); String url = LoginActivity.URL+ "read.jsp?email="+email; StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>(){ @Override public void onResponse(String response) { Toast.makeText(getApplicationContext(),"Response:"+response,Toast.LENGTH_SHORT).show(); Log log = null; log.v("response",""+response); JSONObject jsonObject = null; Double param1 = 0.0; Double param2 = 0.0; Long datecreated = null; String dateCreated = null; Date dateCREATED = null, twoHOURDate; Calendar cal = Calendar.getInstance(); cal.add(Calendar.HOUR, -2); Date twoHourBack = cal.getTime(); Log.v("twoHourBack::", "" + twoHourBack); Toast.makeText(getApplicationContext(),"+twoHourBack:"+twoHourBack,Toast.LENGTH_SHORT).show(); JSONObject jsonObjectData = new JSONObject(); JSONArray array = new JSONArray(); try { jsonObjectData = new JSONObject(response); array = jsonObjectData.getJSONArray("route"); } catch (JSONException e) { e.printStackTrace(); } ArrayList<BusRoute> arrayList = new ArrayList(); for(int n = 0; n < array.length(); n++) { try { JSONObject object = array.getJSONObject(n); BusRoute busRoute = new BusRoute(); busRoute.setId(object.getString("routeID")); busRoute.setLatitude(object.getString("latitude")); busRoute.setLongitude(object.getString("longitude")); busRoute.setDate(object.getString("dateCreated")); busRoute.setPlaceWeight(object.getString("busRouteweight")); busRoute.setPlaceId(object.getString("routename")); arrayList.add(busRoute); } catch (JSONException e) { e.printStackTrace(); } } Collections.sort(arrayList, new Comparator<BusRoute>() { @Override public int compare(BusRoute o1, BusRoute o2) { return o1.getDate().compareTo(o2.getDate()); } }); boolean isBusLocationPassed = false; for(int n = 0; n < arrayList.size(); n++) { try { BusRoute object = arrayList.get(n); param1 = Double.parseDouble(object.getLatitude()); param2 = Double.parseDouble(object.getLongitude()); latLng = new LatLng(param1, param2); Geocoder geocoder = new Geocoder(MapsActivity.this, Locale.getDefault()); List<Address> addresses = null; try { addresses = geocoder.getFromLocation(param1, param2, 1); } catch (Exception e) { e.printStackTrace(); } String cityName = addresses.get(0).getAddressLine(0); String stateName = addresses.get(0).getAddressLine(1); String countryName = addresses.get(0).getAddressLine(2); log.v("cityName", "" + cityName); log.v("lat", "" + param1); log.v("log", "" + param2); BusRoute objectnext = null; if( n < arrayList.size()-1 ) { objectnext = arrayList.get(n + 1); } if(object.getId().trim() .equals(jsonObjectData.getString("prevStop").trim())){ Polyline line = mMap.addPolyline(new PolylineOptions() .add(new LatLng(Double.parseDouble(object.getLatitude()), Double.parseDouble(object.getLongitude())), new LatLng(jsonObjectData.getDouble("latitude"), jsonObjectData.getDouble("Longitude"))) .width(5) .color(Color.GREEN)); if( objectnext != null ) { Polyline line2 = mMap.addPolyline(new PolylineOptions() .add(new LatLng(jsonObjectData.getDouble("Lattitude"), jsonObjectData.getDouble("Longitude")), new LatLng(Double.parseDouble(objectnext.getLatitude()), Double.parseDouble(objectnext.getLongitude()))) .width(5) .color(Color.parseColor("#C56E00"))); } log.v("done", "1 " + jsonObjectData.getString("nextStop")); LatLng latLngCurrent = new LatLng(jsonObjectData.getDouble("Lattitude"), jsonObjectData.getDouble("Longitude")); mMap.addMarker(new MarkerOptions().position(latLngCurrent).title("" + cityName + "" + stateName + "" + countryName) .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_blue))); mMap.addMarker(new MarkerOptions().position(latLng).title("" + cityName + "" + stateName + "" + countryName) .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_green))); isBusLocationPassed = true; }else if(isBusLocationPassed){ if( objectnext != null ) { Polyline line = mMap.addPolyline(new PolylineOptions() .add(new LatLng(Double.parseDouble(object.getLatitude()), Double.parseDouble(object.getLongitude())), new LatLng(Double.parseDouble(objectnext.getLatitude()), Double.parseDouble(objectnext.getLongitude()))) .width(5) .color(Color.parseColor("#C56E00"))); } log.v("done", "2 " + jsonObjectData.getString("nextStop")); mMap.addMarker(new MarkerOptions().position(latLng).title("" + cityName + "" + stateName + "" + countryName) .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_orange))); }else{ if( objectnext != null ) { Polyline line = mMap.addPolyline(new PolylineOptions() .add(new LatLng(Double.parseDouble(object.getLatitude()), Double.parseDouble(object.getLongitude())), new LatLng(Double.parseDouble(objectnext.getLatitude()), Double.parseDouble(objectnext.getLongitude()))) .width(5) .color(Color.GREEN)); } log.v("done", "3 " + jsonObjectData.getString("nextStop")); mMap.addMarker(new MarkerOptions().position(latLng).title("" + cityName + "" + stateName + "" + countryName) .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_green))); } mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); mMap.animateCamera(CameraUpdateFactory.zoomTo(10.0f)); } catch (JSONException e) { e.printStackTrace(); } } Toast.makeText(getApplicationContext(), "Location received, Latitude:" + param1 + " Longitude:" + param2, Toast.LENGTH_SHORT).show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getApplicationContext(),"Error:"+error.getMessage(),Toast.LENGTH_SHORT).show(); } }); queue.add(stringRequest); } } 

any help?

Update:

I tried with solution one and solution two

In both cases, I get an error: OVER_QUERY_LIMIT

Anyone have any solution for this ???

This is how I am on the Android map. I want them to appear in the real way of the road.

enter image description here

On the website, it displays correctly ...

enter image description here

+7
source share
2 answers

To display the actual road path on an Android map, you can use geodesic(true) as shown below.

 Polyline line = map.addPolyline(new PolylineOptions() .add(new LatLng(-37.81319, 144.96298), new LatLng(-31.95285, 115.85734)) .width(25) .color(Color.BLUE) .geodesic(true)); 

According to this link (Read Geodetic segments )

The geodesic setting determines how the line segments between the consecutive vertices of the polyline / polygon are drawn. Geodesic segments are those that follow the shortest path along (the ball) and often appear as curved lines on a map with a Mercator projection. Non-geodetic segments are drawn as straight lines on the map.

Set this property in the shape options object by calling * Options.geodesic (), where true indicates that the segments should be drawn as geodesics, and false indicates that the segments should be drawn as straight lines. If not specified, non-geodesic segments (False) are used by default.

For OVER_QUERY_LIMIT: - The use of the Google Maps API web services depends on the number of requests within 24 hours. Requests sent that exceed these limits will receive an error message.

If you exceed your usage limits, you will receive the OVER_QUERY_LIMIT status OVER_QUERY_LIMIT as an answer.

This means that the web service will stop providing normal responses and will switch to returning only the OVER_QUERY_LIMIT status code until more use is allowed again. This can happen:

  • Within a few seconds if an error is received because your application sends too many requests per second.
  • Over the next 24 hours, if an error is received because your application sends too many requests per day. Day quotas reset at midnight, Pacific Time.

Solutions: -

The above problems can be solved by combining two approaches:

  • Decrease in usage, optimizing applications for more efficient use of web services.
  • Increase usage limits when possible by purchasing additional benefits for your Google Maps API to work license.

See the link for more details.

Hope this helps you.

+3
source

Error

Exceeding a request means that you are performing too many requests per second for the API or have exceeded your quota.

In order to help you, I will need to know in detail which APIs you are using, since I do not know that I am assuming that you are using the Google Maps Maps API , which gives you the paths you want.

Limits of use

Each API has a usage limit. In the case of the Roads API, you can only make 50 requests per second (QPS), and you have other restrictions. To see a list of restrictions on this API, I recommend that you check the link below:

I do not use the road API!

If you are not using the Roads API, you will need to find which API you are using and the restriction for it. The following is a list of all the APIs offered by Google Maps:

Ok, I know which API I use, and I push the limit. Now what?

Once you have identified the previous steps, you usually have two main options to avoid the limitation:

  • Caching Information
  • Throttling requests

Each decision is a world in itself, but I will leave you with some directions in the concepts, so that you have an easier time after them:

I specifically recommend the last link, which points to official documentation and covers both caching and throttling.

Hope this helps!


PS: I also supported the question, not seeing any real reason why someone could lower it. However, I recommend that you add a Minimal Working Example instead of publishing all your code, and this will make more people willing to help you.

In the case of Android, I usually recommend creating a project from scratch as minimal as possible, which can replicate your problem.

0
source

Source: https://habr.com/ru/post/1262012/


All Articles