在近来开发百度地图定位功能时有出现了java.lang.ClassCastException: android.app.Application cannot be cast to com.example.maplocation.LocationApplication问题,其解决方案为在manifest.xml文件中进行配置。在Application节点下添加android:name="自己写的application"。在本代码中就应该写:
<application
android:name="com.example.maplocation.LocationApplication">
另将百度地图定位功能核心代码贴出
public class LocationActivity extends Activity{
private LocationClient mLocationClient;
private TextView LocationResult,ModeInfor;
private Button startLocation;
private RadioGroup selectMode,selectCoordinates;
private EditText frequence;
private LocationMode tempMode = LocationMode.Hight_Accuracy;
private String tempcoor="gcj02";
private CheckBox checkGeoLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.location_main);
mLocationClient = ((LocationApplication)getApplication()).mLocationClient;
//此处要注意,如果在mainest.xml中不进行application声明
//就会抛出异常,classcastexception
LocationResult = (TextView)findViewById(R.id.tv_result);
ModeInfor= (TextView)findViewById(R.id.modeinfor);
ModeInfor.setText(getString(R.string.hight_accuracy_desc));
((LocationApplication)getApplication()).mLocationResult = LocationResult;
frequence = (EditText)findViewById(R.id.frequence);
checkGeoLocation = (CheckBox)findViewById(R.id.geolocation);
startLocation = (Button)findViewById(R.id.addfence);
startLocation.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
InitLocation();
if(startLocation.getText().equals(getString(R.string.startlocation))){
mLocationClient.start();
startLocation.setText(getString(R.string.stoplocation));
}else{
mLocationClient.stop();
startLocation.setText(getString(R.string.startlocation));
}
}
});
selectMode = (RadioGroup)findViewById(R.id.selectMode);
selectCoordinates= (RadioGroup)findViewById(R.id.selectCoordinates);
selectMode.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
String ModeInformation = null;
switch (checkedId) {
case R.id.radio_hight:
tempMode = LocationMode.Hight_Accuracy;
ModeInformation = getString(R.string.hight_accuracy_desc);
break;
case R.id.radio_low:
tempMode = LocationMode.Battery_Saving;
ModeInformation = getString(R.string.saving_battery_desc);
break;
case R.id.radio_device:
tempMode = LocationMode.Device_Sensors;
ModeInformation = getString(R.string.device_sensor_desc);
break;
default:
break;
}
ModeInfor.setText(ModeInformation);
}
});
selectCoordinates.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.radio_gcj02:
tempcoor="gcj02";
break;
case R.id.radio_bd09ll:
tempcoor="bd09ll";
break;
case R.id.radio_bd09:
tempcoor="bd09";
break;
default:
break;
}
}
});
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
mLocationClient.stop();
super.onStop();
}
private void InitLocation(){
LocationClientOption option = new LocationClientOption();
option.setLocationMode(tempMode);//设置定位模式
option.setCoorType(tempcoor);//返回的定位结果是百度经纬度,默认值gcj02
int span=1000;
//设置刷新时间频率
try {
span = Integer.valueOf(frequence.getText().toString());
} catch (Exception e) {
// TODO: handle exception
}
option.setScanSpan(span);//设置发起定位请求的间隔时间为5000ms
option.setIsNeedAddress(checkGeoLocation.isChecked());
mLocationClient.setLocOption(option);
}
package com.example.maplocation;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.GeofenceClient;
import com.baidu.location.LocationClient;
import android.app.Application;
import android.app.Service;
import android.os.Vibrator;
import android.util.Log;
import android.widget.TextView;
/**
* 主Application
*/
public class LocationApplication extends Application {
public LocationClient mLocationClient;
public GeofenceClient mGeofenceClient;
public MyLocationListener mMyLocationListener;
public TextView mLocationResult,logMsg;
public TextView trigger,exit;
public Vibrator mVibrator;
@Override
public void onCreate() {
super.onCreate();
mLocationClient = new LocationClient(this.getApplicationContext());
mMyLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(mMyLocationListener);
}
/**
* 实现实位回调监听
*/
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
//Receive Location
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
sb.append(location.getTime());
sb.append("\nerror code : ");
sb.append(location.getLocType());
sb.append("\nlatitude : ");
sb.append(location.getLatitude());
sb.append("\nlontitude : ");
sb.append(location.getLongitude());
sb.append("\nradius : ");
sb.append(location.getRadius());
logMsg(sb.toString());
Log.i("BaiduLocationApiDem", sb.toString());
}
}
/**
* 显示请求字符串
* @param str
*/
public void logMsg(String str) {
try {
if (mLocationResult != null)
mLocationResult.setText(str);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 高精度地理围栏回调
* @author jpren
*
*/
}
其中页面布局和主MainActivity类很简单就不再贴出,如果想要详细的研究百度地图最好还是看百度地图api
点击打开链接。并且在开发时一定要注意key的配置、相关权限和application的配置。