Android Google Map实例 - 创建一个Google Map例程(Android mapview)

    技术2022-05-19  28

    上篇:Android Google Map应用实例 - MD5 、Google Map API Key获取(Android mapview)

    创建一个GoogleMap地图View

     

    新建一个工程 HelloGoogleMaps打开 AndroidManifest.xml 在 <application> 元素下添加: <uses-library android:name="com.google.android.maps" />在 <manifest> 元素下添加如下语句来取得联网许可: <uses-permission android:name="android.permission.INTERNET" />打开 res/layout/main.xml file 将文件改为: <?xml version="1.0" encoding="utf-8"?> <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:clickable="true"     android:apiKey="这里填入 Google Map API Key " />

    如何取得Google Map API Key 请参阅: http://blog.csdn.net/dengta_snowwhite/archive/2011/03/04/6223251.aspx

    打开 HelloGoogleMaps.java . For this Activity, extend MapActivity (instead of android.app.Activity): public class HelloGoogleMaps extends MapActivity {重写isRouteDisplayed函数: @Override protected boolean isRouteDisplayed() {     return false; } 在HelloGoogleMaps 类中添加 onCreate() : @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main); }添加 import com.google.android.maps.*; 装载main.xml文件定义的layout file.

    如果需要添加地图缩放功能在 onCreate() 下添加:     MapView mapView = (MapView)findViewById(R.id.mapview);     mapView.setBuiltInZoomControls(true);

    运行即可得到地图应用,默认显示美洲大陆

    可以在HelloGoogleMaps类中添加:

            MapController mymapController =mapView.getController();

            Double lng = 121.512974 * 1E6;

            Double lat =  29.578181 * 1E6;

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

            mymapController.setCenter(point);//显示中心

    即可得到想显示的中心如下:

     

    下篇:

    Android Google Map应用实例 - 添加Google Map自定义图层(Android mapview)

    转载本站文章请注明,转载自:ADASiteMap的空间[http://www.adasitemap.tk]

    本文链接:Android Google Map实例 – 创建一个Google Map例程(Android mapview) | ADASiteMap的空间


    最新回复(0)