android - why Can't I see the map -
i want run demo google map not showing map. here code.i have checked api key correct
package com.example.googlemaps; import com.google.android.maps.mapactivity; import android.os.bundle; import android.view.menu; public class mainactivity extends mapactivity{ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override protected boolean isroutedisplayed() { // todo auto-generated method stub return true; } }
manifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.googlemaps" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8"/> <uses-permission android:name="android.permission.internet" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <uses-library android:name="com.google.android.maps" /> <activity android:name="com.androidhive.googlemaps.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> </application> </manifest>
layout xml
<?xml version="1.0" encoding="utf-8"?> <com.google.android.maps.mapview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mapview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:apikey="@string/api_key" />
this old way getting map. google has updated now. use
<fragment android:id="@+id/map" android:layout_width="fill_parent" android:layout_height="fill_parent" class="com.google.android.gms.maps.supportmapfragment" />
in xml layout.
provide api key in manifest file
<meta-data android:name="com.google.android.maps.v2.api_key" android:value="your-api-key" />
in application tag.
also don't extend mapactivity
. instead extend normal activity
then write below code in oncreate
int status = googleplayservicesutil .isgoogleplayservicesavailable(getbasecontext()); if (status != connectionresult.success) { // google play services not available int requestcode = 10; dialog dialog = googleplayservicesutil.geterrordialog(status, this, requestcode); dialog.show(); } else { // google play services available // getting reference supportmapfragment supportmapfragment fragment = (supportmapfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.map); // getting google map mymap = fragment.getmap(); // enabling mylocation in google map mymap.setmylocationenabled(true); // getting locationmanager object system service // location_service locationmanager locationmanager = (locationmanager) getsystemservice(location_service); // creating criteria object retrieve provider criteria criteria = new criteria(); // getting name of best provider string provider = locationmanager.getbestprovider(criteria, true); // getting current location gps location location = locationmanager.getlastknownlocation(provider); if (location != null) { onlocationchanged(location); } locationmanager.requestlocationupdates(provider, 20000, 0, this); }
also add following code on onresume
int resultcode = googleplayservicesutil .isgoogleplayservicesavailable(getapplicationcontext()); if (resultcode == connectionresult.success) { } else { googleplayservicesutil.geterrordialog(resultcode, this, rqs_googleplayservices); }
that's it. best!
Comments
Post a Comment