android google map 的简单使用

    技术2024-04-20  8

    Google map 是android platform中众多应用程序中的一员。

    在移动开发中有两种方法使用google map。

    第一种是使用web版本的map.实际实现是通过发送链接google 地图网站的intent,启动浏览器访问googlemap.没多大用处。

    注意:使用此方法需在AndroidManifest.xml中加入相应的访问权限<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" />

    第二种就是使用google map jar 包的开发。通过创建MapActivity,并使用MapView来呈现地图。

    注意:使用此方法需在AndroidManifest.xml中加入相应的访问权限

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

    GoogleMap library:

    1)MapView:

    Mapview是用来显示地图的view。当MapView获得焦点,可以控制地图的移动和缩放。

     

    地图可以以不同的形式来显示出来,如街景模式,卫星模式等,通过setSatellite(boolean)  setTraffic(boolean), setStreetView(boolean) 方法。

    MapView只能被MapActivity来创建,这是因为mapview需要通过后台的线程来连接网络或者文件系统,而这些线程要由mapActivity来管理。

    需要特别说明的一点是,从android 1.5开始,map的zoom采用了built-in机制,可以通过setBuiltInZoomControls(boolean)来设置是否在地图上显示zoom控件。

    常用方法:getController()  getOverlays()  setSatellite(boolean)  setTraffic(boolean),   setStreetView(boolean)  setBuiltInZoomControls(boolean) 等。

    2)MapActivity:

    MapActivity是一个抽象类,任何想要显示MapView的activity都需要派生自MapActivity。并且必须在其派生类的 onCreate()中,创建一个MapView实例,可以通过new MapView 或者通过layout XML来创建。3)MapController控制地图移动,伸缩,以某个GPS坐标为中心,控制MapView中的view,管理Overlay,提供View的基本功能。使用多种地图模式(地图模式(某些城市可实时对交通状况进行更新),卫星模式,街景模式)来查看Google Map。常用方法:animateTo(GeoPoint point)  setCenter(GeoPoint point)  setZoom(int zoomLevel) 等。

    4)Overlay

    Overlay是覆盖到MapView的最上层,可以扩展其ondraw()接口,自定义在MapView中显示一些自己的东西。MapView通过MapView.getOverlays()对Overlay进行管理。

    除了Overlay这个基类,Google还扩展了2个比较有用的Overlay

    1.MylocationOverlay—集成了Android.location中接收当前坐标的接口,集成SersorManager中CompassSensor的接口

    我们只需要enableMyLocation(),enableCompass就可以让我们的程序拥有实时的MyLocation以及Compass 功能(Activity.onResume()中)。

    2.ItemlizedOverlay—管理一个OverlayItem链表,用图片等资源在地图上作风格相同的标记。

    5)Projection:MapView中GPS坐标与设备坐标的转换(GeoPoint和Point)。另外:

    android.location包下的Geocoder类可以实现街道地址与经纬度之间的转换。

     

    效果图:

    注:代理问题不能展示地图。实际可以。

     

    XML Layout:

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#EEEEEE" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:id="@+id/topL" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:gravity="center" android:paddingLeft="10dip" > <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" > <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:text="@string/storename" android:width="90dip" android:textColor="#000000" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:textColor="#000000" android:id="@+id/stoname" android:text="Carrefour" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:text="@string/tel" android:textColor="#000000" android:width="90dip" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tell" android:textColor="#000000" android:text="021-88888888" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="80dip" android:paddingRight="5dip" android:gravity="right" > <ImageView android:id="@+id/image" android:src="@drawable/carrefour" android:layout_width="80dip" android:layout_height="70dip" android:paddingRight="5dip" /> </LinearLayout> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="10dip" > <TextView android:text="@string/address" android:textColor="#000000" android:width="90dip" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/addre" android:text="No. 2860,Central Gaoke Road" android:textColor="#000000" android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="false" android:maxLines="3" android:paddingRight="15dip" /> </LinearLayout> <LinearLayout android:id="@+id/mediumL" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="260dip" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:layout_marginTop="10dip" android:layout_marginBottom="10dip" android:gravity="center" > <com.google.android.maps.MapView android:id="@+id/map" android:layout_width="fill_parent" android:layout_height="fill_parent" android:enabled="true" android:clickable="true" android:apiKey="@string/MachaelMapKey" /> </LinearLayout> <LinearLayout android:id="@+id/BottomL" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:paddingBottom="5dip" > <Button android:id ="@+id/btnOK" android:text="OK" android:layout_width="150dip" android:layout_height="35dip" /> <Button android:id ="@+id/btnUpdate" android:text="UPDATE" android:layout_width="150dip" android:layout_height="35dip" /> </LinearLayout> </LinearLayout>

     

    源代码:package com.hzsi; import java.util.ArrayList; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.graphics.drawable.Drawable; import android.location.Location; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.google.android.maps.GeoPoint; import com.google.android.maps.ItemizedOverlay; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.android.maps.MyLocationOverlay; import com.google.android.maps.OverlayItem; import com.hzsi.util.ConstantUtil; public class StoreMapInfo extends MapActivity { //log tag private static final String TAG = "StoreMapInfo"; private MapView map = null; private MapController mapCon = null; private Button btnOK, btnUpdate = null; private TextView TVstore, TVTel, TVaddress; private ImageView image = null; private String city = "No city", storeName = "No store", tel = "No tel", address = "No address", picturePh = "", userName = "",storeID = ""; // private LocationManager lm = null; // private static String provider = "gps"; private GeoPoint currentGeoPoint = null; /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.store_map_info); this.initViews(); this.getIntentInfo(); TVaddress.setText(address); TVTel.setText(tel); TVstore.setText(storeName); this.setTitle(city + "-" + storeName); map.setTraffic(true); map.setStreetView(true); map.setSatellite(false); map.setBuiltInZoomControls(true); GeoPoint geoBeiJing = new GeoPoint((int) 39.95 * 1000000, (int) 116.37 * 1000000); mapCon = map.getController(); mapCon.animateTo(geoBeiJing); mapCon.setZoom(10); this.enableLocation(map); this.addShopIcon(); } private void initViews() { map = (MapView) this.findViewById(R.id.map); btnOK = (Button) this.findViewById(R.id.btnOK); btnUpdate = (Button) this.findViewById(R.id.btnUpdate); TVstore = (TextView) this.findViewById(R.id.stoname); TVTel = (TextView) this.findViewById(R.id.tell); TVaddress = (TextView) this.findViewById(R.id.addre); image = (ImageView) this.findViewById(R.id.image); MyOnClickListener listener = new MyOnClickListener(); btnOK.setOnClickListener(listener); btnUpdate.setOnClickListener(listener); } private void getIntentInfo(){ Intent info = this.getIntent(); city = info.getStringExtra(ConstantUtil.CITYNAMEKEY) == null ? city : info.getStringExtra(ConstantUtil.CITYNAMEKEY); storeName = info.getStringExtra(ConstantUtil.STORENAMEKEY) == null ? storeName : info.getStringExtra(ConstantUtil.STORENAMEKEY); tel = info.getStringExtra(ConstantUtil.STORETELKEY) == null ? tel : info.getStringExtra(ConstantUtil.STORETELKEY); address = info.getStringExtra(ConstantUtil.STOREADDRESSKEY) == null ? address : info.getStringExtra(ConstantUtil.STOREADDRESSKEY); picturePh = info.getStringExtra(ConstantUtil.STOREPICTUREKEY)==null?picturePh:info.getStringExtra(ConstantUtil.STOREPICTUREKEY); Log.e(TAG,picturePh); storeID = info.getStringExtra(ConstantUtil.STOREIDKEY)==null?storeID:info.getStringExtra(ConstantUtil.STOREIDKEY); userName = info.getStringExtra(ConstantUtil.LOGINSUCCESSNAME); this.checkUser(); this.setImage(); Log.w(TAG, "city="+city); Log.w(TAG, "store="+storeName); Log.w(TAG, "tel="+tel); Log.w(TAG, "address="+address); Log.w(TAG, "pict="+picturePh); Log.w(TAG, "user="+userName); Log.w(TAG, "storeID="+storeID); } private void checkUser(){ if(userName == null){ AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle(R.string.userNotLogin); alert.setMessage(R.string.UserNotLoginMsg); alert.setPositiveButton("OK", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(StoreMapInfo.this,LoginIn.class); StoreMapInfo.this.startActivity(intent); } }); } } private void setImage() { Drawable dImage = Drawable.createFromPath(picturePh); if (dImage != null) { image.setImageDrawable(dImage); } else { Toast.makeText(this, "Can't find store picture!", 1000); } } // private void initGPSPosition(){ // Log.i(TAG,"initnGPSPosition"); // lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // provider = LocationManager.GPS_PROVIDER; // locationListener = new MyLocationListener(); // requestLocationUpdates(6000,10); // Location loc = lm.getLastKnownLocation(provider); // addShopIcon(); // if(loc!=null){ // currentGeoPoint = new // GeoPoint((int)loc.getLatitude(),(int)loc.getLongitude()); // mapCon.animateTo(currentGeoPoint); // } // } private void enableLocation(MapView mapView) { MyLocOverlay myLay = new MyLocOverlay(this, mapView); myLay.enableMyLocation(); myLay.enableCompass(); GeoPoint curGeoPoint = myLay.getMyLocation(); if (curGeoPoint != null) { currentGeoPoint = curGeoPoint; mapCon.animateTo(currentGeoPoint); } } private void addShopIcon() { GeoPoint geoBeiJing = new GeoPoint((int) 39.95 * 1000000, (int) 116.37 * 1000000); Drawable shopIcon = this.getResources().getDrawable(R.drawable.shop); MyShopOverlay myOverlay = new MyShopOverlay(shopIcon); OverlayItem item1 = new OverlayItem(geoBeiJing, storeName, address); // item1.setMarker(shopIcon); myOverlay.addItem(item1); map.getOverlays().add(myOverlay); // mapCon.animateTo(geoBeiJing); } @Override protected boolean isRouteDisplayed() { return false; } protected class MyOnClickListener implements OnClickListener { public void onClick(View v) { if (v == btnOK) { Intent intent = new Intent(StoreMapInfo.this,StoreListActivity.class); intent.putExtra( ConstantUtil.LOGINSUCCESSNAME,userName ); StoreMapInfo.this.startActivity(intent); } else if (v == btnUpdate) { Intent intent = new Intent(StoreMapInfo.this,StoreUpdateActivity.class); intent.putExtra( ConstantUtil.LOGINSUCCESSNAME,userName ); intent.putExtra(ConstantUtil.STOREIDKEY, storeID); StoreMapInfo.this.startActivity(intent); } } } protected class MyShopOverlay extends ItemizedOverlay<OverlayItem> { public MyShopOverlay(Drawable defaultMarker) { super(boundCenterBottom(defaultMarker)); } private ArrayList<OverlayItem> overlayItems = new ArrayList<OverlayItem>(); @Override protected OverlayItem createItem(int i) { return overlayItems.get(i); } @Override public int size() { return overlayItems.size(); } public void addItem(OverlayItem item) { overlayItems.add(item); this.populate(); } // @Override // public void draw(Canvas canvas, MapView mapView, boolean shadow) { // Projection proj = map.getProjection(); // for(OverlayItem item:overlayItems){ // //title // String title = item.getTitle(); // //snippet // String snippet = item.getSnippet(); // //geopoints to pixels // Point point = proj.toPixels(item.getPoint(), null); // } // super.draw(canvas, mapView, shadow); // } } protected class MyLocOverlay extends MyLocationOverlay { public MyLocOverlay(Context context, MapView mapView) { super(context, mapView); } @Override public synchronized void onLocationChanged(Location location) { super.onLocationChanged(location); currentGeoPoint = new GeoPoint( (int) location.getLatitude() * 1000000, (int) location .getLongitude() * 1000000); } } }

    参考博客:

    http://blog.csdn.net/L_serein/archive/2011/01/07/6122136.aspx

    http://blog.csdn.net/mtawaken/archive/2010/12/17/6081886.aspx

    最新回复(0)