0

I followed the steps to create a Google Map with API V2 according to this example:

http://wptrafficanalyzer.in/blog/showing-current-location-in-google-maps-using-api-v2-with-supportmapfragment/

I obtained de API key and tested on a Samsung Galaxy S3 (real device)

This is the Android Manifest file:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android.localizaciongooglemapv2"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />
<permission
    android:name="android.localizaciongooglemapv2.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
<uses-permission android:name="android.localizaciongooglemapv2.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="android.localizaciongooglemapv2.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
         android:value="MyAPIKey"/> <!--I'm sure It's correct-->
   <!--  <meta-data android:name="com.google.android.gms.version" 
        android:value="@integer/google_play_services_version" />--> <!-- I thought this was the problem but It is not-->
</application>

This is my Main Activity:

    package android.localizaciongooglemapv2;

import android.app.Dialog;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
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;
public class MainActivity extends FragmentActivity implements LocationListener {

    GoogleMap googleMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Getting Google Play availability status
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

        // Showing status
        if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available

            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
            dialog.show();

        }else { // Google Play Services are available

            // Getting reference to the SupportMapFragment of activity_main.xml
            SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

            // Getting GoogleMap object from the fragment
            googleMap = fm.getMap();

            // Enabling MyLocation Layer of Google Map
            googleMap.setMyLocationEnabled(true);

            // Getting LocationManager object from System Service LOCATION_SERVICE
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

            // Creating a criteria object to retrieve provider
            Criteria criteria = new Criteria();

            // Getting the name of the best provider
            String provider = locationManager.getBestProvider(criteria, true);

            // Getting Current Location
            Location location = locationManager.getLastKnownLocation(provider);

            if(location!=null){
                onLocationChanged(location);
            }
            locationManager.requestLocationUpdates(provider, 20000, 0, this);
        }

    }
    @Override
    public void onLocationChanged(Location location) {

        TextView tvLocation = (TextView) findViewById(R.id.tv_location);

        // Getting latitude of the current location
        double latitude = location.getLatitude();

        // Getting longitude of the current location
        double longitude = location.getLongitude();

        // Creating a LatLng object for the current location
        LatLng latLng = new LatLng(latitude, longitude);

        // Showing the current location in Google Map
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

        // Zoom in the Google Map
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

        // Setting latitude and longitude in the TextView tv_location
        tvLocation.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude );

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

And this is my activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/tv_location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/tv_location"
    class="com.google.android.gms.maps.SupportMapFragment" />

And I'm getting this in the error log:

11-06 12:09:03.714: D/AndroidRuntime(24366): Shutting down VM
11-06 12:09:03.714: W/dalvikvm(24366): threadid=1: thread exiting with uncaught exception (group=0x416842a0)
11-06 12:09:03.719: E/AndroidRuntime(24366): FATAL EXCEPTION: main
11-06 12:09:03.719: E/AndroidRuntime(24366): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.localizaciongooglemapv2/android.localizaciongooglemapv2.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.os.Looper.loop(Looper.java:137)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.app.ActivityThread.main(ActivityThread.java:4898)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at java.lang.reflect.Method.invokeNative(Native Method)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at java.lang.reflect.Method.invoke(Method.java:511)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at dalvik.system.NativeStart.main(Native Method)
11-06 12:09:03.719: E/AndroidRuntime(24366): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.app.Activity.setContentView(Activity.java:1924)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.localizaciongooglemapv2.MainActivity.onCreate(MainActivity.java:27)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.app.Activity.performCreate(Activity.java:5206)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
11-06 12:09:03.719: E/AndroidRuntime(24366):    ... 11 more
11-06 12:09:03.719: E/AndroidRuntime(24366): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4030500 but found 0.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
11-06 12:09:03.719: E/AndroidRuntime(24366):    at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at com.google.android.gms.maps.internal.q.v(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at com.google.android.gms.maps.internal.q.u(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at com.google.android.gms.maps.SupportMapFragment$b.cE(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at com.google.android.gms.dynamic.a.a(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290)
11-06 12:09:03.719: E/AndroidRuntime(24366):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
11-06 12:09:03.719: E/AndroidRuntime(24366):    ... 21 more
11-06 12:09:03.749: I/Process(24366): Sending signal. PID: 24366 SIG: 9

I have searched a lot of info everywhere, here in stackoverflow or in the comments of the link and I cannot get a solution. Could you help me?? Thank you very much!!

2 Answers 2

2

You can't declare the schema twice. fragment declaration should be like bellow on activity_main.xml:

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

And on the Manifest file you have to add the meta information about gms version. So add API_KEY as well as gms version within the application tag

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="YOUR MAP API KEY HERE" />
<meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

Wish best of luck...

Sign up to request clarification or add additional context in comments.

Comments

0

This is all you need to load the map:

private void setUpMap() {

        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        googleMap.setMyLocationEnabled(true);
        googleMap.getUiSettings().setZoomGesturesEnabled(true);

        }

xml:

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

For more info, follow this great tutorial:

http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.