地图没有加载

时间:2012-01-19 16:35:43

标签: android google-maps

我有一个简单的应用程序,应该在地图上显示设备的位置。该应用程序似乎与地图分开不加载,而是灰色网格。我做了一些日志记录并查看了栈跟踪。它提到MapActivity无法获得连接工厂客户端。 我已经看了几个线程,但我找不到答案。

一旦应用程序运行,它就会无休止地在onDraw和onLocationChanged之间循环。我已经创建了一个MapView apiKey,清单中也有正确的权限。任何想法我能做什么?感谢。

[编辑]我也在外面走来走去,因为gps no in building:)

[edit2]我还尝试重新生成另一个md5指纹以生成另一个apikey,但keytool生成相同的md5指纹。它的别名已经存在。

[edit3]我还通过在keytool中使用不同的别名生成了不同的MD5指纹。这产生了一个不同的API KEY,遗憾的是它仍然不起作用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical" android:layout_width="match_parent" 
                android:layout_height="wrap_content" android:gravity="fill">
   <com.google.android.maps.MapView
        android:id="@+id/myGMap"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="0??????syQI//C13Y-pRUNaA0a_??????pr86jE2w"
     />

   <EditText android:id="@+id/edittext1" 
             android:inputType="text"
             android:layout_width="fill_parent" 
             android:layout_height="wrap_content" 
             />

   <Button android:layout_height="wrap_content" 
          android:layout_width="wrap_content" 
          android:text="CLOSE"
          android:layout_alignParentRight="true"  
          android:id="@+id/close" ></Button> 

</RelativeLayout>

<?xml version="1.0" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tecmark"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />
     <uses-permission android:name="android.permission.INTERNET"></uses-permission>

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <activity
            android:name=".WcFinderActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <uses-library android:name="com.google.android.maps" />
    </application>



</manifest>

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ZoomControls;

public class WcFinderActivity extends MapActivity implements LocationListener {
    /** Called when the activity is first created. */



    private MapView         gMapView        = null;
    private MapController   mc              = null;
    private Drawable        defaultMarker   = null;
    private GeoPoint        p               = null;
    private double          latitude        = 18.9599990845;
    private double          longitude = 72.819999694;
    private Context         mContext = this;

    private static final String TAG = "WcFinderActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.e(TAG, "**********inside oncreate");

        EditText editText = (EditText)findViewById(R.id.edittext1);


        // Creating and initializing Map
        gMapView = (MapView) findViewById(R.id.myGMap);
        p = new GeoPoint((int) (latitude * 1000000), (int) (longitude * 1000000));
        gMapView.setSatellite(true);
        mc = gMapView.getController();
        mc.setCenter(p);
        mc.setZoom(14);

        // Add a location mark
        MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
        List<Overlay> list = gMapView.getOverlays();
        list.add(myLocationOverlay);

        // Adding zoom controls to Map
        ZoomControls zoomControls = (ZoomControls) gMapView.getZoomControls();
        zoomControls.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));

        gMapView.addView(zoomControls);
        gMapView.displayZoomControls(true);

        // Getting locationManager and reflecting changes over map if distance travel by
        // user is greater than 500m from current location.
        Log.e(TAG, "**********about to call getSystemService");
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Log.e(TAG, "**********about to call requestLocationUpdates");
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);



        Button close = (Button)findViewById(R.id.close);
        close.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Log.e(TAG,"clicked close");

            finish();

            }});

    }

    /* This method is called when use position will get changed */
    public void onLocationChanged(Location location) {
        if (location != null) {
            Log.e(TAG, "**********inside onLocationChanged");
            double lat = location.getLatitude();
            double lng = location.getLongitude();

            p = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
            mc.animateTo(p);
        }
    }

    public void onProviderDisabled(String provider) {
        // required for interface, not used

        Log.e(TAG, "**********inside onProviderDisabled");
    }

    public void onProviderEnabled(String provider) {
        // required for interface, not used
        Log.e(TAG, "**********inside onProviderEnabled");
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // required for interface, not used
        Log.e(TAG, "**********inside onStatusChanged");
    }

    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        Log.e(TAG, "**********inside isRouteDisplayed");
        return false;
    }

    /* User can zoom in/out using keys provided on keypad */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_I) {
            gMapView.getController().setZoom(gMapView.getZoomLevel() + 1);
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_O) {
            gMapView.getController().setZoom(gMapView.getZoomLevel() - 1);
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_S) {
            gMapView.setSatellite(true);
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_T) {
            gMapView.setTraffic(true);
            return true;
        }
        return false;
    }

    /* Class overload draw method which actually plot a marker,text etc. on Map */
    protected class MyLocationOverlay extends com.google.android.maps.Overlay {

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

            super.draw(canvas, mapView, shadow);
            Paint paint = new Paint();
            Log.e(TAG, "**********inside onDraw");
            // Converts lat/lng-Point to OUR coordinates on the screen.
            Point myScreenCoords = new Point();
            mapView.getProjection().toPixels(p, myScreenCoords);

            paint.setStrokeWidth(1);
            paint.setARGB(255, 255, 255, 255);
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.RED);


            canvas.drawCircle(myScreenCoords.x, myScreenCoords.y, 20, paint);
            canvas.drawText("I am here...", myScreenCoords.x, myScreenCoords.y, paint);
            return true;
        }
    }
}

01-19 16:13:59.698: D/ddm-heap(2294): Got feature list request
01-19 16:14:00.893: E/WcFinderActivity(2294): **********inside oncreate
01-19 16:14:01.073: E/WcFinderActivity(2294): **********about to call getSystemService
01-19 16:14:01.083: D/LocationManager(2294): Constructor: service = android.location.ILocationManager$Stub$Proxy@43cf74d8
01-19 16:14:01.083: E/WcFinderActivity(2294): **********about to call requestLocationUpdates
01-19 16:14:01.398: I/MapActivity(2294): Handling network change notification:CONNECTED
01-19 16:14:01.398: E/MapActivity(2294): Couldn't get connection factory client
01-19 16:14:01.438: E/WcFinderActivity(2294): **********inside isRouteDisplayed
01-19 16:14:01.613: D/dalvikvm(2294): GC freed 3423 objects / 218896 bytes in 81ms
01-19 16:14:02.003: D/dalvikvm(2294): GC freed 6408 objects / 380712 bytes in 87ms
01-19 16:14:02.283: E/WcFinderActivity(2294): **********inside onDraw
01-19 16:14:02.378: D/dalvikvm(2294): GC freed 10136 objects / 744376 bytes in 77ms
01-19 16:14:02.383: E/WcFinderActivity(2294): **********inside onDraw
01-19 16:14:02.423: E/WcFinderActivity(2294): **********inside onLocationChanged
01-19 16:14:02.448: E/WcFinderActivity(2294): **********inside onLocationChanged
01-19 16:14:02.493: E/WcFinderActivity(2294): **********inside isRouteDisplayed
01-19 16:14:02.523: E/WcFinderActivity(2294): **********inside onDraw
01-19 16:14:02.528: E/WcFinderActivity(2294): **********inside onDraw
01-19 16:14:02.578: E/WcFinderActivity(2294): **********inside isRouteDisplayed
01-19 16:14:02.603: E/WcFinderActivity(2294): **********inside onDraw

2 个答案:

答案 0 :(得分:0)

您是否获得了调试证书的api密钥?如果没有,那那就是你的问题。

尝试阅读this文章,了解如何做到这一点

答案 1 :(得分:0)

这就是我所做的。

keytool -list -alias androiddebugkey -keystore "c:\Users\matt\debug.keystore" -storepass android -keypass android