0

我正在尝试在地图视图中添加位置的地理点。我想移动地图视图,直到我想要并将中心作为位置(经度和纬度),并使用这个位置和我当前的位置,知道两个地理点之间的距离。

我想要类似于 FakeGPS 应用程序的东西,

http://www.google.es/imgres?q=FAKEGPS&um=1&hl=es&sa=N&tbo=d&biw=1247&bih=548&tbm=isch&tbnid=MIj6qLscPq5OkM:&imgrefurl=http://www.androidpit.es/es/android/market/ aplicaciones/aplicacion/com.lexa.fakegps/Fake-GPS-location&docid=vtGY2p008QVLOM&imgurl=http://fs01.androidpit.info/ass/x93/1612093-1328451448448.jpg&w=320&h=480&ei=RQDzUP_8C8KXoom817&&14HQDQ=hQDQ=zoom87&&Vpy=hQDQ&vpx=i 81&dur=27&hovh=275&hovw=183&tx=84&ty=134&sig=110346020377905343716&page=1&tbnh=129&tbnw=86&start=0&ndsp=27&ved=1t:429,r:1,s:0,i:85

我试过这个:

case R.id.button1:

            GeoPoint geoPoint=mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY());
            int latitude = geoPoint.getLatitudeE6();
            int longitude = geoPoint.getLongitudeE6();
             targetLocation = new Location("Target Position");
             targetLocation.setLatitude(latitude); //punto pre-definido,
            targetLocation.setLongitude(longitude); //no cambiar el valor
             targetLocation.setAltitude(0);

因为我在其他帖子里看到了。但我不知道如何使用事件。因为我没有声明

这是我的所有代码:

公共类 GpsActivity 扩展 MapActivity 实现 OnClickListener{

private LocationManager locationManager;
private Location targetLocation;
double longitude, latitude;
private float distancia;
ProgressDialog pd;
LocationListener listener;
Ringtone ringtone;
boolean alarmaActivada;
Button aumentar, reducir;
Context context;
MapView mapView;


public class MyLocationListener implements LocationListener {

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

    }

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

    }

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

    }

    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        float dist=0;
        float error=0;

        //Escribir posición actual en m1TextMessage
        latitude =location.getLatitude();
        longitude = location.getLongitude();

        //calcular distancia al destino y error estimado
        //Escribir distancia al destino y error en m3TextMessage
        GeoPoint geoPoint;
        List<Overlay> mapOverlays = mapView.getOverlays();
         geoPoint = new GeoPoint((int)(longitude*1E6),(int)
                    (latitude*1E6));
        MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay
                    (context,context.getResources().getDrawable(R.drawable.ic_launcher));
        OverlayItem overlayitem = new OverlayItem(geoPoint,"Error","Distancia");
        itemizedOverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedOverlay);


        Location loc2= new Location(LocationManager.GPS_PROVIDER);

         if (loc2 != null)  
            {
               dist=location.distanceTo(targetLocation);
               error = location.getAccuracy();
               if(dist < distancia){
                   Toast.makeText(getApplicationContext(), "Distancia menor a: "+dist+" metros.",Toast.LENGTH_SHORT).show(); 
                   if(!alarmaActivada)
                   startAlarm();
              }// else 
                //   stopAlarm();
            }else
                 Toast.makeText(getApplicationContext(), "location actual es null!!",Toast.LENGTH_SHORT).show();

        //Lanzar un Toast si la distancia al destino es inferior a 10 metros

    }

};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.gps);
    Button localiza, enviar;

    context = this;

    localiza = (Button)findViewById(R.id.button1);
    enviar = (Button)findViewById(R.id.sendDistancia);
    aumentar =(Button)findViewById(R.id.aumentar);
    reducir = (Button)findViewById(R.id.reducir);

    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapView.postInvalidate();
    MapController mapCont = mapView.getController();
    mapCont.setZoom(12);
    MyItemizedOverlay overlayItem = new MyItemizedOverlay(this.getResources().getDrawable(R.drawable.ic_launcher));


    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    localiza.setOnClickListener(this);
    enviar.setOnClickListener(this);
    reducir.setOnClickListener(this);
    aumentar.setOnClickListener(this);


}private void startAlarm() {  
    Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);  
    if(alert == null){  
        // alert is null, using backup  
        alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);  
        if(alert == null){  // I can't see this ever being null (as always have a default notification) but just incase  
            // alert backup is null, using 2nd backup  
            alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);                 
        }  
    }  
   ringtone = RingtoneManager.getRingtone(getApplicationContext(), alert);  
   if (ringtone != null) {  
       ringtone.play();  
       alarmaActivada = true;
   }  

}

