I integrate google map v2 in my android app. it show blank screen with zoom button and google logo. its saved instance is also null. I signed my app with debug certificate now but at fist i signed with release.store. before it was show error like not contact with google server. when i signed app with
debug.store it shows response in adb like
map = [email protected]
aid = com.[email protected]
aic = null
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bilawalkhan.pakistanmap" >
<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"/>
<!-- 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:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Map"
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="AIzaSyCLgSPDsNfVrGFPO2636kWMXIAaZu2f43p"/>
</application>
</manifest>
Main_Activity is
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
// Get a handle to the Map Fragment
GoogleMap map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
LatLng sydney = new LatLng(-33.867, 151.206);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
map.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}
map.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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".Map">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
its debugger shows like this.
map = [email protected]
aid = com.google.android.g[email protected]
>aic = null
Logcat shows:
10-29 18:40:44.200 3621-3806/com.bilawalkhan.maptesting D/REQUEST﹕ Connection opened to:https://clients4.google.com/glm/mmap/api
10-29 18:40:44.200 3621-3806/com.bilawalkhan.maptesting D/REQUEST﹕ Open Connection
10-29 18:40:45.640 1958-2182/com.bluestacks.gamepophome D/[InMobi]-[Analytics]-4.4.3﹕ Http Status Code: 200
10-29 18:40:45.640 1958-2182/com.bluestacks.gamepophome D/[InMobi]-[Analytics]-4.4.3﹕ IMAppDatabaseManager->deleteEvents
10-29 18:40:45.640 1958-2182/com.bluestacks.gamepophome D/[InMobi]-[Analytics]-4.4.3﹕ Ping Response: {"success":true}
10-29 18:40:45.700 3621-3806/com.bilawalkhan.maptesting D/REQUEST﹕ Processing DataRequest: [email protected]
10-29 18:40:45.720 3621-3806/com.bilawalkhan.maptesting D/REQUEST﹕ Processing DataRequest: [email protected]
10-29 18:40:45.730 3621-3806/com.bilawalkhan.maptesting D/REQUEST﹕ Processing DataRequest: [email protected]
10-29 18:40:45.820 3621-3806/com.bilawalkhan.maptesting D/REQUEST﹕ Processing DataRequest: [email protected]
10-29 18:40:45.820 3621-3795/com.bilawalkhan.maptesting D/USER_EVENTS﹕ Event: type=22, status: fb, data: 1510
10-29 18:40:45.830 3621-3795/com.bilawalkhan.maptesting D/USER_EVENTS﹕ Event: type=22, status: lb, data: 1630
10-29 18:40:45.830 3621-3795/com.bilawalkhan.maptesting D/USER_EVENTS﹕ Event: type=22, status: flbs, data: fb=1510|lb=1630|s=1100
10-29 18:40:45.830 3621-3806/com.bilawalkhan.maptesting D/REQUEST﹕ DRD(0): 62|75|75|147, 1s, 1kb
10-29 18:40:45.830 3621-3806/com.bilawalkhan.maptesting D/REQUEST﹕ Close
10-29 18:41:08.790 1898-2812/com.bluestacks.bstfolder D/dalvikvm﹕ GC_FOR_ALLOC freed 606K, 24% free 2686K/3508K, paused 10ms, total 10ms
10-29 18:41:15.690 1958-2182/com.bluestacks.gamepophome D/[InMobi]-[Analytics]-4.4.3﹕ NetworkManager->handleMessag: msg:{ when=-40ms what=1001 target=android.os.Handler }
10-29 18:41:15.700 1958-2182/com.bluestacks.gamepophome D/[InMobi]-[Analytics]-4.4.3﹕ IMAppDatabaseManager->getEvents
10-29 18:41:15.710 1958-2182/com.bluestacks.gamepophome D/[InMobi]-[Analytics]-4.4.3﹕ PayloadBuilder->getPayloadList:
10-29 18:41:53.870 1898-2812/com.bluestacks.bstfolder D/dalvikvm﹕ GC_FOR_ALLOC freed 606K, 24% free 2678K/3508K, paused 0ms, total 0m
Try like this:
layout.xml
<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:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
// Google Map
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
try {
// Loading map
initilizeMap();
// Changing map type
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
// googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
// googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
// Showing / hiding your current location
googleMap.setMyLocationEnabled(true);
// Enable / Disable zooming controls
googleMap.getUiSettings().setZoomControlsEnabled(false);
// Enable / Disable my location button
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
// Enable / Disable Compass icon
googleMap.getUiSettings().setCompassEnabled(true);
// Enable / Disable Rotate gesture
googleMap.getUiSettings().setRotateGesturesEnabled(true);
// Enable / Disable zooming functionality
googleMap.getUiSettings().setZoomGesturesEnabled(true);
double latitude = 21.3235752;
double longitude =74.1881265;
// lets place some 10 random markers
// random latitude and logitude
double[] randomLocation = createRandLocation(latitude,
longitude);
// Adding a marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(randomLocation[0], randomLocation[1]))
.title(" Mapview Test ");
//marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
googleMap.addMarker(marker);
// Move the camera to last position with a zoom level
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(randomLocation[0],
randomLocation[1])).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
/**
* function to load map If map is not created it will create it for you
* */
@SuppressLint("NewApi")
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
/*
* creating random postion around a location for testing purpose only
*/
private double[] createRandLocation(double latitude, double longitude) {
return new double[] { latitude + ((Math.random() - 0.5) / 500),
longitude + ((Math.random() - 0.5) / 500),
150 + ((Math.random() - 0.5) * 10) };
}
}
Manifest Permissions and API
<permission
android:name="com.example.mapexdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- meta-data before </application> tag -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDxEkfvGvKy.........your api key" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />