J'ai une application web, dans laquelle nous ajoutons la lat et la lng et dans le web, elle affiche clairement l'itinéraire sur la carte. J'ai envoyé les mêmes détails, y compris l'api, la lat et la longitude à Android en utilisant json. Mais dans l'application Android, la carte n'affiche pas le chemin correct.
Voici mon activité cartographique
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);
}
}
de l'aide ?
Mise à jour :
J'ai essayé avec première solution y solution deux
Dans les deux cas, je reçois l'erreur : OVER_QUERY_LIMIT
Quelqu'un a-t-il une solution à ce problème ?
C'est ce que j'obtiens dans la carte Android, je veux qu'ils s'affichent sur la route actuelle.
Dans le site web, il s'affiche correctement...