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

本文介绍如何在Android应用中集成Google Maps服务,并通过GPS信息实现地图定位。包括创建项目、配置API密钥、设置地图类型及通过经纬度更新地图位置等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>


 

 

 

本帖最后由 espressocafe 于 2011-12-11 12:00 编辑 此GMS包中包括下列全套完整的谷歌服务:   ·电子市场(不是最新版,可安装完以后用电子市场升级)   ·谷歌邮件(不是最新版,可安装完以后用电子市场升级)   ·谷歌日历同步   ·谷歌联系人同步   ·谷歌地图(5.12.1,不是最新版,最新版是6.0.1,可安装完以后用电子市场升级)   ·谷歌纵横   ·谷歌导航   ·谷歌搜索   ·谷歌本地搜索   ·谷歌语音(不是最新版,可安装完以后用电子市场升级)   ·谷歌Talk   刷入前请务必确保手机已经安装Root Explorer或类似软件,且已经Root。用数据线连接电脑,连接模式为仅充电,USB调试模式已打开。(这个连接电脑就是正常的开机连入电脑,不是进什么hboot或什么recovery)      安装方法:   1.将附件下载并解压到X:\adb,因为前期如果您刷机,肯定在您的某块盘上有adb这个文件夹,没有?~~~呃,在别人的教程里下载adb吧,您是怎么root并安装RE的?好吧,您赢了,我已经把adb这个4个文件一并放在包里了。 检查,X:\adb目录里面是否有adb.exe、fastboot.exe等文件,应该不少于4个,咱们这个包解出来是个叫app的文件夹,也在adb目录里,app文件夹进去就是一些后缀名为apk、xml、so的文件,保证他们没有在下层文件夹中。 好了,如果你是XP系统,请按下Win+R,输入CMD回车,然后输入以下命令: cd\ x: (这个X如果是c的话就省略了吧,我的是D,就是换到d盘) cd adb 进入adb文件夹了吧? 如果是win7,简单多了,在图形界面进入X:\adb目录,按住SHIFT键同时点鼠标右键,选在此处打开命令行,呃,也进来了吧? 2.依次输入以下命令: adb shell mkdir sdcard/temp/ (在您手机sd卡上建立了一个叫temp的文件夹) adb push app sdcard/temp/ (把您adb目录下app文件夹中的所有内容拷贝到手机sd卡temp目录中) adb shell (看提示符,是不是已经变成$了?) su (嗯,这下变成#了吧?) 好啦,为了防止出错,有经验的同学可以继续以命令行方式输入cp sdcard/temp/*.apk /system/app/,cp sdcard/temp/*.so /system/lib/,cp sdcard/temp/*.xml /system/etc/permissions/,cp sdcard/temp/*.jar /system/framework/而没经验的同学请先不要管电脑上的命令了,拿起手机打开RE,找到sdcard,进去后找到temp文件夹,进入,用多选模式,把文件夹中的所有的apk文件(一大堆)移动到/system/app文件夹中,把所有的so文件(2个还是3个?)移动到/system/lib文件夹中,把所有的xml 文件(好像就1个)移动到 /system/etc/permissions文件夹中,把所有的jar文件(好像也就1个)移动到/system/framework文件夹中。好了么?sd卡上的temp文件夹也可以顺手删除了,使命已经完成了。别忘了Root Explorer要点击左上角的按钮切换到Mount R/O状态,否则写不进去啊。 好啦,还要统一改一下拷入系统那些文件的权限,都回到电脑上来,那个#还在闪呢!输入: chmod 644 /system/lib/*.so /system/app/*.apk /system/framework/*.jar /system/etc/permissions/*.xml,成功了的表现就是系统给你重复了一遍……(好2啊~~~~) 至此GMS包已全部刷入完毕,重启手机即可。如出现mkdir failed for sdcard/temp/,File exists的提示,则表明手机已经存在temp这个文件夹了,那么继续进行下面的操作即可。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小巫技术博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值