在看到几个论坛和 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"/>
它运行,但地图没有出现在屏幕上,有什么建议吗?