android GoogleMap定位(三)

一个根据GPS信息在地图上的定位的小demo

布局文件main.xml

<?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="经度" android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
		<EditText android:id="@+id/lng" android:layout_width="85px"
			android:layout_height="wrap_content" />
		<TextView android:text="纬度" android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
		<EditText android:id="@+id/lat" android:layout_width="85px"
			android:layout_height="wrap_content" />
		<Button android:id="@+id/loc" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="定位"/>
	</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="普通" android:id="@+id/normal"
				android:checked="true" android:layout_width="wrap_content"
				android:layout_height="wrap_content" />
			<RadioButton android:text="卫星" android:id="@+id/satellite"
				android:layout_width="wrap_content" android:layout_height="wrap_content" />
		</RadioGroup>
	</LinearLayout>
	<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="0kcrUw1E0GWVCoo6chWBqqxAxiLQGpqgcTII6HQ" />
</LinearLayout>

 主界面文件

package com.hc;

import java.util.List;

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;
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;

public class GooglemapActivity extends MapActivity {
	/** Called when the activity is first created. */
	Button locBn;
	RadioGroup mapType;
	MapView mv;
	EditText etLng, etLat;
	MapController controller;
	Bitmap posBitmap;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		posBitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.icon);
		mv = (MapView) findViewById(R.id.mv);
		etLng = (EditText) findViewById(R.id.lng);
		etLat = (EditText) findViewById(R.id.lat);
		// 显示放大缩小控制按钮
		mv.setBuiltInZoomControls(true);
		controller = mv.getController();
		locBn = (Button) findViewById(R.id.loc);
		locBn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				String lng = etLng.getEditableText().toString().trim();
				String lat = etLat.getEditableText().toString().trim();
				if (lng.equals("") || lat.equals("")) {
					Toast.makeText(GooglemapActivity.this, "输入有效经度维度",
							Toast.LENGTH_LONG).show();
				} else {
					double dlong = Double.parseDouble(lng);
					double dLat = Double.parseDouble(lat);
					UpdateMapView(dlong, dLat);
				}
			}

		});
		locBn.performClick();
		mapType = (RadioGroup) findViewById(R.id.rg);
		mapType.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				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;
	}

	private void UpdateMapView(double dlong, double dLat) {
	GeoPoint gp = new GeoPoint((int)(dLat*1E6), (int)(dlong*1E6));
	mv.displayZoomControls(true);
	controller.animateTo(gp);
	List<Overlay> ol = mv.getOverlays();
	ol.clear();
	ol.add(new PosOverLay(gp, posBitmap));
	}
}

 地图上显示的自定义定位图片

package com.hc;

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 {

	Bitmap posBitmap;
	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) {
	if(!shadow){
		Projection proj = mapView.getProjection();
		Point p= new Point();
		proj.toPixels(gp, p);
		canvas.drawBitmap(posBitmap, p.x-posBitmap.getWidth()/2,p.y-posBitmap.getHeight(), null);
	}
	}

}

 manifest文件中加入两条

    <uses-permission android:name="android.permission.INTERNET"/>  网络权限

     <uses-library android:name="com.google.android.maps"/>

 

效果如下

 


本帖最后由 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这个文件夹了,那么继续进行下面的操作即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值