Android_GPS定位
GPS定位
一. 知识点
1. Android的GPS定位
2. 地图的放大/缩小功能
二. 代码解析
1. DingWeiActivity
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
public class DingWeiActivity extends Activity {
/** Called when the activity is first created. */
private MapView map=null;
private GraphicsLayer gps = null;// 定位图层
private ImageView button1 = null;// 放大按钮
private ImageView button2 = null;// 缩小按钮
private ImageView button5 = null;// 定位按钮
LocationManager loc = null;//提供访问定位服务的功能,也提供获取最佳定位提供者的功能等
private TextView weizhi = null;// 显示当前位置
Point ptLatLon;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建并初始化地图
map=(MapView)findViewById(R.id.map);
gps = (GraphicsLayer) findViewById(R.id.gps);
//通过使用Context.getSystemService方法,传入Context.LOCATION_SERVICE参数获取定位管理器的实例
loc = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//将上一次LocationManager获得的有效位置信息以Location对象的形式返回
Object init = getLastNonConfigurationInstance();
if (init != null) {
map.restoreState((String) init);
}
gps.setRenderer(new SimpleRenderer(new SimpleMarkerSymbol(Color.GREEN,
7, SimpleMarkerSymbol.STYLE.CIRCLE)));
weizhi = (TextView) findViewById(R.id.weizhi);
button1 = (ImageView) findViewById(R.id.button1);
button2 = (ImageView) findViewById(R.id.button2);
button5 = (ImageView) findViewById(R.id.dingwei);
button1.setAdjustViewBounds(true);
button2.setAdjustViewBounds(true);
button5.setAdjustViewBounds(true);
button1.setMaxHeight(35);
button2.setMaxHeight(35);
button5.setMaxHeight(45);
button5.setMaxWidth(45);
// 放大、缩小
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
DingWeiActivity.this.map.zoomout();
}
});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
DingWeiActivity.this.map.zoomin();
}
});
// 定位功能
button5.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//用户的位置发生10m距离的改变之后进行调用,更新位置信息的最小间隔为2s。这里默认使用的LocationProvider是GPS_PROVIDER
loc.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10,
new LocationListener() {
@SuppressWarnings("static-access")
public void onLocationChanged(Location location) {
ptLatLon = new Point(location.getLongitude(),
location.getLatitude());
Graphic g = new Graphic();
g.setGeometry(ptLatLon);
DingWeiActivity.this.gps.addGraphic(g);
DingWeiActivity.this.gps.postInvalidate();
SpatialReference sr4326 = SpatialReference
.create(4326);
Point ptMap = (Point) GeometryEngine.project(
ptLatLon, sr4326, map
.getSpatialReference());
map.centerAt(ptMap);
// 更改左下角显示当前位置
location = loc
.getLastKnownLocation(loc.GPS_PROVIDER);
// 判断是否定位
if (location != null) {
weizhi.setText("X:"
+ location.getLongitude() + "\nY:"
+ location.getLatitude());
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider,
int status, Bundle extras) {
}
});
}
});
}
}
2.main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/relatLay">
<!--
MapView是基于Android中ViewGroup(可以作为其它View或ViewGroup的容器)的一个类, ArcGIS
Android API中使用MapView作为地图的容器,这和很多ArcGIS API中的Map、 MapControl类的作用是一样的。
-->
<com.esri.android.map.MapView
xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<com.esri.android.map.ags.ArcGISTiledMapServiceLayer
url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer" />
<com.esri.android.map.GraphicsLayer
android:id="@+id/gps" />
</com.esri.android.map.MapView>
<!-- 定位 -->
<TableLayout android:id="@+id/topTableLay"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#6b8bc6">
<TableRow android:gravity="right">
<ImageView android:id="@+id/dingwei" android:src="@drawable/gps"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</TableRow>
</TableLayout>
<!-- 显示位置 -->
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true">
<TextView android:id="@+id/weizhi" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="@android:color/black" />
</LinearLayout>
<!-- 放大/缩小 -->
<LinearLayout android:id="@+id/centerLin"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentBottom="true"
android:layout_alignParentRight="true">
<ImageView android:id="@+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/zoomout" />
<ImageView android:id="@+id/button2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/zoomin" />
</LinearLayout>
</RelativeLayout>
3.DingWeiManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.text" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".DingWeiActivity" 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>
<!-- 设置权限 -->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
</manifest>
4. assets文件夹下添加zoomin.xml和zoomout.xml两个文件
1) zoomin.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<scale android:fromXScale="2.0" android:toXScale="1.0"
android:fromYScale="2.0" android:toYScale="1.0"
android:pivotX="50%p" android:pivotY="50%p"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
2) zoomout.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:zAdjustment="top">
<scale android:fromXScale="1.0" android:toXScale=".5"
android:fromYScale="1.0" android:toYScale=".5"
android:pivotX="50%p" android:pivotY="50%p"
android:duration="@android:integer/config_mediumAnimTime" />
<alpha android:fromAlpha="1.0" android:toAlpha="0"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>