android:mapView没有给我正确的用户位置

发布于 2024-10-18 01:42:27 字数 2449 浏览 1 评论 0原文

作为主题,我目前在瑞典,但在启动我的地图时,它显示我在英国。

我的代码如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱格式化 2024-10-25 01:42:27

您实际上创建了错误的地理点。您在此处设置纬度和海拔高度。

test = new GeoPoint((int)location.getLatitude()*1000000, (int)location.getAltitude()*1000000)

您需要设置纬度和经度

test = new GeoPoint((int)location.getLatitude()*1000000, (int)location.getLongitude()*1000000)

另请尝试查看 MyLocationOverlay

You are actually creating the geo point wrong. You are setting the latitude and altitude here.

test = new GeoPoint((int)location.getLatitude()*1000000, (int)location.getAltitude()*1000000)

You'll want to set the latitude and longitude

test = new GeoPoint((int)location.getLatitude()*1000000, (int)location.getLongitude()*1000000)

Also try checking out the MyLocationOverlay class

北方的韩爷 2024-10-25 01:42:27

它在进行乘法之前会转换为 int,因此计算时需要括号。
换成这个。

test = new GeoPoint((int)(location.getLatitude()*1000000), 
     (int)(location.getLongitude()*1000000));

您可能还想查看 MyLocationOverlay 如果您想在地图上绘制您的位置。

使用 MyLocationOverlay。

MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableMyLocation();

在暂停时,您还应该调用 myLocationOverlay.disableMyLocation() ,这样当您的活动在后台时它就不会耗尽电池。

it is converting to an int before it does the multiplication so you need parens around the calculation.
Replace with this.

test = new GeoPoint((int)(location.getLatitude()*1000000), 
     (int)(location.getLongitude()*1000000));

You also might want to check out MyLocationOverlay if you want to draw your location on a map.

To use MyLocationOverlay.

MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableMyLocation();

In on pause you should also call myLocationOverlay.disableMyLocation() so it doesn't drain battery when your activity is in the background.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文