让map可点击和拖拽 Android:clickable="true"
获取得到的key android:apiKey="0HtSsgCEmiiTTfk-g0Oi59Wi2ndgjaMdoLSDPnw"
- <strong><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <com.google.android.maps.MapView
- android:id="@+id/mapview"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:clickable="true"
- android:apiKey="0HtSsgCEmiiTTfk-g0Oi59Wi2ndgjaMdoLSDPnw"
- />
- </LinearLayout></strong>
- <strong>package com.song;
- 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 com.google.android.maps.Projection;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Point;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- public class C7_GoogleMapActivity extends MapActivity {
- /** Called when the activity is first created. */
- MapView mapview;
- MapController controller;
- Location location;
- LocationManager manager;
- GeoPoint geoPoint;
- Bitmap bitmap;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mapview=(MapView)findViewById(R.id.mapview);
- manager=(LocationManager)getSystemService(LOCATION_SERVICE);
- location=manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
- bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.pos);//生成bitmap位图
- mapview.setBuiltInZoomControls(true);//实现放大缩小功能
- manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, new LocationListener() {
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onProviderEnabled(String provider) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onProviderDisabled(String provider) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onLocationChanged(Location location) {
- // TODO Auto-generated method stub
- getPoint(location);
- }
- });
- }
- public void getPoint(Location location)
- {
- controller=mapview.getController();
- //注意参数是纬度,经度。E6为10的6次方
- // GeoPoint geoPoint=new GeoPoint((int)(37.898989*1E6), (int)(122.989898*1E6));
- geoPoint=new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6));
- //定位到哪个点
- controller.animateTo(geoPoint);
- //获得悬浮图层
- List<Overlay> list = mapview.getOverlays();//获得MapView上原有的overlay对象
- list.clear();//清除所有的overlay对象
- list.add(new MyOverLay());//加新获取的overlay对象
- }
- class MyOverLay extends Overlay
- {
- //画标记的方法
- @Override
- public void draw(Canvas canvas, MapView mapView, boolean shadow) {
- // TODO Auto-generated method stub
- super.draw(canvas, mapView, shadow);
- Projection projection=mapView.getProjection();
- Point point=new Point();//定义输出的像素点
- projection.toPixels(geoPoint, point);//地理坐标转为像素坐标
- //绘制图片
- //bitmap, left, top, paint
- canvas.drawBitmap(bitmap, point.x-(bitmap.getWidth()/2), point.y-(bitmap.getHeight()), null);
- }
- }
- @Override
- protected boolean isRouteDisplayed() {
- // TODO Auto-generated method stub
- return false;
- }
- }</strong>
程序效果

