What is possibly wrong with this code?
I'm not seeing what could possibly be intrefering and the API key is right but it continues to show a grey area.
Can someone give a touch on this?
MAIN ACTIVITY
package com.example;
*ALL THE IMPORTS
public class MainActivity extends Activity {
private GoogleMap googleMap;
private int mapType = GoogleMap.MAP_TYPE_NORMAL;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
MapFragment mapFragment = (MapFragment) fragmentManager.findFragmentById(R.id.map);
googleMap = mapFragment.getMap();
googleMap.setMapType(mapType);
}
}
ACTIVITY_MAIN.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>
MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17"/>
<permission
android:name="com.example.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<!--Required permissions-->
<uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--Used by the API to download map tiles from Google Maps servers: -->
<uses-permission android:name="android.permission.INTERNET"/>
<!--Allows the API to access Google web-based services: -->
<uses-permission
android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!--Allows the API to cache map tile data in the device's external storage area: -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!--Optional permissions-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!--Version 2 of the Google Maps Android API requires OpenGL ES version 2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:label="#string/app_name"
android:icon="#drawable/ic_launcher">
<activity
android:name="com.example.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.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my api"/>
</application>
</manifest>
Seriously, this maps thing is driving me mad.
Check these things
Correct API Key and
in manifest
Google maps should be on in your devloper console
INTERNET PERMISSION in manifest
Updated google play services
Ok, I found a solution, pretty stupid in my opinion but it worked.
I had to declare the api key in the layout.xml
After that, everything worked like charm
Related
I know there are a many questions on this topic. I have went to all of those questions and tried a lot of suggestions given in stack overflow and at other sites. But unfortunately, none has solved my problem.
When I am using MapView and MapsActivity, grid is displayed instead of maps. As suggested, I have regenerated API key and I have made sure that I have given the same package name as of my application, SHA1 key and I have enabled the Google Maps ApI for Android in the Credentials. I have given all the permissions, that are needed for maps, in Manifestfile.xml, my manifest file is as shown below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.samplemaps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<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"/>
<permission
android:name="com.example.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>
<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=".MapsActivity"
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="AIzaSyClOHCG7SBYxg1o6YdUWhn03VSqWwE1y_g"/>
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
My layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.google.android.gms.maps.MapView android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
and my activity that extends MapActivity:
package com.example.mapsample;
import android.os.Bundle;
import com.google.android.maps.MapActivity;
public class GoogleMaps_Sample extends MapActivity{
public void onCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.googlemapssample);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
When I use the above activity, grids are displayed. When I extends FragmentActivity instead of MapAcitivity and fragment instead of mapview, I have been getting an error saying unfortunately stopped and some times it says some fragment class error.
I have also installed Google API system image for the emulator I am using. I have downloaded Google Play Services too. But still, maps is not displayed to me. I have done almost everything as suggested in other questions.
What is the error or mistake in my application that is preventing maps to get displayed and that displays grid instead.
Thanks in advance.
I know there are many post like this, but most of them are related to a wrong API key but this is not my case.
I know that the API key is OK, as if i change it i will get an authorization error. And from the API console, if i go to "Reports" i can see all my request.
I followed the official tutorial, i read it 10 times :
https://developers.google.com/maps/documentation/android/start
Something strange to be noticed:
When the fragment is display for the first time, the grid is blank. But if i turn off the screen, then turn it on (onResum) the map appears but it stuck, i can't move the map or use zoom controller)
And, if from my fragment, i center the map on a location, the map is centered. So forget about API key stuff.
Here is my manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.restoaparis"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="17" />
<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"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<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:icon="#drawable/icon" android:label="#string/app_name" android:theme="#android:style/Theme.Black.NoTitleBar" >
<activity android:name=".StartActivity"
android:label="#string/app_name"
android:theme="#style/FapiaoTheme" android:screenOrientation="portrait"
>
<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="MY_KEY"/>
</application>
Here is my layout :
<?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.SupportMapFragment"/>
Here is my fragment (as you can see i use Sherlock)
public class FragmentContact extends SherlockFragment
{
public void onActivityCreated(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.plan2 , container, false);
return view;
}
}
Of course, i got no error, no warning, from logcat.
I just got some debug message about memory , i don't think it has anything to do whith my problem.
/dev/pmem: Mapped buffer base:0x559da000 size:9539584 offset:7798784 fd:115
I add google-play-service as library of my eclipse project
Any idea ? please help me
i m nice guy, i deserve it
I've experienced similar problem and I solved my issue by adding onResume().
In my case I used MapView class.
#Override
public void onResume() {
mapView.onResume();
super.onResume();
}
You short of another meta-data element :)
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
Also one more thing. You are using SupportMapFragment which is to be used for API 11 and below. Since you have a minimumSDKversion of 11 and most of the phones these days are above API 11, you might want to consider converting this to MapFragment.
Do not put a fragment inside fragment via xml. This is not supported.
I am running sample code that is provided on Google Map Documentation for Google Map Api v2 (https://developers.google.com/maps/documentation/android/start#specify_settings_in_the_application_manifest).
The code run successfully but Map is not loaded on the device. Only white screen is shown on the device. I am using 4.0.3 version Android device.
1) I enabled service for the project.
2) Generated key:
3) "google-play-services_lib" as Library in project:
4) Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<permission
android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" />
<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" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCvZrhQ8YBfyYBvTic3DSdu2O06nAmhpg4" />
<activity
android:name="com.example.mapdemo.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>
5) Activity:
package com.example.mapdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
6) Layout:
<?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"/>
Use Fragment activity
eg:
public class Maps extends FragmentActivity {
GoogleMap map;
double lat;
double lan;
boolean flag = false;
// private LocationManager lm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapptry);
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
}
}
Change ur map.xml to
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
EDIT
Got to do this also
import android.support.v4.app.FragmentActivity;
Before you do this Right click project->properties->buildpath->java build path -> libraries .. then click on add external jars
the go to
user\android-sdks\extras\android\support\v4
and select android-support-v4.jar
PS: Do all this provided your API key is correct . If you API key is wrong then also it shows only a white screen
try to add
GoogleMap googleMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
and replace your xml with
<?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"
/>
you have added the following code outside application tag instead put it inside the tag
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<permission
android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
hope this might help someone who is facing the same problem
May be this problem with API key or you just check your SHA1 fingerprint in your project api console(https://console.developers.google.com/project) and your eclipse Windows -> Preferences -> Android -> Build are same, other wise just update the SHA1 from console to build path or vice versa.
Most probably your problem will be solved when you make sure below mentioned things:
Activate Google Map API
Use correct package name while generating API Key (in my case this was the mistake) .. and this seems problem to your case also as package name appears to be wrong (com.example.mapdemo)
Make Sure you are using the correct keystore file for generating the SHA1
Hope this will be helpful.
To maintain security for the system and users, Android requires apps to request permission before the apps can use certain system data and features. Depending on how sensitive the area is, the system may grant the permission automatically, or it may ask the user to approve the request.
https://developer.android.com/guide/topics/permissions/index.html
In case anyone else is as dumb as me... make sure the device you are testing on is connected to WiFi...
I followed two tutorials to create an android app that displays a google map. The activity launches but there is no map visible. The zoom buttons on the side are seen though. What do I do to see the map ?
The Tutorial I used for the project structure
The Tutorial I used for the activity code
This is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.application.ridingo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="com.application.ridingo.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.application.ridingo.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" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.application.ridingo.Select_Route"
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="ASDfrughiuniGDWUdhiWHdbi" />
</application>
</manifest>
The problem you describe usually happens when there is some problem with the way you defined Google Maps permissions (from a quick look at your manifest file the look OK) or from a bad configuration of the of the allowed API in the Google's API Console.
Take a look at this guide I wrote on how to configure Google Maps API V2 and make sure you do all the steps right:
Google Maps API V2
Make sure that you turn on the Google Maps V2 for Android in the console and not the one for web. another thing that looks strange in the manifest file you posted is your key (it looks too short - did you cut it in purpose) if you are not sure with the way you produced you key I would suggest you to go over this guide as well:
Google Maps API V2 key
try these steps. it worked for me.
change your main layout with this.
<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/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
change your Activity to fragment activity and follow this code
public class YourActivity extends FragmentActivity
//implements GoogleMap.InfoWindowAdapter
{
private GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = mapFragment.getMap();
googleMap.setMyLocationEnabled(true);
}
}
check your project properties file google play library reference must be their
target=android-10
android.library.reference.1=../google-play-services_lib
if library reference not than import library project from
yoursdkPath\sdk\extras\google\google_play_services
after you had import this library . configure this library into your project
During my studies i must develop an application base on google maps, user should found QRcode on the map and scan them.
So i create an android application including ggogle play lib and including the sdk library. i suppose i have created the right api key in my account google console whith tha SHA1 of my computeur.
class java
package com.example.mapa;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
#SuppressLint("NewApi")
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
int i=0;
GoogleMap map=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
}
}
class 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" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
Manifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapa"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_CORSE_LOCATION" />
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
<permission
android:name="com.example.mapa.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.mapa.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"/>
<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.example.mapa.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="my api key"/>
</application>
</manifest>
can every one can tel me why i have button on my map button i have a white screen all the time, with fragments or whithout, it's the same with a map view.
Maybe the while-loop is the problem? I don't see a change of i so this appears to be a infinite loop.
Try to get the reference to the GoogleMap in the onResume() method without a loop, like
GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
I think you got the this Image.
At the moment, referencing the Google Android Map Api v2 you can't run google maps v2 on Android emulator; you must use a device for your tests.
I'm not sure if you didn't update the "my api key" value with your api key on purpose.
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my api key"/>
And I don't remember seeing this in the docs, maybe you should remove it:
<uses-library android:name="com.google.android.maps"/>
Also COARSE_LOCATION is misspelled:
<uses-permission android:name="android.permission.ACCESS_CORSE_LOCATION" />
And you declared this twice. hehe. =)
<uses-permission android:name="android.permission.INTERNET"/>