Google Maps Activity On Android
Solution 1:
EDITED
Your question:
"...want to learn how to use google maps through android studio"
A lot of what I wrote below can be a helpful checklist for your case, but if you want to learn more about Google Maps in Android Studio, it might be best, as I have done below, to start with a basic Blank Activity, MainActivity, and then add some Google Map elements. In the example I provide here, I start with a Blank Activity and add a simple fragment layout file which allows me to, at a minimum, test that Google Maps Android API works with a new project. I'm not relying on the Android Studio's Google Maps Activity Template provided in the New Project "Add an activity to Mobile." You can certainly learn a few things from it, but it might be better to start from scratch.
Are you testing on a physical device? If not, try it on a physical device. If you haven't already done it, turn USB Debugging on your device on. http://developer.android.com/tools/device.html
Are your location services on your device turned on? Find out by going onto your physical device Settings > Location (under Personal) and see that the slider is On.
Did you create a Google Maps API Key? If not, create one. In the following link, look at Step 4 for instructions: https://developers.google.com/maps/documentation/android-api/start In summary:
Go to console.developers.google.com
Log in and Create a Project (if you haven't already)
Create your Google Maps API Key: go to Google APIs by clicking the big blue button, click on the Google Maps Android API link, click the blue Enable button. If you haven't done this before, you'll get the following message: "This API is enabled, but you can't use it in your project until you create credentials. Click "Go to Credentials" to do this now (strongly recommended)." When you go to the Credentials page, it should automatically choose Google Maps Android API for you in the first drop down menu: Which API are you using? (if it doesn't, you will need to), and then go to the second drop down menu Where will you be calling the API from? and select choose Android. Create an API Key Name - my suggestion is just to name it something that relates to the project, but you can call it whatever you like. Add the package name and fingerprint later. Then click the button Create API Key.
Copy and paste the API Key close to the end of your AndroidManifest.xml e.g.
</application><meta-dataandroid:name="com.google.android.maps.v2.API_KEY"android:value="YOUR API KEY HERE"/></manifest>
Add the 4 permissions below to your AndroidManifest.xml between the manifest tag and the beginning of the application tag e.g.
<?xml version="1.0" encoding="utf-8"?><manifestpackage="YOUR PACKAGE NAME"xmlns:android="http://schemas.android.com/apk/res/android"><uses-permissionandroid:name="ANDROID.PERMISSION.INTERNET"/><uses-permissionandroid:name="ANDROID.PERMISSION.ACCESS_NETWORK_STATE"/><uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permissionandroid:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/><applicationandroid:allowBackup="true"...
Also make sure you have the necessary libraries and SDK packages:
- Android Support Repository
- Google Repository
- Google Play Services
- Android Support Library
Go to your build.gradle(app) file and add the Google Play dependencies, the line: compile 'com.google.android.gms:play-services:8.4.0' e.g.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'compile'com.android.support:appcompat-v7:23.1.1'compile'com.android.support:design:23.1.1'compile'com.google.android.gms:play-services:8.4.0'
}
Then add the following to your AndroidManifest.xml which will verify the Google Play Services Library in your project:
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
And then in MainActivity.java add the maps package to your import statements:
import com.google.android.gms.maps.GoogleMap; publicclassMainActivityextendsAppCompatActivity {
Since you're using a fragment, I'll stick with that here:
I create a new layout (right-click on res/layout, New > Layout Resource File) and call it activity_map with root of fragment.
In the activity_map, I've kept most of your fragment code:
<?xml version="1.0" encoding="utf-8"?><fragmentandroid:id="@+id/map"android:name="com.google.android.gms.maps.SupportMapFragment"xmlns:android="http://schemas.android.com/apk/res/android"xmlns:map="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"/>
To test the app, one final change is to add the fragment to your MainActivity in place of the activity_main. You should, of course, launch your fragment however you like when you know that this actually runs:
setContentView(R.layout.activity_map);
Solution 2:
I had same problem. This definitely help you:
In AndroidManifest.xml, add the following element as a child of the <application>
element, by inserting it just before the closing </application>
tag:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY"/>
more details: https://developers.google.com/maps/documentation/android-api/signup#add_the_api_key_to_your_application
Post a Comment for "Google Maps Activity On Android"