0

我正在尝试从 GPS 获取坐标(这实际上给出了 Toast 的 lat 和 long 的值。)我正在尝试获取该值并将其用作地图坐标并显示当前位置,但它没有显示它。我哪里出错了?

public class Map extends MapActivity {

    MapController mControl;
    GeoPoint GeoP;
    MapView mapV;


    public double lat;
    public double longi;

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

    protected LocationManager locationManager;

    @Override

    public void onCreate (Bundle saveInstanceState){
        super.onCreate(saveInstanceState);
        setContentView(R.layout.map);


        mapV = (MapView)findViewById(R.id.mapview);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 
                MINIMUM_TIME_BETWEEN_UPDATES, 
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                new MyLocationListener()
        );

        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {

            lat = location.getLatitude();
            longi = location.getLongitude();

        }

        String message = String.format(
                "New Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude()
        );
        Toast.makeText(Map.this, message, Toast.LENGTH_LONG).show();



        GeoP = new GeoPoint ((int)(lat*1E6),(int)(longi * 1E6));


        mControl = mapV.getController();
        mControl.animateTo(GeoP);
        mControl.setZoom(13);


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

    private class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location location) {
            String message = String.format(
                    "New Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude()
            );
            Toast.makeText(Map.this, message, Toast.LENGTH_LONG).show();
        }

        @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

        }

}
}
4

3 回答 3

1

嘿,我已经使用这个网站作为例子来实现,请看一下它可能会帮助你 http://mobiforge.com/developing/story/using-google-maps-android http://www.vogella.com/articles/ AndroidLocationAPI/article.html

于 2012-06-15T06:51:07.403 回答
1

我想知道为什么您不打算在 onLocationChanged 回调中显示位置。当您的设备获取某个位置时,首先会调用此方法。

所以在 onLocationChanged 中包含以下代码

它应该是这样的。

 @Override
 public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude() * 1E6);
        int lng = (int) (location.getLongitude() * 1E6);
        GeoPoint point = new GeoPoint(lat, lng);
        mControl.animateTo(point); // mapController.setCenter(point);

        }
于 2012-06-15T07:02:30.597 回答
0
package m.a.p;

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

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

public class MappingActivity extends MapActivity {
    /** Called when the activity is first created. */

    MapController mControl;
    GeoPoint GeoP;
    MapView mapV;

    public double lat;
    public double lng;

     private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
     private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

    protected LocationManager locationManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapV = (MapView) findViewById(R.id.mapview);

//        double lat = 40.8;
//        double lng = -96.66;


        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 
                MINIMUM_TIME_BETWEEN_UPDATES, 
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                new MyLocationListener()
        );

        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {

            lat = location.getLatitude();
            lng = location.getLongitude();

        }

        String message = String.format(
                "New Location \n Longitude: %1$s \n Latitude: %2$s",
                lat, lng
        );
        Toast.makeText(MappingActivity.this, message, Toast.LENGTH_LONG).show();



        GeoP = new GeoPoint ((int)(lat*1E6),(int)(lng * 1E6));



  //      GeoP = new GeoPoint ((int)(lat * 1E6),(int)(lng*1E6));

        mControl = mapV.getController();
        mControl.animateTo(GeoP);
        mControl.setZoom(13);
    }

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


    public class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            String message = String.format(
                    "New Location \n Longitude: %1$s \n Latitude: %2$s",
                    lat, lng
            );
            Toast.makeText(MappingActivity.this, message, Toast.LENGTH_LONG).show();
        }

        @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

        }

}
}

谢谢大家的帮助。它终于奏效了

于 2012-06-15T08:57:17.680 回答