android网络-GoogleMap之GPS定位

这篇博客详细介绍了在Android应用中集成GoogleMap并实现GPS定位的步骤,包括获取MapAPIKey、创建Google APIs AVD、设置工程及权限,以及实现地图的交互功能。通过这些步骤,开发者能够使应用程序根据经纬度变化在地图上显示位置。

首先,获取MapAPIKey

1,获取android keystore位置

eclipse->window->preferences->android->build

在default debug keystore下找到android key的存储位置C:\Users\acer\.android\debug.keystore,备用。

2,用jdk提供的keytool为android keystore生成认证指纹

进入cmd,按下图输入

C:\Program Files\Java\jdk1.7.0_03\bin>keytool -list -alias androiddebugkey -keystore "C:\Users\acer\.android\debug.keystore" -v

密码输入:android或不输。

取MD5值,备用


3,输入网址http://code.google.com/intl/zh-CN/android/maps-api-signup.html

,登陆Google账号,并且在申请页面上输入得到的MD5认证指纹,点击”Generate API Key”来获取我们得到的API Key

然后,创建基于Google APIs的AVD,以及创建基于Google APIs的工程

就是把所有Android2.2的都改为Google APIs

最后,编写程序

程序效果:运行程序,send经纬度后,界面标记根据经纬度变化

manifest

注意添加internet和gps的权限,以及添加googlemap的library

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.song"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".C7_GoogleMapActivity" >
            <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>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
</manifest>
主布局

让map可点击和拖拽 android:clickable="true"
获取得到的key           android:apiKey="0HtSsgCEmiiTTfk-g0Oi59Wi2ndgjaMdoLSDPnw"

<?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" >

 <com.google.android.maps.MapView
     android:id="@+id/mapview"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:clickable="true"
     android:apiKey="0HtSsgCEmiiTTfk-g0Oi59Wi2ndgjaMdoLSDPnw"
     />

</LinearLayout>
主activity

package com.song;

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 com.google.android.maps.Projection;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class C7_GoogleMapActivity extends MapActivity {
    /** Called when the activity is first created. */
	MapView mapview;
	MapController controller;
	Location location;
	LocationManager manager;
	GeoPoint geoPoint;
	Bitmap bitmap;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mapview=(MapView)findViewById(R.id.mapview);  
        manager=(LocationManager)getSystemService(LOCATION_SERVICE);
        location=manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.pos);//生成bitmap位图
   
        
        mapview.setBuiltInZoomControls(true);//实现放大缩小功能
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, new LocationListener() {
			
			@Override 
			public void onStatusChanged(String provider, int status, Bundle extras) {
				// TODO Auto-generated method stub
				
			}
			 
			@Override
			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onLocationChanged(Location location) {
				// TODO Auto-generated method stub
			    getPoint(location);
			}
		});
 
    }
    public void getPoint(Location location)
    {
    	 controller=mapview.getController();
         //注意参数是纬度,经度。E6为10的6次方
//       GeoPoint geoPoint=new GeoPoint((int)(37.898989*1E6), (int)(122.989898*1E6));
         geoPoint=new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6));
         //定位到哪个点
         controller.animateTo(geoPoint);
         //获得悬浮图层
         List<Overlay> list = mapview.getOverlays();//获得MapView上原有的overlay对象
         list.clear();//清除所有的overlay对象
         list.add(new MyOverLay());//加新获取的overlay对象
    }

	class MyOverLay extends Overlay
	{  
		//画标记的方法
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			// TODO Auto-generated method stub
			super.draw(canvas, mapView, shadow);
			Projection projection=mapView.getProjection();
			
			Point point=new Point();//定义输出的像素点
			projection.toPixels(geoPoint, point);//地理坐标转为像素坐标
			//绘制图片
			//bitmap, left, top, paint
			canvas.drawBitmap(bitmap, point.x-(bitmap.getWidth()/2), point.y-(bitmap.getHeight()), null);
		} 
	}
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
}
程序效果:下下图为天安门的定位效果



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值