2010-09-23 102 views
3

我正在使用以下内容来模拟我在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到位置spoofer.Actually当我们打开谷歌地图,它会显示我们目前的位置,蓝色在我们location..similarly闪烁,如果我改变了坐标即纬度和纬度它表明,我在蓝色闪烁该位置,

![替代文本] [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" 
    /> 

回答

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教程可以在这里找到,Using Google Maps in Android

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); 
     } 
}; 
+0

嗨,感谢您的回复...我不想要当前的位置..我想设置经度和纬度,以获得所需的位置...与蓝色闪烁bubble.Any帮助 – user455422 2010-09-23 07:19:41

+0

那么你根本不需要位置监听器,只是一些虚拟数据。你想在地图上设置位置吗? – 2010-09-23 07:36:52

+0

是的,我想设置地图上的位置,并在该位置蓝色闪烁应该出现...我怎么能做到这个艺术工作..?请帮助我..类似于位置欺骗.. – user455422 2010-09-23 07:51:29

0

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