Android使用Google Map服务 - 根据GPS信息在地图上定位

Android使用Google Map服务 - 根据GPS信息在地图上定位

自暑假7月7日开始,到今天的8月7日,整个一个月,我总算是学到了Google Map这部分的内容。原本挺兴奋的,却被注册api key的网页显示错误弄得挺无聊,还好网上的能人多,搜了一下子就找到了解决方案,挺感激的,泪牛满面啊。这样我就可以继续这部分的学习。

在使用Google Map服务之前要做一些必要的准备

1.获取Map API Key

2.创建支持Google Map API 的AVD

关于获取Map API Key 的方法前面的博客已经有所介绍,也就不另外总结了,创建AVD,我想有过一段事件的Android学习的人肯定知道的,也不想多说。

 

下面是一个使用Google Map的简单例子,亲测成功!!!

创建项目:LocationMap

运行项目效果图:

      

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
<LinearLayout
	android:orientation="horizontal"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:gravity="center_horizontal">
<TextView
	android:text="@string/txtLong"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"/>
<!-- 定义输入经度值的文本框 -->	
<EditText
	android:id="@+id/lng"
	android:text="@string/lng"
	android:layout_width="85px"
	android:layout_height="wrap_content" />
<TextView
	android:text="@string/txtLat"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:paddingLeft="8px" />
<!-- 定义输入纬度值的文本框 -->
<EditText
	android:id="@+id/lat"
	android:text="@string/lat"
	android:layout_width="85px"
	android:layout_height="wrap_content" />
<Button
	android:id="@+id/loc"
	android:text="@string/loc"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:layout_weight="4" />	
</LinearLayout>
<LinearLayout
	android:orientation="horizontal"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:gravity="center_horizontal">
<!-- 定义选择地图类型的单选框组 -->
<RadioGroup
	android:id="@+id/rg"
	android:orientation="horizontal"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:layout_weight="1">
<RadioButton
	android:text="@string/normal"
	android:id="@+id/normal"
	android:checked="true"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"/>
<RadioButton
	android:text="@string/satellite"
	android:id="@+id/satellite"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"/>
</RadioGroup>
</LinearLayout>
<!-- 定义一个MapView,注意apiKey必须是用户自己申请的 -->
<com.google.android.maps.MapView
	android:id="@+id/mv"
	android:clickable="true"
	android:enabled="true"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:apiKey="0RgnU2CyaG_2EmVilj8kShSS6JxagQv2i6SOzxw" />
</LinearLayout>


 

package org.wwj.map;

import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class LocationMap extends MapActivity {
	
	//定义界面上的可视化控件
	Button locBn;
	RadioGroup mapType;
	MapView mv;
	EditText etLng, etLat;
	//定义MapController对象
	MapController controller;
	Bitmap posBitmap;
	@Override
	protected void onCreate(Bundle icicle) {
		// TODO Auto-generated method stub
		super.onCreate(icicle);
		setContentView(R.layout.main);
		posBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pos);
		//获取界面的MapView对象
		mv = (MapView) findViewById(R.id.mv);
		//获取界面上两个文本框
		etLng = (EditText) findViewById(R.id.lng);
		etLat = (EditText) findViewById(R.id.lat);
		//设置显示放大、缩小控制按钮
		mv.setBuiltInZoomControls(true);
		//创建MapController对象
		controller = mv.getController();
		//获取Button对象
		locBn = (Button) findViewById(R.id.loc);
		locBn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//获取用户输入的经度、纬度值
				String lng = etLng.getEditableText().toString().trim();
				String lat = etLat.getEditableText().toString().trim();
				if(lng.equals("") || lat.equals("")){
					Toast.makeText(LocationMap.this, "请输入有效的经度、纬度!", Toast.LENGTH_LONG).show();
				}
				else
				{
					double dLong = Double.parseDouble(lng);
					double dLat = Double.parseDouble(lat);
					//调用方法更新MapView
					updateMapView(dLong, dLat);
				}
			}
		});
		//触发按钮的单击事件
		locBn.performClick();
		//获得RadioGroup对象
		mapType = (RadioGroup) findViewById(R.id.rg);
		//为RadioGroup的选中状态改变添加监听器
		mapType.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch(checkedId){
				//如果勾选的是“正常视图”的单选按钮
				case R.id.normal:
					mv.setSatellite(false);
					break;
				//如果勾选的是“卫星视图”的单选按钮
				case R.id.satellite:
					mv.setSatellite(true);
					break;
				}
			}
		});
	}
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return true;
	}
	//根据经度、纬度将MapView定位到指定地点的方法
	private void updateMapView(double lng, double lat){
		//将经纬度信息包装成GeoPoint对象
		GeoPoint gp = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));
		//设置显示放大缩小按钮
		mv.displayZoomControls(true);
		//将地图移动到指定的地理位置
		controller.animateTo(gp);
		//获得MapView上原有的Overlay对象
		List<Overlay> ol = mv.getOverlays();
		//清除原有的overlay对象
		ol.clear();
		//添加一个新的overLay对象
		ol.add(new PosOverLay(gp, posBitmap));
	}
}


 

package org.wwj.map;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class PosOverLay extends Overlay{
	
	//定义该PosOverLay所绘制的位图
	Bitmap posBitmap;
	//定义该PosOverLay绘制位图的位置
	GeoPoint gp;
	public PosOverLay(GeoPoint gp, Bitmap posBitmap){
		super();
		this.gp = gp;
		this.posBitmap = posBitmap;
	}
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		// TODO Auto-generated method stub
		if(!shadow){
			//获取MapView的Projection对象
			Projection proj = mapView.getProjection();
			Point p = new Point();
			//将真实的地理坐标转换为屏幕上的坐标
			proj.toPixels(gp, p);
			//在指定位置绘制图片
			canvas.drawBitmap(posBitmap, p.x - posBitmap.getWidth() / 2
					, p.y - posBitmap.getHeight() / 2, null);
		}
		
	}
	


完整manifest文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.wwj.map"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET"/>
	
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".LocationMap"
            android:label="@string/title_activity_location_map" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <uses-library android:name="com.google.android.maps"/>
    </application>

</manifest>


 

 

 

posted on 2012-08-07 13:14  1.曲待续  阅读(652)  评论(0编辑  收藏  举报

导航