1

Here is Main Activity.java code :

package com.example.vd;

import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;

import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;

import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;

public class MainActivity extends FragmentActivity implements LocationListener {


GoogleMap googleMap;
LatLng myPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SupportMapFragment fm=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map1);
    googleMap=fm.getMap();
    googleMap.setMyLocationEnabled(true);
    LocationManager locationManager=(LocationManager)getSystemService(LOCATION_SERVICE);
    Criteria criteria=new Criteria();
    String provider =locationManager.getBestProvider(criteria, true);
    Location location=locationManager.getLastKnownLocation(provider);
    if(location!=null){
        double latitude =location.getLatitude();
        double longitude=location.getLongitude();
        LatLng latlng=new LatLng(latitude, longitude);
        myPosition=new LatLng(latitude, longitude);
        googleMap.addMarker(new MarkerOptions().position(myPosition).title("Here"));
    }





}

@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;
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

@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

}

}

here is activity_main.xml code :

<?xml version="1.0" encoding="utf-8"?>
<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" >
<fragment 
    android:id="@+id/map1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>

And here is AndroidManifest.xml code :

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

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="14" />
<uses-feature 
    android:glEsVersion="0x00020000"
    android:required="true"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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-permission android:name="com.example.vd.permission.MAPS_RECEIVE"/>
<permission android:name="com.example.vd.permission.MAPS_RECEIVE"
     />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.vd.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="AIzaSyB5pN0xyJTRLVOAX3scagfb58b0bB0TBwg"/>
</application>

And I'm using Android 4.2.2 and using library google-play-services_lib I've already search on the Internet and try to fix it for many many times but I still getting this error : Unfortunately application has stopped error.

CRABOLO
  • 8,605
  • 39
  • 41
  • 68
user3113734
  • 19
  • 2
  • 6

2 Answers2

0

See here,just change the api key with your key in manifest file and follow these steps: and make sure that generate api key with package name which is mentioned in android manifest file and your google_play_services_lib project should be present in your project's work space only.

Manifest file:

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <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" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.geeklabs.map.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="replace with your API key"/>

    </application>

</manifest>

MainActivity.java:

    package com.geeklabs.map;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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

}

activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <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:name="com.google.android.gms.maps.MapFragment"/>

After got this let me know.

And make sure following steps done correct or not:

Steps: * to ensure that device has Google Play services APK * to install Google Play Service rev. more than 2

enter image description here

  • to create project at https://code.google.com/apis/console/
  • to enable "Google Maps Android API v2" enter image description here
  • to register of SHA1 in project (NOW, YOU NEED WRITE SHA1;your.app.package.name) at APIs console and get API KEY
  • to copy directory ANDROID_SDK_DIR/extras/google/google_play_services/libproject/google-play-services_lib to root of your project
  • to add next line to the YOUR_PROJECT/project.properties

android.library.reference.1=google-play-services_lib

  • to add next lines to the YOUR_PROJECT/proguard-project.txt

-keep class * extends java.util.ListResourceBundle {

protected Object[][] getContents();

}

Okay, now you ready to create your own Google Map app with using Google Map APIs V2 for Android.

If you create application with min SDK = 8, please use android support library v4 + SupportMapFragment instead of MapFragment.

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
  • i still getting error : Unfortunely application ........... here is may console code : - vd] Android Launch! - vd] adb is running normally. Performing com.geeklabs.map.MainActivity activity launch Uploading vd.apk onto device '0123456789ABCDEF' Installing vd.apk... Success! Starting activity com.geeklabs.map.MainActivity on device 0123456789ABCDEF ActivityManager: Starting: Intent { v.vvv.v......} – user3113734 Dec 18 '13 at 05:17
  • You are trying to get map in avd right.. so you can follow this link which i answered in previous question.http://stackoverflow.com/questions/19372399/running-google-map-application-on-android-emulator/19407415#19407415 – Shailendra Madda Dec 18 '13 at 05:29
  • and here is some pictures : http://www.mediafire.com/download/9tgtwc6l4a21gfn/New+folder+%283%29.rar – user3113734 Dec 18 '13 at 05:36
  • i try run it on avd but it takes for a long times so i upload my files, you can test it http://www.mediafire.com/download/nsd74y4nel5dtb1/vd.rar – user3113734 Dec 18 '13 at 05:59
0

Replace your Code

from

SupportMapFragment fm=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map1);

to

MapFragment fm=(MapFragment)getFragmentManager().findFragmentById(R.id.map1);

Also Add

<permission
        android:name="com.xxxxxx.locationapi.maps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

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

For More Details: Visit: Android Maps V2

Community
  • 1
  • 1
Anchit Mittal
  • 3,412
  • 4
  • 28
  • 48