Google Maps API Map No Renderiza on Mobile, Only in Android Studio

Asked

Viewed 847 times

0

I am learning Android alone, via the internet and am working this time with Google Maps API. I’ve done several things with it in Android Studio, displaying, using Marker, geolocation, among other functions. But when I did the APK yesterday and I ran it on the NOX virtual machine or mobile phones, the API map is not rendering. It runs the application, does the functions of the application, just does not render the map.

inserir a descrição da imagem aqui

For example, in this application below, when you click on the map (even not rendered) it displays on the screen the location data(parents, neighborhood, city, etc).

ps: It does not give any error at any time. Only the map does not render on mobile. Android Studio works normally.

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private Marker marker;
private ArrayList<LatLng> list;
private ArrayAdapter<String> adpDados;
private ArrayList<Double> recebeLatLng;

ArrayList []vet = new ArrayList[4];

double auxLat, auxLng;
int cont;
 SQLiteDatabase conn;
 RepositórioTabela repositórioTabela;
SQLiteDatabase db;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    list = new ArrayList<LatLng>();

    LatLng sydney = new LatLng(-34, 151);

    LatLng Rio = new LatLng(-23.2301558,-42.9121089);
    mMap.addMarker(new MarkerOptions().position(Rio));
    LatLng bdDados = new LatLng(auxLat,auxLng);
    customAddMarker(bdDados);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Rio,0));

    LatLng PERTH = new LatLng(-31.90,115.86);



    try {
        DataBase dataBase = new DataBase(this);

        conn = dataBase.getWritableDatabase();
        repositórioTabela = new RepositórioTabela(conn);

       //   repositórioTabela.buscaTabelaLatLng(this);
       vet = repositórioTabela.buscaTabelaLatLng(this);

        ArrayList auxList = vet[1];
        ArrayList auxList2 = vet[2];

        for (int i= 0;i<auxList.size();i++){

            double auxLatN = (double)auxList.get(i);
            double auxLngN = (double)auxList2.get(i);
            customAddMarker(new LatLng(auxLatN,auxLngN));
        }






    } catch (SQLException EX) {
        AlertDialog.Builder dlg = new AlertDialog.Builder(this);
        dlg.setMessage("Erro" + EX.getMessage());
        dlg.setNeutralButton("Ok", null);
        dlg.show();

    }
        mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            Log.i("Script", "setOnMapClickListener()");
      /*      if(marker != null){
                marker.remove();
            } */



            customAddMarker(new LatLng(latLng.latitude, latLng.longitude));
            list.add(latLng);

            View view = null;

              getLocation(view);


            //   drawRoute();

        }
    });


}


public void customAddMarker(LatLng latLng ){

    MarkerOptions options = new MarkerOptions();
    options.position(latLng);

 //   marker = mMap.addMarker(options);


    // Lembrar de fazer o DELETE MARKER NESSA CLASS
}




public void getLocation(View view){
    // https://developer.android.com/reference/android/location/Address.html

    Geocoder gc = new Geocoder(MapsActivity.this);

    List<Address> addressList;
    try {
        addressList = gc.getFromLocation(list.get(list.size() - 1).latitude, list.get(list.size() - 1).longitude, 1);
        //  addressList = gc.getFromLocationName("Rua Vergueiro, Sãoo Paulo, São Paulo, Brasil", 1);

        String address = "Rua: " + addressList.get(0).getThoroughfare() + "\n"; // Rua
        address += "Postal Code: " + addressList.get(0).getPostalCode() + "\n"; // Postal Code
        address += "Numero: " + addressList.get(0).getFeatureName() + "\n";   // Numero
        address += "Cidade: " + addressList.get(0).getLocality() + "\n";
        address += "Estado: " + addressList.get(0).getAdminArea() + "\n";
        address += "Pais: " + addressList.get(0).getCountryName() + "\n";
        address += "Latitude: " + addressList.get(0).getLatitude() + "\n";
        address += "Longitude: " + addressList.get(0).getLongitude() + "\n";
        address += "Bairro: " + addressList.get(0).getSubLocality() + "\n"; // Bairro

        //  ListaTarefas listaTarefas = new ListaTarefas();
        //  listaTarefas.markDado = "kkkk";

        String lat = "" + addressList.get(0).getLatitude();
        String lng = "" + addressList.get(0).getLongitude();
        // DATA BASE 1

        repositórioTabela = new RepositórioTabela(conn);
        for (long i = 0; i <= 500; i++) {
          //   repositórioTabela.excluir(i);
        }

        repositórioTabela.testeInserirContatos(address,lat,lng);




        Toast.makeText(MapsActivity.this, "Local: "+address, Toast.LENGTH_LONG).show();
        //  Toast.makeText(MapsActivity.this, "LatLng: "+ll, Toast.LENGTH_LONG).show();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}

public void irParaTarefas(View v){
    Intent intentAux1 = new Intent(getApplicationContext(),ListaTarefas.class);
    // aux.setClass(this,Calculadora.class);
    startActivity(intentAux1);
}

And below my manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.moorg.alertmaps">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">


    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" />

    <activity
        android:name=".MapsActivity"
        android:label="AlertMaps">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".PontoCerto"></activity>
    <activity android:name=".ListaTarefas"></activity>

</application>

  • It may be permissions problem. Which version of Android phone?

  • I tested on two phones, android 6.0 and 5.1. And both give the same error. I tested on android 4.4, 7.0 and 5 or 6 in android studio and both worked

  • Gives error or only the map does not appear?

  • No error at any time. Just do not display. Let me try to print

  • The print got a little big, but it looks like this. If I click on the place of the map, it will display the place where I clicked. (city, state, etc). And if I go on tasks the data will appear there. (the task layout works perfectly)

1 answer

1

I had this problem and I managed to solve it. I just had to open Androidmainifest and change the part where the maps Key API is requested. Instead of indicating the key using string, I typed the key directly. It looks like this: Before:

<meta-data-->
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

Afterward:

<meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="digitei a API KEY aqui" />
  • Since it is not executable code like javascript, it is not necessary to use snippet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.