`
104zz
  • 浏览: 1503790 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

android google地图定位开发,且可以自由移动位置重新获取定位,地址信息

阅读更多

 

 

一:申请key:

1.首先找到debug keystore位置:

  打开Eclipse--->Windows--->Preferences--->Android--->Build

一般是这样的路径 C:\Documents and Settings\Administrator\.android\debug.keystore

2.cmd中执行

keytool -list -alias androiddebugkey -keystore "C:\Documents and Settings\Administrator\.android\debug.keystore" -storepass android -keypass android


得到认证指纹 (MD5)6F:C9:41:48:A5:F3:36:A5:D3:DD:B5:D1:CB:AC:47:88

3.打开申请key页面 http://code.google.com/android/maps-api-signup.html

复制 认证指纹 (MD5):到下面的 My certificate's MD5 fingerprint

4.然后点击 Generate Api key

5.等到apikey:0Mg_koWoyZUhlluO4-i6-bq9WYMFbxKodZZMz2Q

 

二:设计main.xml如下:(将申请到得key设置在com.google.android.maps.MapView 中如下所示

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <FrameLayout
        android:id="@+id/map_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <com.google.android.maps.MapView
            android:id="@+id/map_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            	   android:apiKey="0Mg_koWoyZUhlluO4-i6-bq9WYMFbxKodZZMz2Q"
            android:clickable="true"
            android:enabled="true" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical"
            android:paddingBottom="105dip" >
            <TextView
                android:id="@+id/map_bubbleText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/location_tips"
                android:gravity="left|center"
                android:maxEms="12"
                android:paddingLeft="12dip"
                android:paddingRight="10dip"
                android:text="@string/load_tips"
                android:textColor="#cfcfcf"
                android:textSize="14sp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical" >
            <ImageView
                android:id="@+id/point_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginBottom="30dip"
                android:src="@drawable/point_start" />
        </LinearLayout>
    </FrameLayout>
</LinearLayout>
 

 

三:创建MyLocationManager类主要用于管理经纬度获取方法实现

package com.android.map;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class MyLocationManager {
	private final String TAG = "FzLocationManager";
	private static Context mContext;
	private LocationManager gpsLocationManager;
	private LocationManager networkLocationManager;
	private static final int MINTIME = 2000;
	private static final int MININSTANCE = 2;
	private static MyLocationManager instance;
	private Location lastLocation = null;
	private static LocationCallBack mCallback;
	
	public static void init(Context c , LocationCallBack callback) {
		mContext = c;
		mCallback = callback;
	}

	
	private MyLocationManager() {
		// Gps 定位
		gpsLocationManager = (LocationManager) mContext
				.getSystemService(Context.LOCATION_SERVICE);
		Location gpsLocation = gpsLocationManager
				.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				MINTIME, MININSTANCE, locationListener);
        // 基站定位
		networkLocationManager = (LocationManager) mContext
				.getSystemService(Context.LOCATION_SERVICE);
		Location networkLocation = gpsLocationManager
				.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		networkLocationManager.requestLocationUpdates(
				LocationManager.NETWORK_PROVIDER, MINTIME, MININSTANCE,
				locationListener);
	}

	public static MyLocationManager getInstance() {
		if (null == instance) {
			instance = new MyLocationManager();
		}
		return instance;
	}

	private void updateLocation(Location location) {
		lastLocation = location;
		mCallback.onCurrentLocation(location);
	}

	
	private final LocationListener locationListener = new LocationListener() {
		
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}

		
		public void onProviderEnabled(String provider) {
		}

		
		public void onProviderDisabled(String provider) {
		}

		
		public void onLocationChanged(Location location) {
			Log.d(TAG, "onLocationChanged");
			updateLocation(location);
		}
	};

	public Location getMyLocation() {
		return lastLocation;
	}
	
    private static int ENOUGH_LONG = 1000 * 60;	 
	
	public interface LocationCallBack{
		/**
		 * 当前位置
		 * @param location 
		 */
		void onCurrentLocation(Location location);
	}
	
	
	public void destoryLocationManager(){
		Log.d(TAG, "destoryLocationManager");
		gpsLocationManager.removeUpdates(locationListener);
		networkLocationManager.removeUpdates(locationListener);
	}
}
   

 

四:创建MyMapOverlay抽象类,并继承Overlay,创建抽象方法 

changePoint(GeoPoint newPoint,int type)用于回调重新获取到的GeoPoint 重新定位地图,并获取地址信息

 

 

import android.view.MotionEvent;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
//覆盖整个地图捕捉触控事件的OverLay
public abstract class MyMapOverlay extends Overlay{
private int point_X;
private int point_Y;
private GeoPoint newPoint;
public MyMapOverlay(int x,int y){
point_X = x;
point_Y = y;
}
boolean flagMove=false;
//触控屏幕移动地图,重新根据屏幕中心点获取该点经纬度
    @Override 
    public boolean onTouchEvent(MotionEvent event, MapView mapView) {
    	System.out.println("X->"+event.getX()+":"+point_X);
    	System.out.println("Y->"+event.getY()+":"+point_Y);
        if(event.getAction() == MotionEvent.ACTION_DOWN){
        	changePoint(newPoint,1);
        }else if(event.getAction() == MotionEvent.ACTION_UP){
        	newPoint = mapView.getProjection().fromPixels(point_X,point_Y);
        	changePoint(newPoint,2);
        }       
        return false;
    }
    
    public abstract void changePoint(GeoPoint newPoint,int type);
}
 

 

五:MyMapActivity 继承MapActivity类并实现经纬度获取回调接口LocationCallBack 。项目实现如下:

package com.android.googlemap;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.graphics.Rect;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.android.map.MyLocationManager;
import com.android.map.MyLocationManager.LocationCallBack;
import com.android.map.MyMapOverlay;
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 MyMapActivity extends MapActivity implements LocationCallBack {

	
	private MapView mapView;
	private MapController mMapCtrl;
	private MyLocationManager myLocation;
	private List<Overlay> mapOverlays;
	public GeoPoint locPoint;
	private MyMapOverlay mOverlay;
	private TextView desText;
	private String lost_tips;
	private int point_X;
	private int point_Y;
	private int statusBarHeight;

	public final int MSG_VIEW_LONGPRESS = 10001;
	public final int MSG_VIEW_ADDRESSNAME = 10002;
	public final int MSG_GONE_ADDRESSNAME = 10003;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		mapView = (MapView) findViewById(R.id.map_view);
		desText = (TextView) this.findViewById(R.id.map_bubbleText);
		lost_tips = getResources().getString(R.string.load_tips);
		mapView.setBuiltInZoomControls(true);
		mapView.setClickable(true);
		mMapCtrl = mapView.getController();
		point_X = this.getWindowManager().getDefaultDisplay().getWidth() / 2;
		point_Y = this.getWindowManager().getDefaultDisplay().getHeight() / 2;
		mOverlay = new MyMapOverlay(point_X, point_Y) {
			@Override
			public void changePoint(GeoPoint newPoint, int type) {
				if (type == 1) {
					mHandler.sendEmptyMessage(MSG_GONE_ADDRESSNAME);
				} else {
					locPoint = newPoint;
					mHandler.sendEmptyMessage(MSG_VIEW_LONGPRESS);
				}

			}
		};
		mapOverlays = mapView.getOverlays();
		if (mapOverlays.size() > 0) {
			mapOverlays.clear();
		}
		mapOverlays.add(mOverlay);
		mMapCtrl.setZoom(12);
		MyLocationManager.init(MyMapActivity.this.getApplicationContext(),
				MyMapActivity.this);
		myLocation = MyLocationManager.getInstance();

	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	public void onCurrentLocation(Location location) {
		if (locPoint == null) {
			locPoint = new GeoPoint((int) (location.getLatitude() * 1E6),
					(int) (location.getLongitude() * 1E6));
			mHandler.sendEmptyMessage(MSG_VIEW_LONGPRESS);
		}
	}

	public void changePoint(GeoPoint locPoint) {

	}

	/**
	 * 通过经纬度获取地址
	 * 
	 * @param point
	 * @return
	 */
	private String getLocationAddress(GeoPoint point) {
		String add = "";
		Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
		try {
			List<Address> addresses = geoCoder.getFromLocation(
					point.getLatitudeE6() / 1E6, point.getLongitudeE6() / 1E6,
					1);
			Address address = addresses.get(0);
			int maxLine = address.getMaxAddressLineIndex();
			if (maxLine >= 2) {
				add = address.getAddressLine(1) + address.getAddressLine(2);
			} else {
				add = address.getAddressLine(1);
			}
		} catch (IOException e) {
			add = "";
			e.printStackTrace();
		}
		return add;
	}

	/**
	 * 
	 * 用线程异步获取
	 */
	Runnable getAddressName = new Runnable() {
		public void run() {
			String addressName = "";
			while (true) {
				addressName = getLocationAddress(locPoint);
				if (!"".equals(addressName)) {
					break;
				}
			}
			Message msg = new Message();
			msg.what = MSG_VIEW_ADDRESSNAME;
			msg.obj = addressName;
			mHandler.sendMessage(msg);
		}
	};

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_VIEW_LONGPRESS:// 处理长按时间返回位置信息
			{
				if (null == locPoint)
					return;
				new Thread(getAddressName).start();
				desText.setVisibility(View.VISIBLE);
				desText.setText(lost_tips);
				mMapCtrl.animateTo(locPoint);
				mapView.invalidate();
			}
				break;
			case MSG_VIEW_ADDRESSNAME:
				desText.setText((String) msg.obj);
				desText.setVisibility(View.VISIBLE);
				if (statusBarHeight == 0) {
					Rect frame = new Rect();
					getWindow().getDecorView().getWindowVisibleDisplayFrame(
							frame);
					statusBarHeight = frame.top;
					point_Y -= statusBarHeight / 2;
				}
				break;
			case MSG_GONE_ADDRESSNAME:
				desText.setVisibility(View.GONE);
				break;
			}
		}
	};

	// 关闭程序也关闭定位
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		myLocation.destoryLocationManager();
	}
	
}
   

六:AndroidManifest.xml中不要忘了要添加访问网络和启动定位等的几个权限已经google地图库

 

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!--在application里面添加google地图库如下(一定要记得添加):-->
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" /> 
        <activity android:name="com.android.googlemap.MyMapActivity"  android:screenOrientation="portrait" 
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 

 

七:运行效果:

1


2以后后重新获取如下:
 

 


提示:如果没有出现地图只出现地址信息背景是网格的,那就是key的问题,根据开始的方法找到自己开发的eclipse中sdk的debug keystore位置重新申请key即可

 

  • 大小: 207.2 KB
  • 大小: 184.9 KB
分享到:
评论
2 楼 cys一 2017-05-31  
google api key v2  新的不能使用
1 楼 renci 2013-07-04  
楼主你这个实地测量过没?
国内的地图坐标是火星坐标和gps获取的坐标是不一致的.

相关推荐

    Google Android SDK开发范例大全(第3版) 4/5

    《Google Android SDK开发范例大全(第3版)》在上一版的基础上,以Android手机应用程序开发(采用Android SDK 2.3.3)为主题,超过200多个范例全面且深度地整合了手机、网络及服务等多个开发领域,为读者提高程序设计...

    Google Android SDK开发范例大全(第3版) 3/5

    《Google Android SDK开发范例大全(第3版)》在上一版的基础上,以Android手机应用程序开发(采用Android SDK 2.3.3)为主题,超过200多个范例全面且深度地整合了手机、网络及服务等多个开发领域,为读者提高程序设计...

    Google Android SDK开发范例大全(第3版) 5/5

    《Google Android SDK开发范例大全(第3版)》在上一版的基础上,以Android手机应用程序开发(采用Android SDK 2.3.3)为主题,超过200多个范例全面且深度地整合了手机、网络及服务等多个开发领域,为读者提高程序设计...

    安卓源码包 Android GPS 开发 地图&导航&定位&指南等设计代码合集 (45个).zip

    百度地图移动获取位置,自动定位.zip 百度快速定位locSDK_3.3_Demo.zip 监控别人的行踪.rar 级联菜单,两级菜单自定义实现提供多种方式PopWindow,Fragment引用..rar 自动判断位置的弹出菜单.zip 菜单动画(类似QQ...

    Google Android SDK开发范例大全(第3版) 1/5

    《Google Android SDK开发范例大全(第3版)》在上一版的基础上,以Android手机应用程序开发(采用Android SDK 2.3.3)为主题,超过200多个范例全面且深度地整合了手机、网络及服务等多个开发领域,为读者提高程序设计...

    Android获取位置信息的方法

    本文实例为大家分享了Android获取位置信息的具体代码,供大家参考,具体内容如下 1.位置服务的简介:位置服务,英文翻译为Location-Based Services,缩写为LBS,又称为定位服务或基于位置的服务,融合了GPS定位、移动...

    新版Android开发教程.rar

    将会支持 Google 可能发布的手机操作系统或者应用软件,共同开发名为 Android 的开放源代码的移动 系 统。 谷歌早在 2002 年就进入了移动领域,可是由于目前的手机操作系统企业和手机企业相对封闭,提高了 行业的...

    工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究

    Android 一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导开发 IOS 由苹果公司开发的移动操作系统 Webkit 一个开源的浏览器引擎,在手机上的...

    android开发实例大全_王东华

    本书以Android应用程序的开发为主题,并结合真实的案例向读者详细介绍了Android的基本组件的使用及应用程序开发的整个流程。本书的讲述由浅入深,实例全面并典型,几乎囊括了所有和Android应用相关的项目。全书分为...

    Android 定位系统(GPS)开发详解

    该地理定位服务可以用来获取当前设备的地理位置,应用程序可以定时请求更新设备当前的地理位置信息。比如应用程序可以借助一个Intent接收器来实现如下功能: 以经纬度和半径规划一个区域,当设备进入给区域,发出...

    Android 传感器数据采集 - 位置传感器 (GPS) 示例:从运行 MATLAB 的计算机中获取移动设备的位置数据并将其可视化。-matlab开发

    此示例展示了如何获取 Android 移动设备(例如手机或平板电脑)的位置和速度数据。 位置数据由纬度、经度和高度数据组成。 在此示例中,捕获的纬度和经度数据在 MATLAB 中绘制,并使用 Mapping Toolbox 覆盖在来自 ...

    集成目前Android主流优秀第三方组件

    这是一个集成目前Android主流优秀第三方组件、优秀好用的自定义控件、实用工具类封装、以及一些APP共通模块(比如:版本更新、意见反馈、引导界面等等)的开发包,帮助程序员快速开发自己的APP 已集成第三方开源...

    UCMAP 开发使用手册

    8, GPS 定位,支持获取手机GPS 定位数据,实现GPS 定位监控,以及基站定位; 9, Bing Maps API,支持访问互联网地图服务Bing Maps; 10,Google Maps API,支持访问互联网地图服务Google Maps; 11,数据融合,...

    Android集成主流优秀第三方组件框架

    地图定位工具类(ToolLocation.java),读取GPS状态、请求定位、获取经纬度等方法 社会化分享工具类(ToolShareSDK.java),各大开发平台分享API操作 短信验证码工具类(ToolMSM.java),移动/联通/电信三网发送手机...

    Android例子源码集成安卓主流优秀第三方组件框架.zip

    这是一个集成目前Android主流优秀第三方组件、优秀好用的自定义控件、实用工具类封装、以及一些APP共通模块(比如:版本更新、意见反馈、引导界面等等)的开发包,帮助程序员快速开发自己的APP 已集成第三方开源...

    android取得所在位置的经纬度

    见博客:http://blog.csdn.net/baidu_nod/article/details/37697685

    基于java的本地旅游天气实时平台

    详细来讲,本文所研讨的移动旅行信息管理系统首要包含三个子系统:景点查询,该子系统能供给对旅行景点方位查询,在地图上标识经纬度值,能对当时及未来三天进行天气预报,能查询两地址间的行走搭车途径以及周边住宿、...

    mylife:Android应用程序通过设备传感器记录用户活动

    My-Life是专为Android平台设计的移动应用程序。 使用不同的智能手机传感器,该软件可以记录白天或锻炼期间用户活动的数据。 收集的信息显示为一组统计信息,并且还以图形或地图的形式显示。 My-Life是由弗罗茨瓦夫...

Global site tag (gtag.js) - Google Analytics