问题描述
- Android百度地图API onReceiveLocation
- onReceiveLocation 在模拟器上运行可以触发 移动到真机上onReceiveLocation触发不了
不知道为什么
<?xml version=""1.0"" encoding=""utf-8""?>
package=""com.example.baidumapdingwei""
android:versionCode=""1""
android:versionName=""1.0"" ><uses-sdk android:minSdkVersion=""19"" android:targetSdkVersion=""19"" /><application android:allowBackup=""true"" android:icon=""@drawable/ic_launcher"" android:label=""@string/app_name"" android:theme=""@style/AppTheme"" > <!-- service --> <service android:name=""com.baidu.location.f"" android:enabled=""true"" android:process="":remote""> </service> <!-- meta-data需要写在application中 --> <meta-data android:name=""com.baidu.lbsapi.API_KEY"" android:value=""66iOAPAecwuaafXjw2XgaAGf"" /> <activity android:name=""com.example.baidumapdingwei.MainActivity"" 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.ACCESS_COARSE_LOCATION"" > </uses-permission> <uses-permission android:name=""android.permission.ACCESS_FINE_LOCATION"" > </uses-permission> <uses-permission android:name=""android.permission.ACCESS_WIFI_STATE"" > </uses-permission> <uses-permission android:name=""android.permission.ACCESS_NETWORK_STATE"" > </uses-permission> <uses-permission android:name=""android.permission.CHANGE_WIFI_STATE"" > </uses-permission> <uses-permission android:name=""android.permission.READ_PHONE_STATE"" > </uses-permission> <uses-permission android:name=""android.permission.WRITE_EXTERNAL_STORAGE"" > </uses-permission> <uses-permission android:name=""android.permission.INTERNET"" /> <uses-permission android:name=""android.permission.MOUNT_UNMOUNT_FILESYSTEMS"" > </uses-permission> <uses-permission android:name=""android.permission.READ_LOGS"" > </uses-permission> <uses-permission android:name=""android.permission.VIBRATE"" /> <uses-permission android:name=""android.permission.WAKE_LOCK"" /> <uses-permission android:name=""android.permission.WRITE_SETTINGS"" />
java
public class MainActivity extends Activity {
private LocationClient mLocationClient;
private TextView LocationResult;
private Button startLocation;
private BDLocation location1;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLocationClient = new LocationClient(getApplicationContext()); mLocationClient.registerLocationListener(new MyLocationListener()); LocationResult = (TextView)findViewById(R.id.textView1); startLocation = (Button)findViewById(R.id.button1); //startLocation.setOnClickListener(new OnClickListener() { }public void onClick(View v) { // TODO Auto-generated method stub InitLocation(); if(startLocation.getText().equals(""start"")){ mLocationClient.start(); //mLocationClient.requestLocation(); startLocation.setText(""stop""); }else{ mLocationClient.stop(); startLocation.setText(""start""); } }@Overridepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main menu); return true;}@Overrideprotected void onStop() { // TODO Auto-generated method stub mLocationClient.stop(); super.onStop();}private void InitLocation(){ LocationClientOption option = new LocationClientOption(); //location1 = new LocationClient(getApplicationContext()); option.setLocationMode(LocationMode.Hight_Accuracy);//设置定位模式 option.setCoorType(""gcj02"");//返回的定位结果是百度经纬度,默认值gcj02 option.setScanSpan(5000);//设置发起定位请求的间隔时间为100ms option.setIsNeedAddress(true); option.setOpenGps(true); mLocationClient.setLocOption(option);}/** * 实现实位回调监听 */public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { //Receive Location location1=location; StringBuffer sb = new StringBuffer(256); sb.append(""time : ""); sb.append(location.getTime()); sb.append(""error code : ""); sb.append(location.getLocType()); sb.append(""latitude : ""); sb.append(location.getLatitude()); sb.append(""lontitude : ""); sb.append(location.getLongitude()); sb.append(""radius : ""); sb.append(location.getRadius()); if (location.getLocType() == BDLocation.TypeGpsLocation){ sb.append(""speed : ""); sb.append(location.getSpeed()); sb.append(""satellite : ""); sb.append(location.getSatelliteNumber()); sb.append(""direction : ""); sb.append(""addr : ""); sb.append(location.getAddrStr()); sb.append(location.getDirection()); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){ sb.append(""addr : ""); sb.append(location.getAddrStr()); //运营商信息 sb.append(""operationers : ""); sb.append(location.getOperators()); } int flag=location.getLocType(); String a=""error : ""; a=a+flag; sb.append(a); Toast.makeText(MainActivity.thissb"" Toast.LENGTH_SHORT); Log.i(""dwtedx"" sb.toString()); }}
}
布局 里面只有一个显示的TextView 定位的Button
解决方案
<meta-data android:name=""com.baidu.lbsapi.API_KEY"" android:value=""66iOAPAecwuaafXjw2XgaAGf"" /> 这个 你可能要再申请一个key 你使用的多半是debug的key
解决方案二:
参考如下blog:
http://blog.csdn.net/crazy1235/article/details/43898451
时间: 2024-12-21 05:24:01