public class GPSLocation extends AsyncTask<Void, Void, Void>
{  
    boolean running =true;
            @Override
            protected void onPreExecute()
            {  
                super.onPreExecute(); 
                pd = new ProgressDialog(GpsActivity.this);
                pd.setOnCancelListener(new DialogInterface.OnCancelListener(){
                      public void onCancel(DialogInterface dialog) {
                          pd.cancel();  
                      }
                });
                longitude=0;
                latitude =0;
                getLonLat();
                pd.setCancelable(true);
                pd.setMessage("Getting GPS Location...");
                pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);

                pd.show();

            } 

            @Override 
            protected void onProgressUpdate(Void... values) {
                super.onProgressUpdate(values);


                // Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
             }

            @Override 
            protected void onPostExecute(Void result)
            {  


                    pd.dismiss(); 
            }

            @Override
            protected Void doInBackground(Void... params) {  
                boolean isDataSubmitted = false;

                while(!isDataSubmitted)
                {  
                    if(longitude !=0 && latitude!=0)
                    { 

                        isDataSubmitted = true;
                        Log.d("LONGITUD", ""+longitude);
                        Log.d("LATITUDE", ""+latitude);

                    }  
                } 

                return null;    
            } 
 } 


public void getLonLat(){
    listener = new MyLocationListener();

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        (locationManager).requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,listener);

    }else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
        Log.d("Long", ""+longitude);
    } else 
        enableLocationSettings();

}

@Override
public void onStart(){

    //Crear servicio de localización LocationManager       
    //Determinar si el GPS está encendido
    //Caso esté apagado, permitir al usuario activarlo mediante método enableLocationSettings()

    //Solicitar actualizaciones de posición al proveedor de GPS (opcionalmente también al proveedor de red)
    //Nota: el LocationListener ya ha sido creado: listener


    super.onStart();
}


private void enableLocationSettings() { //enable Location services is necessary
    Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(settingsIntent);
}


@Override
public void onStop(){
     locationManager.removeUpdates(listener);
     super.onStop();
}

public void onClick(View v) {
        EditText distan;
        switch (v.getId()) {
        case R.id.sendDistancia:
            distan = (EditText)findViewById(R.id.distancia);
            distancia = Float.valueOf(distan.getText().toString());
            new GPSLocation().execute();
            break;

        case R.id.button1:
            GeoPoint geoPoint=mapView.getProjection().fromPixels((int).getX(),(int)event.getY());
            int latitude = geoPoint.getLatitudeE6();
            int longitude = geoPoint.getLongitudeE6();
             targetLocation = new Location("Target Position");
             targetLocation.setLatitude(latitude); //punto pre-definido,
            targetLocation.setLongitude(longitude); //no cambiar el valor
             targetLocation.setAltitude(0);

            break;
        case R.id.aumentar:
            break;
        case R.id.reducir:
            break;
        }



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

}

谢谢,也很抱歉。

4

1 回答 1

0

最后我用这个做到了。

       GeoPoint geoPoint=mapView.getMapCenter();
            double division = 1000000.0;
            double latitude = (geoPoint.getLatitudeE6()/division);
            double longitude = (geoPoint.getLongitudeE6()/division);

             Drawable drawable = context.getResources().getDrawable(R.drawable.ic_launcher);
            itemizedoverlay = new MyItemizedOverlay(context, drawable);


                OverlayItem overlayitem = new OverlayItem(geoPoint, "Objetivo", "");
                itemizedoverlay.addOverlay(overlayitem);
                if (itemizedoverlay.size() > 0) {
                    mapView.getOverlays().add(itemizedoverlay);
                }
于 2013-01-13T20:16:14.087 回答