谷歌地图不会出现在屏幕上

时间:2013-01-11 11:25:22

标签: android google-maps-android-api-2 android-maps-v2

在看到Android的Google地图的几个论坛和教程之后,我就有了这个

public class MapsActivity extends MapActivity {

MapView mapView;
MapController mc;
GeoPoint p;

class MapOverlay extends com.google.android.maps.Overlay{
    @Override
    public boolean draw(Canvas canvas, MapView mapView,
            boolean shadow, long when){
        super.draw(canvas, mapView,shadow);
        Point screenPts = new Point();
        mapView.getProjection().toPixels(p,screenPts);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
        return true;
    }

    public boolean onTouchEvent(MotionEvent event, MapView mapView){
        if(event.getAction()==1) {
            p=mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY());
            Toast.makeText(getBaseContext(),
            p.getLatitudeE6()/1E6 + ","+p.getLongitudeE6()/1E6,
            Toast.LENGTH_SHORT).show();
            mc.animateTo(p);
            return false;
        } else return false;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    mapView=(MapView) findViewById(R.id.mapView);
    LinearLayout zoomLayout=(LinearLayout)findViewById(R.id.zoom);
    @SuppressWarnings("deprecation")
    View zoomView=mapView.getZoomControls();
    zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    mapView.displayZoomControls(true);
    mc=mapView.getController();
    String coordinates[]={"51.986367","5.666304"};
    double lat=Double.parseDouble(coordinates[0]);
    double lng=Double.parseDouble(coordinates[1]);
    p=new GeoPoint((int)(lat*1E6),(int)(lng*1E6));
    mc.animateTo(p);
    mc.setZoom(18);
    MyLocationOverlay mMyLocationOverlay=new MyLocationOverlay(this,mapView);
    mMyLocationOverlay.enableMyLocation();
    mapView.getOverlays().add(mMyLocationOverlay);
    mapView.invalidate();
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

}

根据Google开发人员的说法,我修改了这样的清单

 <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"/> 
 <permission 
    android:name="tovar.testing.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
<uses-permission android:name="tovar.testing.permission.MAPS_RECEIVE"/>
<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" >
    <uses-library android:name="com.google.android.maps"/>
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyAYLFPye3YL3HgXnPHGuoDCgYjrGJ4VtkA"/>
    <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>
</application>

我还将API键添加到布局

<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:apiKey="AIzaSyAYLFPye3YL3HgXnPHGuoDCgYjrGJ4VtkA"
    android:clickable="true"
    android:enabled="true" 
    class="com.google.android.gms.maps.MapFragment"/>

它运行,但是地图没有出现在屏幕上,有什么建议吗?

2 个答案:

答案 0 :(得分:0)

嘿,为什么你的清单文件中需要以下内容

<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"/> 
 <permission 
    android:name="tovar.testing.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
<uses-permission android:name="tovar.testing.permission.MAPS_RECEIVE"/>
<uses-feature 
    android:glEsVersion="0x00020000"
    android:required="true"/>    

您已在Maps V1中编写了所有代码,并尝试添加Maps V2所需的权限。如果要构建Maps Activity V2,则完全省略V1。

更重要的是,你的MapView标签是一个非常大的错误

没有这样的标签接受class =“..... MapFragment”。

我建议您只查看Maps V2并继续开发

答案 1 :(得分:0)

您的代码可能需要更改的内容很少:

1)您的类Maps Activity应该扩展FragmentActivity

2)你的xml代码应该是这样的

    <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"/>

3)您的清单文件应该重新检查此信息:

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="YOUR_API_KEY"/>

帮助链接:http://wptrafficanalyzer.in/blog/google-maps-in-android-application-with-new-google-maps-android-api-v2-using-supportmapfragment/

http://wptrafficanalyzer.in/blog/showing-current-location-in-google-maps-using-api-v2-with-supportmapfragment/