MapView的使用 之 最新帖

在论坛里看到一篇 "MapView和其它控件一起显示 " 的帖子, 那是很老的一篇帖子了, 很多朋友都说无法在android SDK 1.0上运行。既然那么多人关心,我在这里就把它重写一遍,顺便加入了一些新的功能 ,感兴趣的朋友可以看看。

第一步,当然是增加map的支持了。在Android Manifest.xml中增加以下语句:
<uses-library android:name="com.google.android.maps" />

第二步, 传说中的Layout:
<?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/map"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:apiKey="Map_Demo"
                android:clickable="true" />

        <EditText android:layout_width="fill_parent"  
                  android:layout_height="wrap_content"  
                  android:text="输入查询地址"  
                  android:selectAll/>
</RelativeLayout>


然后, 创建一个MapViewActivity:
public class MapViewActivity extends MapActivity {  
    MapView mapView;
    MapController mapController;
    @Override  
    public void onCreate(Bundle icicle) {  
        super.onCreate(icicle);  
        setContentView(R.layout.main);

        mapView = (MapView) findViewById(R.id.map);
        mapController = mapView.getController();
        mapController.setZoom(15);
        updateView();
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }  


    private void updateView(){
         Double lat = 31.23717*1E6;
         Double lng = 121.50811*1E6;


         GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());
         mapController.setCenter(point);
    }
}


好了,你的MapView上面就多了一个EditText了。

接着,我希望在MapView中增加ZoomIn和ZoomOut的功能(鄙视一下Google ,缺省的MapView居然连这个功能都没有)
1. 在我们的Layout中增加一段:
        <LinearLayout android:id="@+id/zoom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true" />


2. 在onCreate函数中增加:
        ViewGroup zoom=(ViewGroup)findViewById(R.id.zoom);
        zoom.addView(mapView.getZoomControls());



现在在你的地图中点一下,屏幕左下角,是不是出现了一个Zoom Table? 这才是一个最基本的地图功能嘛。

附件是相应的源代码,不想敲键盘的同志,也可以直接import这个代码。
MapView.rar (24.02 KB)



下一步,我打算在上面实现更多的功能,先准备实现一个“做标记”的功能,即可以在自己感兴趣的点上,插上
一面小红旗 ,往后准备再实现一个在MapView上面画一段路经的功能,等实现后,我将尽快的把代码贴上来。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值