ACCESSandroid用户界面-组件Widget-地图视图MapView

一、在谷歌(Google)地图上出示本地的任务。

1、首先注目的在于AndroidManifest.xml文件中,扩充权限:

<uses-permission
android:name=”android.permission.ACCESS_FINE_LOCATION”></uses-permission>

在application中activity外,增加:uses-library

<uses-library android:name=”com.google.android.maps” /> 

 

 

2、在类中,将extends Activity改为extends MapActivity

3、设置可放大减弱地图的控件。

在xml文件中,添加以下内容:

    <LinearLayout android:orientation=”vertical”
        android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
        android:id=”@+id/zoom”
android:layout_alignParentBottom=”true”
        android:layout_centerHorizontal=”true” >
    </LinearLayout>

在类中,加入:

        LinearLayout zoomLayout =
(LinearLayout)findViewById(R.id.zoom); 
        View zoomView = mapView.getZoomControls();
        zoomLayout.addView(zoomView,
                new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
        mapView.displayZoomControls(true);

        mapView.getController().setZoom(14);//设置缩放级别
        p = this.getCurrentGeoPoint();
        mapView.getController().animateTo(p);//
通过动画情势移动到指定坐标s

4、通过以下方式赢得当前义务的经纬度

private GeoPoint getCurrentGeoPoint() {
        LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
        Location location = locationManager
               
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        return new GeoPoint((int) (location.getLatitude() * 1e6),
                (int) (location.getLongitude() * 1e6));
    }

5、通过以下办法展示当前职分在地图上浮现:

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);                  
            //—translate the GeoPoint to screen pixels—
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);
            //—add the marker—
            Bitmap bmp = BitmapFactory.decodeResource(
                getResources(), R.drawable.pushpin);           
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50,
null);        
            return true;
        }
    }

在oncreate方法中,加入:

MapOverlay mapOverlay = new MapOverlay();
      List<Overlay> listOfOverlays = mapView.getOverlays();
      listOfOverlays.clear();
      listOfOverlays.add(mapOverlay);

诸如此类就能在显示器中显示你眼前地点的地图彰显了;

如下图:

ACCESS 1

源代码见:http://henzil.googlecode.com/svn/trunk/android.googleMap01/

二、输入地方,在地形图上突显相应的职位。此方法是经过询问地址名称,重回一个list结果。在地图上体现:

此办法与上述办法基本一致,分化之处在于:是用getFromLocationName方法,来查询目标地的中纬度。

正如代码:

        // 通过系统默许区域设置开展地图定位
        Geocoder gc = new Geocoder(this);
        mapView.setStreetView(true);
        try {
            //
通过地点名称描述再次来到一个询问结果的数组(后边参数为回去最大结果数)
            addresses = gc.getFromLocationName(address, 5);

            // 如若未查询到其余结果
            if (addresses != null) {
                geoPoint = new GeoPoint(
                // 重临纬度,经度
                        (int) (addresses.get(0).getLatitude() * 1E6),
                        (int) (addresses.get(0).getLongitude() * 1E6));

                setTitle(addresses.get(0).getFeatureName());
                MyOverlay myOverlay = new MyOverlay();
                mapView.getOverlays().add(myOverlay);
                mapView.getController().setZoom(16);
                mapView.getController().animateTo(geoPoint);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

如下图所示:

ACCESS 2

相关文章