0

在看到几个论坛和 Android 版谷歌地图教程后,我有了这个

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;
}

}

根据谷歌开发者的说法,我修改了这样的清单

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

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

4

2 回答 2

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并继续开发

于 2013-01-11T11:38:42.330 回答
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/

于 2013-01-11T11:43:50.390 回答