1

我试图让我的模拟器指示我在地图上的位置,问题是我没有得到我的真实位置,也没有指南针我只在计算机上使用模拟器我整合了谷歌地图、密钥、导入manifest.xml 这里是代码:

     package com.training.surveyproject;
     import android.location.Location;
     import android.location.LocationListener;
     import android.location.LocationManager;
     import android.os.Bundle;
     import android.view.KeyEvent;
     import android.widget.Toast;

     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 com.google.android.maps.MyLocationOverlay;

     public class SurveyActivity extends MapActivity implements LocationListener
     {

        private MapView mapView = null;
        private LocationManager lm = null;
        private double lat = 0;
        private double lng = 0;
        private MapController mc = null;
        private MyLocationOverlay myLocation = null;

        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_survey);
        mapView = (MapView) this.findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);

        lm = (LocationManager) this.getSystemService(LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, this);
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, this);

        mc = mapView.getController();
        mc.setZoom(12);
        myLocation = new MyLocationOverlay(getApplicationContext(),mapView); 
        myLocation.runOnFirstFix(new Runnable(){
    public void run(){
     mc.animateTo(myLocation.getMyLocation());
     mc.setZoom(17);
      }
       });
       mapView.getOverlays().add(myLocation);
       myLocation.enableMyLocation();
       myLocation.enableCompass();
             }

       @Override
       public boolean onKeyDown(int keyCode, KeyEvent event)
                {
                if (keyCode == KeyEvent.KEYCODE_S)
                {
      mapView.setSatellite(!mapView.isSatellite());
      return true;
             }
       return super.onKeyDown(keyCode, event);
      }

      @Override
      public void onLocationChanged(Location location)
      {
            lat = location.getLatitude();
            lng = location.getLongitude();
            Toast.makeText(getBaseContext(),
            "Location change to : Latitude = " + lat + " Longitude = " + lng,
            Toast.LENGTH_SHORT).show();
            GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
            mc.animateTo(p);
            mc.setCenter(p);
            }

            @Override
            protected boolean isRouteDisplayed()
               {
                    return false;
               }

          @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

1 回答 1

0

您可以通过打开 DDMS 透视图和其中的“模拟器控制”选项卡将模拟 GPS 修复发送到模拟器。该选项卡有一个“位置控制”选项,它将把纬度/经度发送到模拟器。

于 2012-07-11T18:54:03.737 回答