0
지도보기에서 위치 지오 포인트를 추가하려고 삼중했습니다. 나는 원하는 위치와 위도 (위도와 위도)로이 위치와 현재 위치에서 중심점을 얻을 때까지지도 뷰를 이동하고 싶습니다. 두 geopoint 사이의 거리를 알아야합니다. 나는이 시도했습니다지도에서 지오 포인트를 수동으로 추가하고 위도와 경도를 얻습니다.
나는 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;
}
}
가 감사를 구현 확장 : 나는 해달라고 때문에이 내 모든 코드
선언 , 그리고 미안.