¿Cómo puedo incluir la capa de tráfico de Google Maps?
Soy nuevo en el desarrollo dentro de Android con la API de Google Maps. He podido configurar un mapa y probar la funcionalidad básica, pero tengo problemas para implementar la lógica que se muestra en la documentación en mi propio código.
Investigué y encontré a través de la documentación de Google que debe hacer que el mapa verifique si los datos de tráfico están disponibles mediante el uso de:
public final boolean isTrafficEnabled()
y luego llamando al método:
public final boolean isTrafficEnabled() {
return mMap.isTrafficEnabled();
}
public final void setTrafficEnabled(boolean enabled) {
mMap.setTrafficEnabled(enabled);
}
No estoy exactamente seguro de cómo implementar esto, ya que soy completamente nuevo en el desarrollo. Encontré en otra fuente de documentación el siguiente ejemplo:
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var trafficLayer = new google.maps.TrafficLayer();
trafficLayer.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
Sin embargo, parece que no puedo entender cómo hacerlo correctamente. ¿Tengo que editar el manifiesto XML de alguna manera o todo esto se realiza desde mainActivity? Aquí está mi código de actividad completo:
package example.testdevice;
import android.app.Dialog;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
public class MainActivity extends FragmentActivity {
private static final int GPS_ERRORDIALOG_REQUEST = 9001;
GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (servicesOK()) { //checks if APK is available; if it is, display Map
setContentView(R.layout.activity_map);
if (initMap()){
Toast.makeText(this, "Ready to Map", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "Map not available!", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); //pass this as context
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
}
else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST); //error code, activity, request code
dialog.show();
}
else {
Toast.makeText(this, "Can't connect to Google Play Services", Toast.LENGTH_SHORT).show();
}
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private boolean initMap() {
if (mMap == null) {
SupportMapFragment mapFrag =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); // reference to support map fragment
mMap = mapFrag.getMap();
}
return (mMap != null);
}
public final boolean isTrafficEnabled() {
return mMap.isTrafficEnabled();
}
public final void setTrafficEnabled(boolean enabled) {
mMap.setTrafficEnabled(enabled);
}
}
El mapa se carga pero no muestra ningún tipo de tráfico. Cualquier ayuda sería muy apreciada; gracias de antemano.