通过百度API获取经纬度

阅读更多
第一步,先要导入百度API的架包libBMapApiEngine_v1_3_3.so和baidumapapi.jar,且libBMapApiEngine_v1_3_3.so包要放到armeabi文件夹下,armeabi和baidumapapi.jar平级。
(附件中的libBMapApiEngine_v1_3_3.zip要解压,解压后把libBMapApiEngine_v1_3_3.so包要放到armeabi文件夹下就行了)

第二步,在布局页面里要加上如下代码


     
    

    
    
    



第三步:写代码

import android.location.Location;
import android.os.Bundle;
import android.widget.TextView;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKLocationManager;
import com.baidu.mapapi.MapActivity;

/**
 * 创建Activity(继承com.baidu.mapapi.MapActivity)
 */
public class LocationActivity extends MapActivity implements LocationListener {
	private BMapManager mapManager;
	private MKLocationManager mLocationManager = null;

	private TextView latText;
	private TextView lonText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		latText = (TextView) findViewById(R.id.lat);
		lonText = (TextView) findViewById(R.id.lon);

		// 初始化MapActivity
		mapManager = new BMapManager(getApplication());
		// init方法的第一个参数需填入申请的API Key
		mapManager.init("285B415EBAB2A92293E85502150ADA7F03C777C4", null);
		super.initMapActivity(mapManager);

		mLocationManager = mapManager.getLocationManager();
		// 注册位置更新事件
		mLocationManager.requestLocationUpdates(this);
		// 使用GPS定位
		mLocationManager.enableProvider((int) MKLocationManager.MK_GPS_PROVIDER);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	protected void onDestroy() {
		if (mapManager != null) {
			mapManager.destroy();
			mapManager = null;
		}
		mLocationManager = null;
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (mapManager != null) {
			mapManager.stop();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		if (mapManager != null) {
			mapManager.start();
		}
		super.onResume();
	}

	/**
	 * 根据MyLocationOverlay配置的属性确定是否在地图上显示当前位置
	 */
	@Override
	protected boolean isLocationDisplayed() {
		return false;
	}

	/**
	 * 当位置发生变化时触发此方法
	 * 
	 * @param location 当前位置
	 */
	@Override
	public void onLocationChanged(Location location) {
		if (location != null) {
			// 显示定位结果
			lonText.setText("当前经度:" + location.getLongitude());
			latText.setText("当前纬度:" + location.getLatitude());
		}
	}
}



最后:添加权限


    
    
    
    
    
    
    
    
    
    
    
    
    
  • baidumapapi.jar (231.5 KB)
  • 下载次数: 98
  • libBMapApiEngine_v1_3_3.zip (593.8 KB)
  • 下载次数: 144
  • location.zip (857.7 KB)
  • 下载次数: 238

你可能感兴趣的:(android,百度API,经度,纬度)