<uses-library android:name="com.google.android.maps" /> </application> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-sdk android:minSdkVersion="3" /> </manifest>
mapview.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.google.android.maps.MapView android:id="@+id/mapview_mv" android:layout_width="fill_parent" android:layout_height="fill_parent" android:enabled="true" android:clickable="true" android:apiKey="0HYDh8uGZ3WWc58LxmauEVsNWqAGP_HSGFRzixQ" /> <LinearLayout android:id="@+id/mapview_llayout_zoom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" /> <RelativeLayout android:id="@+id/mapview_rlayout_search" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@color/darkgray" android:visibility="invisible" android:paddingTop="8dip" android:paddingBottom="4dip" android:paddingLeft="8dip" android:paddingRight="8dip"> <AutoCompleteTextView android:id="@+id/mapview_autotxtview_search" android:layout_width="245dip" android:layout_height="wrap_content" android:text="@string/edt_search_default" android:textColor="@color/darkgray" android:layout_marginRight="6dip" /> <ImageButton android:id="@+id/mapview_imgbtn_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/ic_search_category_default" android:layout_toRightOf="@id/mapview_autotxtview_search" android:layout_alignBottom="@id/mapview_autotxtview_search" /> </RelativeLayout> </RelativeLayout>
public class ActivityMap extends MapActivity { private MapView mMapView; private GeoPoint gp; private LocationManager mLocMgr; public static Location mLoc; private String strLocInfo; private final LocationListener mLocListener = new LocationListener() { public void onLocationChanged(Location location) { refreshMapView(location); } public void onProviderDisabled(String provider) { } public void onProviderEnabled(String provider) { } public void onStatusChanged(String provider, int status, Bundle extras) { } }; protected void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.mapview); mMapView = (MapView) findViewById(R.id.mapview_mv); mLocMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE); updateLocation(mLocMgr); refreshMapView(mLoc); // mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, mLocListener); mLocMgr.requestLocationUpdates(strLocInfo, 2000, 10, mLocListener); // zoom widget LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.mapview_llayout_zoom); View zoomView = mMapView.getZoomControls(); zoomLayout.addView(zoomView, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); // mMapView.displayZoomControls(true); // mMapView.setBuiltInZoomControls(true); // mMapView.displayZoomControls(false); } private void refreshMapView(Location location) { updateGeoPoint(location); MapController mMapController = mMapView.getController(); mMapController.animateTo(gp); mMapController.setZoom(mMapView.getZoomLevel()); try { Log.e("************", getAddress(gp)); } catch (IOException e) { e.printStackTrace(); } } private void updateLocation(LocationManager lm) { Criteria mCriteria = new Criteria(); mCriteria.setAccuracy(Criteria.ACCURACY_FINE); // 经纬度是否精确提供 mCriteria.setAltitudeRequired(false); // 是否提供高度信息 mCriteria.setBearingRequired(false); // 是否提供航向信息 mCriteria.setCostAllowed(true); // 费用 mCriteria.setPowerRequirement(Criteria.POWER_LOW); strLocInfo = lm.getBestProvider(mCriteria, true); mLoc = lm.getLastKnownLocation(strLocInfo); if (mLoc == null) { mLoc = new Location(strLocInfo); mLoc.setLatitude(31.183333); mLoc.setLongitude(121.483333); } } private void updateGeoPoint(Location location) { double dLatitude = location.getLatitude() * 1E6; double dLongitude = location.getLongitude() * 1E6; gp = new GeoPoint( (int) (dLatitude), (int) (dLongitude) ); } private String getAddress(GeoPoint gp) throws IOException { double dLatitude = gp.getLatitudeE6() / 1E6; double dLongitude = gp.getLongitudeE6() / 1E6; //自经纬度取得地址 StringBuilder sb = new StringBuilder(); Geocoder gc = new Geocoder(ActivityMap.this, Locale.getDefault()); List<Address> lstAddr = gc.getFromLocation(dLatitude, dLongitude, 1); if (lstAddr.size() > 0) { Address addr = lstAddr.get(0); for (int i = 0; i < addr.getMaxAddressLineIndex(); i++) { sb.append(addr.getAddressLine(i)).append("\n"); } sb.append(addr.getLocality()).append("\n"); //所在地 sb.append(addr.getPostalCode()).append("\n"); //邮编 sb.append(addr.getCountryName()); } return sb.toString(); } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.optionsmenu, menu); return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.optionsmenu_search: Intent intent = new Intent(); intent.setClass(ActivityMap.this, ActivitySearch.class); startActivity(intent); break; case R.id.optionsmenu_directions: break; case R.id.optionsmenu_mapmode: break; case R.id.optionsmenu_mapmode_map: mMapView.setStreetView(true); mMapView.setSatellite(false); mMapView.setTraffic(false); break; case R.id.optionsmenu_mapmode_satellite: mMapView.setStreetView(false); mMapView.setSatellite(true); mMapView.setTraffic(false); break; case R.id.optionsmenu_mapmode_traffic: mMapView.setStreetView(false); mMapView.setSatellite(false); mMapView.setTraffic(true); break; case R.id.optionsmenu_myloc: MapOverlay mMapOverlay = new MapOverlay(); List<Overlay> listOfOverlay = mMapView.getOverlays(); listOfOverlay.clear(); listOfOverlay.add(mMapOverlay); mMapView.invalidate(); updateLocation(mLocMgr); refreshMapView(mLoc); break; } return super.onOptionsItemSelected(item); } protected boolean isRouteDisplayed() { return false; } private class MapOverlay extends com.google.android.maps.Overlay { public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { super.draw(canvas, mapView, shadow); // translate the GeoPoint to screen pixels Point screenPts = new Point(); mapView.getProjection().toPixels(gp, screenPts); // add the marker Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_android); canvas.drawBitmap(bmp, screenPts.x, screenPts.y, null); return true; } } }
public class ActivitySearch extends MapActivity { private MapView mMapView; public static int screen_width, screen_height; protected void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.mapview); screen_width = getWindowManager().getDefaultDisplay().getWidth(); screen_height = getWindowManager().getDefaultDisplay().getHeight(); mMapView = (MapView) findViewById(R.id.mapview_mv); MapOverlay mMapOverlay = new MapOverlay(); List<Overlay> listOfOverlay = mMapView.getOverlays(); listOfOverlay.clear(); listOfOverlay.add(mMapOverlay); mMapView.invalidate(); RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.mapview_rlayout_search); rLayout.setVisibility(View.VISIBLE); ImageButton btn_search = (ImageButton) findViewById(R.id.mapview_imgbtn_search); btn_search.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { } }); mMapView.setEnabled(false); } protected boolean isRouteDisplayed() { return false; } private class MapOverlay extends com.google.android.maps.Overlay { public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { super.draw(canvas, mapView, shadow); Paint innerPaint = new Paint(); Paint borderPaint = new Paint(); innerPaint.setARGB(75, 80, 80, 80); // darkgray borderPaint.setARGB(255, 255, 255, 255); // white borderPaint.setAntiAlias(true); // 抗锯齿 borderPaint.setStyle(Style.STROKE); //描边,和Style.Fill相对 borderPaint.setStrokeWidth(2); RectF drawRect = new RectF(); drawRect.set(0,0, screen_width, screen_height); canvas.drawRoundRect(drawRect, 5, 5, innerPaint); canvas.drawRoundRect(drawRect, 5, 5, borderPaint); return true; } } }
22