AndroidManifest.xml文件配置
<?
xml
version="1.0"encoding="utf-8"?>
<
manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.itcast.main"
android:versionCode="1"
android:versionName="1.0">
<
application
android:icon="@drawable/icon"android:label="@string/app_name">
<
activity
android:name=".Main"
android:label="@string/app_name">
<
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_COARSE_LOCATION"/>
<
uses-permission
android:name="android.permission.ACCESS_FIND_LOCATION"/>
</
manifest
>
Main.java文件
package
cn.itcast.main;
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.app.AlertDialog;
import
android.content.Context;
import
android.content.DialogInterface;
import
android.content.DialogInterface.OnClickListener;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.graphics.Canvas;
import
android.graphics.Color;
import
android.graphics.Paint;
import
android.graphics.Point;
import
android.location.Address;
import
android.location.Criteria;
import
android.location.Geocoder;
import
android.location.Location;
import
android.location.LocationListener;
import
android.location.LocationManager;
import
android.os.Bundle;
import
android.view.MotionEvent;
import
android.widget.TextView;
public
class
Main
extends
MapActivity
{
private
MapController
mapController
;
private
GeoPoint
geoPoint
;
private
String
msg
;
@Override
public
void
onCreate(
Bundle
savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(
R
.
layout
.
main
);
//
获得
mapview
MapView
mapView=(
MapView
)
this
.findViewById(
R
.
id
.
mapview
);
//
地图的显示格式为交通图
mapView.setTraffic(
true
);
//
设置可控
mapView.setClickable(
true
);
mapView.setEnabled(
true
);
mapView.setBuiltInZoomControls(
true
);
//
得到
gps
设备的访问
LocationManager
locationManager=(
LocationManager
)getSystemService(
Context
.
LOCATION_SERVICE
);
//
设置
gps
定位配置
Criteria
criteria=
new
Criteria
();
//
设置显示精度
criteria.setAccuracy(
Criteria
.
ACCURACY_COARSE
);
//
是否获得海拔数据
criteria.setAltitudeRequired(
false
);
//
是否获得方向数据
criteria.setBearingRequired(
false
);
//
是否允许运营商计费
criteria.setCostAllowed(
true
);
//
设置耗电程度
criteria.setPowerRequirement(
Criteria
.
POWER_LOW
);
//
获得服务供应商
String
provider=locationManager.getBestProvider(criteria,
true
);
//
获取上一个定位点
Location
location=locationManager.getLastKnownLocation(provider);
//
获得
gps
定位坐标信息
Double
latitude=location.getLatitude()*1E6;
Double
longitude=location.getLongitude()*1E6;
//
获得卫星定位点
geoPoint
=
new
GeoPoint
(latitude.intValue(),longitude.intValue());
//
获得地图控制器
mapController
=mapView.getController();
//
设置地图显示初始化精度
mapController
.setZoom(12);
mapController
.animateTo(
geoPoint
);
//
实例化自定义绘图层
MyOverlay
myOverlay=
new
MyOverlay
();
//
为
mapview
添加绘图层
mapView.getOverlays().add(myOverlay);
//
定义一个
final
,
TextView
,以备子类引用
final
TextView
textView=(
TextView
) findViewById(
R
.
id
.
textview
);
LocationListener locationListener=
new
LocationListener(){
@Override
public
void
onStatusChanged(
String
provider,
int
status,
Bundle
extras) {
}
@Override
public
void
onProviderEnabled(
String
provider) {
//
TODO
Auto-generatedmethod stub
}
@Override
public
void
onProviderDisabled(
String
provider) {
//
TODO
Auto-generatedmethod stub
}
@Override
public
void
onLocationChanged(
Location
location) {
Double
latitude=location.getLatitude()*1E6;
Double
longitude=location.getLongitude()*1E6;
try
{
//
获得精度纬度字符串
msg
=
"
经度:
"
+ location.getLongitude() +
"\n"
;
msg
+=
"
纬度:
"
+ location.getLatitude() +
"\n"
;
//
根据经纬度获得改点地址信息
Geocoder
gc=
new
Geocoder
(
Main
.
this
);
List<
Address
> addresses=gc.getFromLocation(latitude, longitude,1);
if
(addresses.size()>0) {
//
获得地址信息
msg
+=
"AddressLine:"
+addresses.get(0).getAddressLine(0)+
"\n"
;
//
获得国家名
msg
+=
"CountryName
:
"
+ addresses.get(0).getCountryName()+
"\n"
;
msg
+=
"Locality
:
"
+ addresses.get(0).getLocality() +
"\n"
;
msg
+=
"FeatureName
:
"
+ addresses.get(0).getFeatureName();
}
textView.setText(
msg
);
}
catch
(
Exception
e) {
e.printStackTrace();
}
}
};
//
注册位置监听器,
1
秒钟扫描
1
次
locationManager.requestLocationUpdates(provider, 1000, 0, locationListener);
}
class
MyOverlay
extends
Overlay
{
//
保证触控事件不重复操作
private
int
count
=0;
@Override
public
boolean
draw(
Canvas
canvas,
MapView
mapView,
boolean
shadow,
long
when) {
//
定义画笔
Paint
paint=
new
Paint
();
paint.setColor(
Color
.
RED
);
//
定义屏幕点
Point
screenPoint=
new
Point
();
//gps
点转屏幕点
mapView.getProjection().toPixels(
geoPoint
, screenPoint);
//
获得
gps
标志点图片
Bitmap
bitmap=
BitmapFactory
.decodeResource(getResources(),
R
.
drawable
.
flag
);
//
绘制
gps
点图片
canvas.drawBitmap(bitmap,screenPoint.
x
,screenPoint.
y
, paint);
//
绘制文字说明
canvas.drawText(
"
当前位置
"
, screenPoint.
x
, screenPoint.
y
, paint);
return
super
.draw(canvas, mapView, shadow, when);
}
@Override
public
boolean
onTouchEvent(
MotionEvent
e,
MapView
mapView) {
//
定义一个屏幕点
Point
screenPoint=
new
Point
();
//
把
gps
点变成屏幕点
mapView.getProjection().toPixels(
geoPoint
, screenPoint);
//
获得触点坐标
int
currentX=(
int
) e.getX();
int
currentY=(
int
) e.getY();
//
在
50
,
30
范围内触碰,显示当前经纬度
if
((currentX-screenPoint.
x
)>=0&&(currentX-screenPoint.
x
)<50
&&(currentY-screenPoint.
y
>=0)&&(currentY-screenPoint.
y
)<30) {
if
(
count
==0) {
new
AlertDialog
.
Builder
(
Main
.
this
).setMessage(
msg
)
.setPositiveButton(
"
确定
"
,
new
OnClickListener(){
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
count
=0;
}
}).show();
}
count
++;
}
return
super
.onTouchEvent(e, mapView);
}
}
@Override
protected
boolean
isRouteDisplayed() {
//
TODO
Auto-generatedmethod stub
return
false
;
}
}
注
1
:如果你的手机上没有
gps
定位系统,会有以下错误
08-10 13:04:55.610:ERROR/AndroidRuntime(492): java.lang.RuntimeException: Unable to start activityComponentInfo{cn.itcast.main/cn.itcast.main.Main}: java.lang.IllegalArgumentException:provider==null
注
2
:记得去
google
官网
http://code.google.com/intl/zh-CN/android/maps-api-signup.html
申请自己机器的
apikey
C:\Documents and Settings\Administrator\.android>keytool -list-keystore debug.kEystore
默认密码
android
我机器的
apikey
,你用不好使
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0EITsWoXq7NC_mgKW6yIxIViNLhMGsIW4dbmlsg
/>
最终实现效果:随着
android
客户端得移动,在地图上提供精确的当前所在位置