最近做的项目用到了GPS、Wifi、基站三种不同方式进行定位,研究一下发现高德地图提供的智能选择定位方式进行定位较好,就写了下面相关代码进行定位并在地图上显示。有些写的不详细,大家可以去参考官方文档。
1.java代码:MyActivity
package com.gu.ALocationByGD;
import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
/**
* 本实例采用高德的智能定位包进行的智能选择定位方式进行定位,wifi网络、周围基站以及gps
*/
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.location).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//开始定位
initLocation();
}
});
}
//定位管理服务
private LocationManagerProxy mLocationManagerProxy;
//停止定位,在定位完不需要再次定位是关闭定位服务,释放资源
private void stopLocation() {
if (mLocationManagerProxy != null) {
mLocationManagerProxy.removeUpdates(amapLocationListener);
mLocationManagerProxy.destory();
}
mLocationManagerProxy = null;
}
//初始化定位
private void initLocation() {
mLocationManagerProxy = LocationManagerProxy.getInstance(this);
//此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
//注意设置合适的定位时间的间隔,并且在合适时间调用removeUpdates()方法来取消定位请求
//在定位结束后,在合适的生命周期调用destroy()方法
//其中如果间隔时间为-1,则定位只定一次
mLocationManagerProxy.requestLocationUpdates(LocationProviderProxy.AMapNetwork, -1, 1, amapLocationListener);
mLocationManagerProxy.setGpsEnable(false);
}
//定位监听
private AMapLocationListener amapLocationListener = new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (aMapLocation != null) {
saveDisconnectLocation(aMapLocation);
}
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
double lat, lon;
//保存定位到的坐标位置,并跳转到地图界面进行定位
private void saveDisconnectLocation(AMapLocation amaplocation) {
lon = amaplocation.getLongitude();
lat = amaplocation.getLatitude();
// disconnect record
if (lon != 0 && lat != 0) {
stopLocation();
Intent intent = new Intent();
intent.setClass(MyActivity.this, MapActivity.class);
intent.putExtra("lat", lat);
intent.putExtra("lon", lon);
startActivity(intent);
finish();
}
}
}
相应xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动选择gps、wifi、基站定位"
android:textColor="#ff00ff"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" />
<Button
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location"
android:textColor="#ff00ff"
android:padding="10dp"
android:layout_centerInParent="true" />
</RelativeLayout>
2.java代码:MapActivity
package com.gu.ALocationByGD;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Toast;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import java.text.SimpleDateFormat;
/**
* Todo:地图显示定位信息
*
* @author moxiang
* @version V1.0
* @data 2014/10/14
*/
public class MapActivity extends Activity implements Runnable, AMap.OnMarkerDragListener,
AMap.OnMapLoadedListener, AMap.OnMarkerClickListener, AMap.OnInfoWindowClickListener, AMap.InfoWindowAdapter,
GeocodeSearch.OnGeocodeSearchListener {
private static final String TAG = MapActivity.class.getSimpleName();
protected LatLonPoint mLatLonPoint = null;
private double latitude;
private double longitude;
private MapView mapView;
private AMap mAMap;
private String addressName;
private GeocodeSearch geocodeSearch;
private ProgressDialog progDialog = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.map_layout);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
init();
Intent intent = getIntent();
latitude = intent.getDoubleExtra("lat", 0);
longitude = intent.getDoubleExtra("lon", 0);
Log.e(TAG, "lat: " + latitude + " lon: " + longitude);
if (latitude != 0 && longitude != 0) {
mLatLonPoint = new LatLonPoint(latitude, longitude);
getAddressByLatLng(mLatLonPoint);
}
}
private void init() {
if (mAMap == null) {
mAMap = mapView.getMap();
geocodeSearch = new GeocodeSearch(this);
geocodeSearch.setOnGeocodeSearchListener(this);
setUpMap();
progDialog = new ProgressDialog(this);
}
}
private void setUpMap() {
mAMap.setOnMarkerDragListener(this);
mAMap.setOnMapLoadedListener(this);
mAMap.setOnMarkerClickListener(this);
mAMap.setOnInfoWindowClickListener(this);
mAMap.setInfoWindowAdapter(this);
}
// 根据经纬度获取地址
public void getAddressByLatLng(final LatLonPoint latLngPoint) {
showDialog();
RegeocodeQuery query = new RegeocodeQuery(latLngPoint, 200, GeocodeSearch.AMAP);
geocodeSearch.getFromLocationAsyn(query);
}
public void showDialog() {
progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDialog.setIndeterminate(false);
progDialog.setCancelable(true);
progDialog.setMessage(getString(R.string.map_loading));
progDialog.show();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
return null;
}
@Override
public void onInfoWindowClick(Marker marker) {
}
@Override
public void onMapLoaded() {
}
@Override
public boolean onMarkerClick(Marker marker) {
return false;
}
@Override
public void onMarkerDragStart(Marker marker) {
}
@Override
public void onMarkerDrag(Marker marker) {
}
@Override
public void onMarkerDragEnd(Marker marker) {
}
@Override
public void run() {
}
@Override
public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
dismissDialog();
if (rCode == 0) {
if (result != null && result.getRegeocodeAddress() != null
&& result.getRegeocodeAddress().getFormatAddress() != null) {
addressName = result.getRegeocodeAddress().getFormatAddress() + "\n " + getDate() + " " + getTime();
addPositionAnnotation(latitude, longitude, addressName);
}
} else {
Toast.makeText(this, getString(R.string.map_neterr), Toast.LENGTH_SHORT).show();
}
}
//获取系统日期
public String getDate() {
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
return sDateFormat.format(new java.util.Date());
}
//获取系统时间
public String getTime() {
SimpleDateFormat sDateFormat = new SimpleDateFormat("hh:mm:ss");
return sDateFormat.format(new java.util.Date());
}
@Override
public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
}
public void addPositionAnnotation(double latitude, double longitude, String address) {
/*
*第一个参数是包含经纬度的对象
* 第二个参数是地图的放大倍数
*/
mAMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 16));
//地址标注
Marker marker = mAMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
.position(new LatLng(latitude, longitude))//地图上的位置
.title(address)//要显示的内容
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.draggable(false));
marker.showInfoWindow();
}
public void dismissDialog() {
if (progDialog != null) {
progDialog.dismiss();
}
}
}
相应xml代码:
<?xml version="1.0" encoding="utf-8"?>
<com.amap.api.maps2d.MapView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
3.manifest配置:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="key值" />
显示效果如下:
代码包下载地址:http://download.csdn.net/detail/u013899706/8038193
转载注明出处