0

Am trying to implement a google map on my app that has other activities but it is not working. When i compile the apk and run it in my phone the map appears blank anybody with an idea. I tried to check my API key but it appears to be okay. I have been working on this for 3days any help?

This are my files

androidmanifest.xml

 <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

  <application
        android:allowBackup="true"
        android:icon="@mipmap/kaps"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
   <activity
            android:name=".SplashScreen"
            android:label="@string/app_name" >
   <intent-filter>
   <action android:name="android.intent.action.MAIN" />

   <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
   </activity>
   <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
   <intent-filter>
   <action android:name="android.intent.action.MAIN" />

   <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>
   </activity>
   <activity
            android:name=".models.StartWatchActivity"
            android:label="@string/title_activity_start_watch" >
   </activity>
   <activity
            android:name=".models.PaymentActivity"
            android:label="@string/title_activity_payment" >
   </activity>
   <activity
            android:name=".models.LocationActivity"
            android:label="@string/title_activity_location" >
    </activity>
    <activity
            android:name=".models.GetCurrentLocation"
            android:label="@string/title_activity_get_current_location" >
    </activity>
    <activity
            android:name=".models.ConfirmRegistration"
            android:label="@string/title_activity_confirm_registration" >
    </activity>

    <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="*******************************" />

    <activity
            android:name=".models.MapsActivity"
            android:label="@string/title_activity_maps" >
    </activity>
    </application>

MapActivity.java

import android.location.Address;
import android.location.Geocoder;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;


import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import org.w3c.dom.Text;

import java.io.IOException;
import java.util.List;

public class MapsActivity extends FragmentActivity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }


    public void onSearch(View view){

        EditText location_tf=(EditText)findViewById(R.id.TFaddress);
        String location=location_tf.getText().toString();
        List<Address> addressList=null;
        if(location !=null ||!location.equals(""))
        {
            Geocoder geocoder=new Geocoder(this);
            try {
                addressList=geocoder.getFromLocationName(location, 1);
            } catch (IOException e) {
                e.printStackTrace();
            }

            Address address=addressList.get(0);
            LatLng latLng=new LatLng(address.getLatitude(),address.getLongitude());
            mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
            mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
        }
    }

    public void onZoom(View view)
    {
        if (view.getId()==R.id.Bzoomin)
        {
            mMap.animateCamera(CameraUpdateFactory.zoomIn());
        }
        if (view.getId()==R.id.Bzoomout)
        {
            mMap.animateCamera(CameraUpdateFactory.zoomOut());
        }
    }
    public void changeType(View view)
    {
        if(mMap.getMapType()==GoogleMap.MAP_TYPE_NORMAL)
        {
            mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        }
        else
            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    }
    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }


    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
        mMap.setMyLocationEnabled(true);
    }
}

locationactivity.java when a button is clicked takes you to the MapActivity

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.example.barbegambino.parkcar.R;


public class LocationActivity extends AppCompatActivity {


    Button button4,button,button5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);

        button4 = (Button) findViewById(R.id.button4);
        button = (Button) findViewById(R.id.button);
        button5 = (Button) findViewById(R.id.button5);



        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LocationActivity.this, GetCurrentLocation.class);
                startActivity(intent);

            }
        });



        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(LocationActivity.this, StartWatchActivity.class);
                startActivity(intent);
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LocationActivity.this,MapsActivity.class);
                startActivity(intent);
            }
        });
    }

    @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_location, menu);
        return true;
    }

    @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);
    }
}
Nico
  • 201
  • 3
  • 11

1 Answers1

1

Your problem occurs due to missing of Google Play Service.

3 step process:-

4.4 Kitkat

5.0 Lollipop

5.1 Lollipop

6.0 Marshmallow

7.0 Nougat

7.1 Nougat (webview patch)

Download from above link Just drag & drop downloaded zip file to genymotion and restart Add google account and download Google Play Service and Run.

Note :- The above process can be automated if you install OpenGapps apk in your device.

Download link for apk

Mr Robot
  • 1,747
  • 6
  • 35
  • 67