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"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayoutandroid:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"><TextViewandroid:text="@string/txtLong"android:layout_width="wrap_content"android:layout_height="wrap_content"/><!-- 定义输入经度值的文本框 --><EditTextandroid:id="@+id/lng"android:text="@string/lng"android:layout_width="85px"android:layout_height="wrap_content" /><TextViewandroid:text="@string/txtLat"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingLeft="8px" /><!-- 定义输入纬度值的文本框 --><EditTextandroid:id="@+id/lat"android:text="@string/lat"android:layout_width="85px"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/loc"android:text="@string/loc"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="4" /></LinearLayout><LinearLayoutandroid:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"><!-- 定义选择地图类型的单选框组 --><RadioGroupandroid:id="@+id/rg"android:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"><RadioButtonandroid:text="@string/normal"android:id="@+id/normal"android:checked="true"android:layout_width="wrap_content"android:layout_height="wrap_content"/><RadioButtonandroid: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.MapViewandroid: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;@Overrideprotected void onCreate(Bundle icicle) {// TODO Auto-generated method stubsuper.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);//调用方法更新MapViewupdateMapView(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 stubswitch(checkedId){//如果勾选的是“正常视图”的单选按钮case R.id.normal:mv.setSatellite(false);break;//如果勾选的是“卫星视图”的单选按钮case R.id.satellite:mv.setSatellite(true);break;}}});}@Overrideprotected boolean isRouteDisplayed() {// TODO Auto-generated method stubreturn 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;}@Overridepublic void draw(Canvas canvas, MapView mapView, boolean shadow) {// TODO Auto-generated method stubif(!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>


更多相关文章

  1. 自定义View:Android(安卓)仿 PS 选色板
  2. Android(安卓)自定义 View 实现跑马灯效果
  3. [置顶] Android(安卓)自定义ViewGroup实现整个Item布局竖直跑马
  4. 【移动开发】Android中不用图片资源也能做出好看的界面
  5. android回调的理解------异步任务回调接口
  6. Android(安卓)自定义 Toast 示例
  7. 自定义控件之TabIndicator
  8. android自定义View绘制几何图形
  9. Fragment.setArguments()方法向fragment对象传递数据的重要作用

随机推荐

  1. Android蓝牙理论
  2. Android之解决java.lang.UnsatisfiedLink
  3. android 开发浅谈(JDK && NDK)
  4. 【Android】AsyncTask机制
  5. (笔记)Android(安卓)studio——相对布局(Rel
  6. Android(安卓)线刷小白教程
  7. IOS、Android自动化测试框架
  8. Android读取联系人信息(姓名,电话,地址,邮箱,
  9. 【转】 Android(安卓)Layout之三:Ralative
  10. Lottie Android(安卓)初探