android:mapView没有给我正确的用户位置
作为主题,我目前在瑞典,但在启动我的地图时,它显示我在英国。
我的代码如下所示:
private MapListener myMapViewListener;
private MapController mapController;
private GeoPoint test;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
setupListener();
MapView mapView = (MapView) findViewById(R.id.mapview);
mapController = mapView.getController();
mapView.setBuiltInZoomControls(true);
mapView.setClickable(true);
mapController.setZoom(7); // defualt zoom
}
private void setupListener() {
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
MapListener listener = new MapListener();
MapView mapView = (MapView) findViewById(R.id.mapview);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
test = new GeoPoint((int)location.getLatitude()*1000000, (int)location.getAltitude()*1000000);
mapController.animateTo(test);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
}
我
<uses-library android:name="com.google.android.maps" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
<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_NETWORK_STATE"></uses-permission>
</manifest>
在清单中添加了..............。
我正在使用 http://developer.android.com/guide /topics/location/obtaining-user-location.html 作为指南。
非常感谢帮助!
As topic I'm currently in sweden but when launching my map it shows that I'm in England.
My code looks like this:
private MapListener myMapViewListener;
private MapController mapController;
private GeoPoint test;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
setupListener();
MapView mapView = (MapView) findViewById(R.id.mapview);
mapController = mapView.getController();
mapView.setBuiltInZoomControls(true);
mapView.setClickable(true);
mapController.setZoom(7); // defualt zoom
}
private void setupListener() {
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
MapListener listener = new MapListener();
MapView mapView = (MapView) findViewById(R.id.mapview);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
test = new GeoPoint((int)location.getLatitude()*1000000, (int)location.getAltitude()*1000000);
mapController.animateTo(test);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
}
I've added........
<uses-library android:name="com.google.android.maps" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
<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_NETWORK_STATE"></uses-permission>
</manifest>
..... in my manifest.
I'm using http://developer.android.com/guide/topics/location/obtaining-user-location.html as a guide.
Very thankful for help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上创建了错误的地理点。您在此处设置纬度和海拔高度。
您需要设置纬度和经度
另请尝试查看 MyLocationOverlay 类
You are actually creating the geo point wrong. You are setting the latitude and altitude here.
You'll want to set the latitude and longitude
Also try checking out the MyLocationOverlay class
它在进行乘法之前会转换为 int,因此计算时需要括号。
换成这个。
您可能还想查看 MyLocationOverlay 如果您想在地图上绘制您的位置。
使用 MyLocationOverlay。
在暂停时,您还应该调用 myLocationOverlay.disableMyLocation() ,这样当您的活动在后台时它就不会耗尽电池。
it is converting to an int before it does the multiplication so you need parens around the calculation.
Replace with this.
You also might want to check out MyLocationOverlay if you want to draw your location on a map.
To use MyLocationOverlay.
In on pause you should also call myLocationOverlay.disableMyLocation() so it doesn't drain battery when your activity is in the background.