1

I have been working with Google Maps API v2 from the past 4 days but getting failed in displaying Google Maps but display only grey tiles. Eclipse don't show any error while running the app in emulator and on real device( connected to PC as emulator).The API key I have mentioned(in place of my api key) is the one I got by displaying the debug certificate fingerprint. Anyone Pls. help! Thanks in advance.

manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.VertexVerveInc.GPSLocator"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

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

<application
     android:allowBackup="true"
    android:icon="@drawable/icon" 
    android:label="@string/app_name">
    <uses-library android:name="com.google.android.maps"/>
    <activity
        android:name="GPSLocatorActivity"
        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>

main.xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical"
 android:layout_width="fill_parent"  
 android:layout_height="fill_parent">
<com.google.android.maps.MapView 
android:id="@+id/mapView"  
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:enabled="true" 
android:clickable="true" 
android:apiKey="my api key"
/>
</LinearLayout>

GPSLocatorActivity.java file

package com.VertexVerveInc.GPSLocator;


import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MapController;
import com.google.android.maps.GeoPoint;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

import android.location.Geocoder;
import android.location.Address;

import com.google.android.maps.Overlay;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.util.List;
import java.util.Locale;
import java.io.IOException;

import android.os.Bundle;
import android.widget.Toast;

public class GPSLocatorActivity extends MapActivity {

private MapView mapView;
private MapController mapController;

private LocationManager locationManager;
private LocationListener locationListener;

@SuppressWarnings( "deprecation" )

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

    locationListener = new GPSLocationListener();

    locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, 
        0, 
        0, 
        locationListener);

    mapView = (MapView) findViewById(R.id.mapView);

    // enable Street view by default
    mapView.setStreetView(true);

    // enable to show Satellite view
     mapView.setSatellite(true);

    // enable to show Traffic on map
     mapView.setTraffic(true);
    mapView.setBuiltInZoomControls(true);

    mapController = mapView.getController();
    mapController.setZoom(16); 
}

@Override
protected boolean isRouteDisplayed() {
    return false;
}

private class GPSLocationListener implements LocationListener 
{
    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            GeoPoint point = new GeoPoint(
                    (int) (location.getLatitude() * 1E6), 
                    (int) (location.getLongitude() * 1E6));

            /* Toast.makeText(getBaseContext(), 
                    "Latitude: " + location.getLatitude() + 
                    " Longitude: " + location.getLongitude(), 
                    Toast.LENGTH_SHORT).show();*/

            mapController.animateTo(point);
            mapController.setZoom(16);

            // add marker
            MapOverlay mapOverlay = new MapOverlay();
            mapOverlay.setPointToDraw(point);
            List<Overlay> listOfOverlays = mapView.getOverlays();
            listOfOverlays.clear();
            listOfOverlays.add(mapOverlay);

            String address = ConvertPointToLocation(point);
            Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();

            mapView.invalidate();
        }
    }

    public String ConvertPointToLocation(GeoPoint point) {   
        String address = "";
        Geocoder geoCoder = new Geocoder(
                getBaseContext(), Locale.getDefault());
        try {
            List<Address> addresses = geoCoder.getFromLocation(
                point.getLatitudeE6()  / 1E6, 
                point.getLongitudeE6() / 1E6, 1);

            if (addresses.size() > 0) {
           for (int index = 0; index <                                   addresses.get(0).getMaxAddressLineIndex(); index++)
                    address += addresses.get(0).getAddressLine(index) + " ";
            }
        }
        catch (IOException e) {                
            e.printStackTrace();
        }   

        return address;
    } 

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}

class MapOverlay extends Overlay
{
    private GeoPoint pointToDraw;

    public void setPointToDraw(GeoPoint point) {
        pointToDraw = point;
    }

    public GeoPoint getPointToDraw() {
        return pointToDraw;
    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
        super.draw(canvas, mapView, shadow);                   

        // convert point to pixels
        Point screenPts = new Point();
        mapView.getProjection().toPixels(pointToDraw, screenPts);

        // add marker
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.red);
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null); // 24 is the      height of image        
        return true;
    }
} 

}

Kara
  • 6,115
  • 16
  • 50
  • 57
user2439492
  • 91
  • 1
  • 2
  • 8

5 Answers5

4

If you use signed apk, debug key export means,the map looks blank in real device. You are using unsigned apk, ie debug key. if you export apk, use release key.

https://developers.google.com/maps/documentation/android/start#the_google_maps_api_key

Release certificate finger print is given.

And some permissions are missing in manifest.please add that.

    <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />
    <permission
    android:name="com.VertexVerveInc.GPSLocator.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.VertexVerveInc.GPSLocator.permission.MAPS_RECEIVE" />

And in layout,you are using map v1, switch over to map v2.

      <fragment  
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
       android:layout_below="@id/rg_views"
      android:name="com.google.android.gms.maps.SupportMapFragment"
      />

http://www.vogella.com/articles/AndroidGoogleMaps/article.html

Shadow
  • 6,864
  • 6
  • 44
  • 93
  • okay...thanks...Do I need to make any changes in **GPSLocatorActivity.java** file also? – user2439492 Jun 06 '13 at 08:52
  • Dont change it everytime.think before which answer to choose and then decide which will be an apt one for you.and yes everything will gets changed.overlay all gets depricated and to get current location, there is a class called as LocationManager. locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this); //You can also use LocationManager.GPS_PROVIDER and LocationManager.PASSIVE_PROVIDER @user2439492 – Shadow Jun 06 '13 at 09:04
1

Use below Permissions:

    <uses-permission android:name="android.permission.INTERNET"/ 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

check your api key is right or not.

Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
1

First you cross check on device. You can just add your apk file on device.

If same problem persists then it is issue with your API keys only. Your API keys with debug signed certificate

The procedure for generating API keys is https://developers.google.com/maps/documentation/android/start#the_google_maps_api_key

chaitanya
  • 1,726
  • 2
  • 17
  • 13
1

In order to show the map you have to generate two v2 API keys: one debug key (for debugging in Eclipse during the development) and one release key (for publishing the app).
Now if you are testing the app yourself you need a debug key.

Umberto
  • 2,011
  • 1
  • 23
  • 27
1

The map that you're using com.google.android.maps.MapView is a v1 of Google Map API. Unfortunately, it is officially deprecated.

You should use the new version of Google Maps API which is Version 2 .

Here it is good example how to use Google Maps API v2 .

Hope this helps :-)

AndroidLearner
  • 4,500
  • 4
  • 31
  • 62