2

I have attempted the following activity using Google Maps API v2 (Android). The API Key generated is working and the necessary permissions in the manifest.xml has been declared accordingly.

Here is the code for the activity class, MapView.java, which references the xml Layout Google_Map.xml:

**Map View.java**

public class MapView extends Activity implements LocationListener {

    private GoogleMap googleMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_google_map);

        int status= GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
        if(status!=ConnectionResult.SUCCESS){
            int reqCode=10;
            Dialog diag= GooglePlayServicesUtil.getErrorDialog(status,this,reqCode);
        }
        else{
            buildMap();
        }

    }


    private void buildMap(){
        if(googleMap==null){
            googleMap= ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
            googleMap.setMyLocationEnabled(true);
           googleMap.getUiSettings().setMyLocationButtonEnabled(true);

            LocationManager locManager= (LocationManager)getSystemService(LOCATION_SERVICE);
            Criteria criteria= new Criteria();


            String provider= locManager.getBestProvider(criteria,true);
            Location location= locManager.getLastKnownLocation(provider);



            if(googleMap==null){
                Toast.makeText(this,"Unable to initialise map at the moment...",Toast.LENGTH_SHORT).show();
            }
            if(location!=null){
                onLocationChanged(location);
            }
            locManager.requestLocationUpdates(provider,20000,0,this);
        }
    }

    @Override
    public void onLocationChanged(Location loc){
        double lat= loc.getLatitude();
        double lng=loc.getLongitude();

        LatLng latlng= new LatLng(lat,lng);
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

    }

    @Override
    public void onProviderDisabled(String provider){}

    @Override
    public void onProviderEnabled(String provider){}

    @Override
    public void onStatusChanged(String provider,int status, Bundle extra){}





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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.google_map, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

layout file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.hotel.moeccefamilytime.MapView">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"/>

</RelativeLayout>

My experience with Android is very basic and this is my first project to incorporate Google Maps to provide geographical capabilities for users in obtaining directions to specific places.

This was the error which had occurred while debugging; the camera does not zoom in on the current location. (Location settings have been activated on the phone)

Here are the following references which have been provided but the solutions are not working for my case:

Thank you so much if you could point me in the right direction.

Community
  • 1
  • 1
Eric
  • 21
  • 3

1 Answers1

0

I had similar issue after upgrading to API Level 23 (with client google-play-service_lib 5.0.77).

The issue was resolved after Google Play Service was updated to latest (6.1.09) on the test device.

So, it seems to have been (a device-specific issue) due to incompatibility between the Google Play Service version on the device/emulator and the client library API (google-play-service_lib) in the project.

According to Google's documentation, when the client is newer than the service, the client library API is supposed to resolve out-of-date service apk issues; (and it almost does in this case since the issue does not really affect released apk's, except for logcat messages) https://developer.android.com/google/play-services/index.html.

My earlier attempts to resolve this issue by using new API key, and fresh installs of Eclipse/ADT/SDK/google-play-service_lib, as suggested in this and other similar questions did not work.

virtualSO
  • 11
  • 1