我一直在尝试创建一个与地图相关的Android应用程序,但我意识到我的应用程序的onLocationChanged尚未调用,因此地图始终保留在默认区域(美国)。
我的代码:
public class MapMainActivity extends MapActivity
implements OnClickListener, LocationListener {
MapView mapView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_main);
//reference
mapView = (MapView)findViewById(R.id.map_view);
mapView.setBuiltInZoomControls(true);
this.findViewById(R.id.btn_satellite).setOnClickListener(this);
this.findViewById(R.id.btn_street).setOnClickListener(this);
}//onCreate
@Override
protected void onResume() {
super.onResume();
Toast.makeText(this, "GPS tracking started",
Toast.LENGTH_SHORT).show();
// Start location updates; 5s/5m
LocationManager locManager = (LocationManager)getSystemService(
Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
5000, 0, this);
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locManager.getBestProvider(crit, true);
Location loc = locManager.getLastKnownLocation(provider);
}//onResume
@Override
protected void onPause() {
super.onPause();
Toast.makeText(this, "GPS tracking stopped",
Toast.LENGTH_SHORT).show();
LocationManager locManager = (LocationManager)getSystemService(
Context.LOCATION_SERVICE);
locManager.removeUpdates(this);
}//onPause
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == findViewById(R.id.btn_street))
{
mapView.setSatellite(false);
}//street view
else if (v == findViewById(R.id.btn_satellite))
{
mapView.setSatellite(true);
}
}//onClick
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}//isRouteDisplayed
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double lat = location.getLatitude();
double lon = location.getLongitude();
TextView txtLat = (TextView)findViewById(R.id.txt_lat);
txtLat.setText(String.format("%.6f", lat));
TextView txtLon = (TextView)findViewById(R.id.txt_lon);
txtLon.setText(String.format("%.6f", lon));
MapView map = (MapView)findViewById(R.id.map_view);
map.getController().animateTo(new GeoPoint((int)(lat*1E6),//1000000),
(int)(lon*1E6)));
}//onLocationChanged
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "GPS disabled",
Toast.LENGTH_SHORT).show();
}//onProviderDisabled
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "GPS enabled",
Toast.LENGTH_SHORT).show();
}//onProviderEnabled
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}//onStatusChanged
}//class
当我通过我的设备运行应用程序时,我的整个日志猫(16/12/2011)
D/InputTransport(5357): Input channel constructed: name='40baf680 Toast (client)', ashmemFd=54, receivePipeFd=57, sendPipeFd=58
I/MapActivity(5357): Handling network change notification:CONNECTED
E/MapActivity(5357): Couldn't get connection factory client
I/ViewRoot(5357): !@FINISH DRAWING : sg.edu.tp/sg.edu.tp.SIP_TestMapActivity
I/ViewRoot(5357): !@finishDrawing is completed : sg.edu.tp/sg.edu.tp.SIP_TestMapActivity
I/ViewRoot(5357): !@FINISH DRAWING : Toast
I/ViewRoot(5357): !@finishDrawing is completed : Toast
D/InputTransport(5357): Input channel destroyed: name='40baf680 Toast (client)', ashmemFd=54, receivePipeFd=57, sendPipeFd=58
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sg.edu.tp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".SIP_MLT_TestActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps" />
<activity android:name=".SIP_TestMapActivity"></activity>
<activity android:name=".SIP_TestDraw1Activity"></activity>
<activity android:name=".SIP_TestDraw2Activity"></activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
</manifest>
答案 0 :(得分:1)
你是在测试时是在移动时还是在用USB连接到你的开发机器并观看logcat时进行测试?
如果坐在开发机器上,我怀疑你无法移动触发位置变更事件所需的5米,就像你在requestLocationUpdates的调用中所要求的那样
来自API文档:。
如果minDistance大于0,则只有广播位置才会广播 设备按minDistance米移动。
尝试将该调用中的最小距离参数减少到零,看看会发生什么。
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000, 0, this);
除了测试之外,您可能不希望将其保留为零。
如果这没有帮助,在你请求locationUpdates后,你可能会尝试通过在你的onResume上做类似的事情看到你的GPS正在工作:
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(crit, true);
Location loc = lm.getLastKnownLocation(provider);
并查看您是否获得了良好的位置信息。
答案 1 :(得分:0)
位置stuiff很棘手。一方面,您需要快速的最近位置,另一方面可能具有最佳精度,您不喜欢快速耗尽电池。 API级别之间也存在差异。有关详细说明,请参阅本手册:
http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html
至于您的原始问题 - 要求位置更新只是对位置管理员的推荐,以便在发生更改后向您发送这些更新。没有义务这样做 所以。您的GPS系统可能不够精确(当然不是),以便在USB电缆上移动时检测到snall位置的变化。出于测试目的,您可以创建模拟提供程序 并使用其他应用来操纵它。