我正在尝试在地图视图中添加位置的地理点。我想移动地图视图,直到我想要并将中心作为位置(经度和纬度),并使用这个位置和我当前的位置,知道两个地理点之间的距离。
我想要类似于 FakeGPS 应用程序的东西,
我试过这个:
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;
}
}
谢谢,也很抱歉。