在android中的模拟位置

时间:2010-09-23 01:30:51

标签: android

我使用以下内容模拟我在Android中的位置。

public class GPSwork extends Activity implements LocationListener{
LocationManager locationManager;
String mocLocationProvider;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    mocLocationProvider = LocationManager.GPS_PROVIDER; 
    setLocation(28.574853,78.063201);
}

public void setLocation(double latitude, double longitude) {   
    locationManager.addTestProvider(mocLocationProvider, false, true, false, false, false, false, false, 0, 5);
    locationManager.setTestProviderEnabled(mocLocationProvider, true);
    locationManager.requestLocationUpdates(mocLocationProvider, 0, 0, this);
    Location loc = new Location(mocLocationProvider);
    loc.setTime(System.currentTimeMillis());
    loc.setLatitude(latitude);
    loc.setLongitude(longitude);
    locationManager.setTestProviderLocation(mocLocationProvider, loc);   
}

public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub      
}

public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub      
}

public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub      
}   
}

但它不起作用。我希望在真实设备中看到它并且泡泡在那个位置闪烁,但我没有得到任何东西。请更正我的代码,并帮助我。我使用了权限。

我想建立一个应用程序similatiom来定位欺骗者。实际上,当我们打开谷歌地图时,它会显示我们当前的位置,蓝色闪烁在我们的位置..同样,如果我改变坐标,即纬度和纬度,它必须显示我在那个位置,蓝色闪烁,

![替代文字] [1] 这是屏幕短......

我的XML很好

<?xml version="1.0" encoding="utf-8"?>

<com.google.android.maps.MapView 
    android:id="@+id/mapView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:apiKey="0Sy8vgJlQkFxcisUAkrxu3xN33dqFgBetCg4jxg"
    />

3 个答案:

答案 0 :(得分:2)

这是使用Google地图的应用的基本模式:

public class MapsActivity extends MapActivity 
{    

    MapView mapView;
MapController mc;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mapView = (MapView)findViewById(R.id.mapView);
        //zoom controlls
        mc = mapView.getController();
        mc.setZoom(12);
        setContentView(R.layout.main);
    }

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

在AndroidManifest.xml文件中添加:

 <uses-library android:name="com.google.android.maps" />

<uses-permission android:name="android.permission.INTERNET" />

要显示您的特定位置,请将其添加到onCreate方法:

 String coordinates[] = {"1.352566007", "103.78921587"};
 double lat = Double.parseDouble(coordinates[0]);
 double lng = Double.parseDouble(coordinates[1]);

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

 mc.animateTo(p);
 mc.setZoom(17); 
 mapView.invalidate();

最后,您需要使用地图叠加层设置标记。将以下类添加到MapsActivity类:

class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
            super.draw(canvas, mapView, shadow);                   

            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(
                getResources(), R.drawable.my_bubble);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
            return true;
        }
    } 

在此绘制方法中,您可以根据需要使用某些标记动画。现在您必须将叠加层添加到地图中:

//---Add a location marker---
 MapOverlay mapOverlay = new MapOverlay();
 List<Overlay> listOfOverlays = mapView.getOverlays();
 listOfOverlays.clear();
 listOfOverlays.add(mapOverlay);        

 mapView.invalidate();

这是一个快速概述。您需要一个谷歌API密钥才能使用谷歌地图API。有关google maps api的详细教程,请访问Using Google Maps in Android

答案 1 :(得分:0)

以下是如何在android中处理gps。让主要活动收听位置更新是一个糟糕的解决方案。使用和修改以下类:

public MyGPS implements LocationListener{

    public LocationManager lm = null;
    private MainActivity SystemService = null;
    //lat, lng
    private double mLongitude = 0;
    private double mLatitude = 0;

    public MyGPS(MainActivity sservice){
        this.SystemService = sservice;
        this.startLocationService();
    }

    public void startLocationService(){
        this.lm = (LocationManager) this.SystemService.getSystemService(Context.LOCATION_SERVICE);
        this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 5, this);
    }

    public void onLocationChanged(Location location) {
        location = this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        try {
            this.mLongitude = location.getLongitude();
            this.mLatitude = location.getLatitude();
        } catch (NullPointerException e) {
            Log.i("Null pointer exception " + mLongitude + "," + mLatitude, null);
        }
    }
}

在onCreate方法中,创建此类的实例,locationlistener将开始侦听gps更新。但是你不能访问lng和lat,因为你不知道你的活动是设置还是null。因此,您需要一个向主要活动发送消息的处理程序:

使用以下方法修改:

public void onLocationChanged(Location location) {
        location = this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        try {
            this.mLongitude = location.getLongitude();
            this.mLatitude = location.getLatitude();
            Message msg = Message.obtain();
            msg.what = UPDATE_LOCATION;
            this.SystemService.myViewUpdateHandler.sendMessage(msg);
        } catch (NullPointerException e) {
            Log.i("Null pointer exception " + mLongitude + "," + mLatitude, null);
        }
    }

在您的主要活动中添加以下内容:

Handler myViewUpdateHandler = new Handler(){

        public void handleMessage(Message msg) {
                switch (msg.what) {
                case UPDATE_LOCATION:
                //access lat and lng   
         }));
               }

                super.handleMessage(msg);
        }
};

答案 2 :(得分:0)

我认为您必须扩展MyLocationOverlay类并覆盖onDraw并将位置更改为所需位置,然后调用superclass.onDraw方法。