0

我尝试制作自己的应用程序并使用谷歌地图。我希望它在我当前的 gps 位置上设置地图的中心,但是当我的手机上有 gps 锁定时,我只会转到这些坐标 (0,0) 我不知道我哪里出错了。谢谢大家 :D

import android.content.Context;
import android.location.Location;
import android.location.LocationListener; 
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;


public class Courses extends MapActivity {

MapView map;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.courses);       
    MapView map = (MapView) findViewById (R.id.MapView);
    map.setBuiltInZoomControls(true);
    map.setSatellite(true);
    final MapController control = map.getController();


    LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    LocationListener listener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            control.setCenter(new GeoPoint((int)location.getLatitude(),(int)location.getLongitude()));              
            control.setZoom(19);
        }

        @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

        }

    };


manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);

}


@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

} `

4

1 回答 1

0

我在这里看到的第一个问题是location.getLatitude()location.getLongitude()返回float,它必须乘以 1E6 然后转换为int才能被GeoPoint接受。这也解释了为什么您的坐标约为 0,0

我建议control.setCenter(new GeoPoint((int)location.getLatitude(),(int)location.getLongitude()));control.setCenter(new GeoPoint((int)(location.getLatitude() * 1E6),(int)(location.getLongitude() * 1E6)));

试试看,它应该可以工作。

于 2012-06-27T10:34:44.683 回答