安卓中的模拟位置

3
我正在使用以下内容来模拟我的安卓设备位置。
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      
}   
}

但是它不起作用。我想在真实设备上看到泡泡在那个位置闪烁,但我什么都没有得到。请纠正我的代码,并帮助我解决问题。我已经使用了权限。
我想要构建一个类似于位置模拟器的应用程序。实际上,当我们打开谷歌地图时,它会显示我们当前位置,并在我们的位置处以蓝色闪烁显示。同样,如果我更改坐标即纬度和经度,它应该显示我在那个位置,并显示蓝色闪烁。
这是屏幕快照:
![alt text][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个回答

2
这是一个使用谷歌地图的应用程序的基本框架:
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。有关谷歌地图API的详细教程,请参阅此处:在Android中使用Google Maps

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。因此,您需要一个处理程序,向主要活动发送消息:
在以下方法中进行修改:
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);
        }
};

好的,请在您的问题中添加您想要设置地图位置的内容,因为这一点不够清晰。 - DarkLeafyGreen
@ArtWork 感谢您的回答。我按照步骤生成了谷歌地图API密钥并成功获取了它。但是我只能看到网格线,无法显示地图。为什么会这样呢?我已经正确设置了权限和XML文件,但仍然无法显示地图。 - user455422
你说的格线是什么意思?能发一下截图吗?也许你的布局XML配置错误了。 - DarkLeafyGreen
嗨,ArtWork,我想贴快照,但由于我的声望不到10,我无法贴快照。网格线意味着它只显示正方形框而不是地图,我没有得到任何地图。我已经发布了XML。 - user455422
只需在您的设备上部署应用程序。Eclipse会为您完成其他所有工作,您无需替换任何内容。 - DarkLeafyGreen
显示剩余7条评论

0
我认为你需要扩展MyLocationOverlay类,并重写onDraw方法,将位置更改为你想要的位置,然后调用superclass.onDraw方法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接