2012-02-07 12 views
0

私は緯度と経度の位置を表示するアプリケーションを試しています。私はlatを与えようとしました。 & lon。ジオフィックスとエミュレータコントロールを使用して値を取得していますが、出力が得られません。誰か助けてくれますか?エミュレータで現在の固定位置を取得する方法は?

は、ここで私が得るコードと出力です:

public class LocationTest extends Activity 
{ 
    private LocationManager mgr; 
    private TextView output; 
    private String best; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mgr = (LocationManager) getSystemService(LOCATION_SERVICE); 
     output = (TextView) findViewById(R.id.output); 
     StringBuilder sb = new StringBuilder("Enabled Provider:"); 

     Criteria criteria = new Criteria(); 
     String provider = mgr.getBestProvider(criteria, true); 

     mgr.requestLocationUpdates(provider, 1000, 0, 
     new LocationListener() 
     { 
      public void onLocationChanged(Location location) {} 
      public void onProviderDisabled(String provider) {} 
      public void onProviderEnabled(String provider) {} 
      public void onStatusChanged(String provider, int status, Bundle extras) {} 
     }); 
     sb.append("\n").append(provider).append(":"); 
     Location location = mgr.getLastKnownLocation(provider); 
     if (location != null) 
     { 
      double lat = location.getLatitude(); 
      double lng = location.getLongitude(); 
      sb.append(lat).append(",").append(lng); 
     } 
     else 
     { 
      sb.append("No Location"); 
     } 
     output.setText(sb); 
    } 
} 

出力:

イネーブルプロバイダ: GPS:あなたのLocationListenerpublic void onLocationChanged(Location location) {}にコードを配置する必要がありません場所

答えて

0

。これは、場所の変更が通知される場所です。 (最も可能性が高いエミュレータ上ヌルになります)

getLastKnownLocation()あなたの現在の位置を与えるものではありませんが、最後の既知の場所

Android SDKのDDMSを使用してGPS位置を通知します。これはonLocationChanged()となります。クラスの新しい場所を保持する方法が必要です(たとえば、クラスオブジェクトをActivityに追加するなど)。

+0

で私に受信トレイが必要な場合、私はCじゃない、2または3の画像のressourcesが必要な場合があります私の場所をぶら下げている。私は、私が養っている価値を得る必要があります。次に、onLocationChanged(Location location)にコードを入れる必要はありますか?それは正しく正しく動作するはずですか? – Nikunj

+0

問題ではありません。最初のロケーションイベントは、* onLocationChanged()*に送信されます。私が言ったように、onLocationChanged()は有効なLocationオブジェクトを提供していますが、getLastKnownLocationはnullでもかまいません。 – m0skit0

+0

あなたは私がonLocationChanged()にコードを入れようとしましたが、それでもアプリケーションは座標を表示していないと言いました。 – Nikunj

0

あなたのエミュレータは模擬場所を受け取るように設定しましたか? ない場合は、それを行うと、それは私のコードで、ここで

を動作するかどうかを確認するためにそれをthrought telnetのモックの場所を送信し、それが正常に(長いもの)に動作します:あなたがOnLocationでは変更された部分に必要なもの;-)

package tfe.rma.ciss.be; 

import java.io.BufferedReader; 
import java.util.List; 


import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.content.ServiceConnection; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Point; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.location.LocationProvider; 
import android.os.Bundle; 
import android.os.IBinder; 

import android.util.Log; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 
import com.google.android.maps.MyLocationOverlay; 
import com.google.android.maps.Overlay; 
import com.google.android.maps.MapView.LayoutParams; 

public class TheMap extends MapActivity implements LocationListener 
{ // public ServiceMenace serviceMenace; 
    public MyInterface monInterface ; 
// public MonService monservice; 
    static final String tag = "Main"; // for Log 
    private MapView mapView = null; 
    private MapController mapController = null; 
    TextView txtInfo; 
    LocationManager lm; 
    StringBuilder sb; 
    int noOfFixes = 0; 
    double latitude=53, longitude= 4.16; 
    GeoPoint p,P2, p3; 
    double lat=53, latTest, longTest ; 
    double lng=4, A=2 ; 
    int clientID= 2; 
    BufferedReader bufferedReader= null; 
    public String update="", Data=""; 
    Intent myIntent; 
    private EditText goLat; 
    private EditText goLon; 

    private MyLocationOverlay me=null; 
    double i= 3; 
    Point screenP3ts = new Point(); 

    String _lat = "" , _long=""; 

    /*public void onReceive(Context contex,Intent intent){ 

     Data = intent.getStringExtra("datas"); 

    }*/ 

     @Override 
     protected boolean isLocationDisplayed() { 
     return false; 
     } 

    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.blue);    
      canvas.drawBitmap(bmp, screenPts.x-15, screenPts.y-30, null); 



//   Point screenP2ts = new Point(); 
//   mapView.getProjection().toPixels(P2, screenP2ts); 
//    
//   //---add the marker--- 
//   Bitmap bmp2 = BitmapFactory.decodeResource(
//    getResources(), R.drawable.node_safe);    
//   canvas.drawBitmap(bmp2, screenP2ts.x-15, screenP2ts.y-30, null); 
//    
//    




      //---add the marker--- 
      Bitmap bmp3 = BitmapFactory.decodeResource(
       getResources(), R.drawable.maps_position_marker);    
      canvas.drawBitmap(bmp3, screenP3ts.x-15, screenP3ts.y-30, null); 


      return true; 
      } 
     public boolean onCreateOptionsMenu(Menu menu) { 
      MenuInflater inflater = getMenuInflater(); 
      inflater.inflate(R.menu.menu, menu); 
      return true; 
     } 

     @Override 
     public boolean onTouchEvent(MotionEvent event, MapView mapView) 
     { 
      //---when user lifts his finger--- 
      if (event.getAction() == 1) {     
       GeoPoint p = mapView.getProjection().fromPixels(
        (int) event.getX(), 
        (int) event.getY()); 
        Toast.makeText(getBaseContext(), 
         p.getLatitudeE6()/1E6 + "," + 
         p.getLongitudeE6() /1E6 , 
         Toast.LENGTH_SHORT).show(); 
      }        
      return false; 
     }   

    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.icon:  
      myIntent = new Intent (this, VisClass.class); 
      startActivity(myIntent);     
           break; 
      case R.id.text:  
      myIntent = new Intent (this, InfraredClass.class); 
      startActivity(myIntent); 
           break; 
      case R.id.icontext: 
      myIntent = new Intent (this, RadarClass.class); 
      startActivity(myIntent); 
           break; 
      case R.id.menu: 
       myIntent = new Intent (this, menu.class); 
       startActivity(myIntent); 
           break; 
     } 
     return true; 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.menu, menu); 
     return true; 
    } 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.map);   
     final MapView mapView = (MapView) findViewById(R.id.mapView); 

     mapView.setBuiltInZoomControls(true); 
//  
     lm = (LocationManager) getSystemService(LOCATION_SERVICE); 

     LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom); 
     View zoomView=mapView.getZoomControls(); 
     zoomLayout.addView(zoomView, 
       new LinearLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT)); 
      mapView.displayZoomControls(true); 
      /* mapView.setSatellite(true); 
      mapView.setStreetView(true); 
      mapView.setTraffic(true);*/ 
      mapView.setBuiltInZoomControls(true); 
      mapController=mapView.getController(); 
      String coordinates[] = {"53", "4.6"};//schuman/ 



      //---Add a location marker--- 
      MapOverlay mapOverlay = new MapOverlay(); 
      List<Overlay> listOfOverlays = mapView.getOverlays(); 
      listOfOverlays.clear(); 
      listOfOverlays.add(mapOverlay); 



      mapView.invalidate(); 


      p = new GeoPoint(
        (int) (lat * 1E6), 
        (int) (lng * 1E6)); 
      mapController.animateTo(p); 
      mapController.setCenter(p); 

      ((Button)findViewById(R.id.goMap)).setOnClickListener(
         new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         // On récupère notre EditText 
         EditText UserName = ((EditText)findViewById(R.id.getLon)); 
         EditText Password = ((EditText)findViewById(R.id.getLat)); 

         // On garde la chaîne de caractères 
          _lat = UserName.getText().toString(); 
          _long = Password.getText().toString(); 

         latTest = Double.parseDouble(_lat)* 1E6; 
         longTest = Double.parseDouble(_long)* 1E6; 

         p3 = new GeoPoint(
            (int) (latTest), 
            (int) (longTest)); 

         mapView.getProjection().toPixels(p3, screenP3ts); 
          mapController.animateTo(p3); 
          mapController.setCenter(p3); 

         Toast.makeText(TheMap.this, "lat=" + latTest + " et " + "long= " + longTest, Toast.LENGTH_SHORT).show(); 


         } 
         }); 




         ((Button)findViewById(R.id.sat)).setOnClickListener(
           new OnClickListener() { 
          @Override 
          public void onClick(View v) { 
           mapView.setSatellite(true); 


           } 
           }); 

         ((Button)findViewById(R.id.normal)).setOnClickListener(
           new OnClickListener() { 
          @Override 
          public void onClick(View v) { 
           mapView.setSatellite(false); 
           mapView.setStreetView(false); 
           mapView.setTraffic(false); 


           } 
           }); 


         ((Button)findViewById(R.id.traffic)).setOnClickListener(
           new OnClickListener() { 
          @Override 
          public void onClick(View v) { 
           mapView.setTraffic(true); 


           } 
           }); 

         ((Button)findViewById(R.id.street)).setOnClickListener(
           new OnClickListener() { 
          @Override 
          public void onClick(View v) { 
           mapView.setStreetView(true); 


           } 
           }); 




    } 



    @Override 
    protected void onResume() { 
     /* 
     * onResume is is always called after onStart, even if the app hasn't been 
     * paused 
     * 
     * add location listener and request updates every 1000ms or 10m 
     */ 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this); 
     super.onResume(); 
    } 

    @Override 
    protected void onPause() { 
     /* GPS, as it turns out, consumes battery very much */ 
     lm.removeUpdates(this); 
     super.onResume(); 
    } 


    @Override 
    public void onLocationChanged(Location location) { 
     Log.v(tag, "Location Changed"); 

     lat= (int) (location.getLatitude()); 
     lng= (int) (location.getLongitude()); 

     sb = new StringBuilder(512); 

     noOfFixes++; 

     p = new GeoPoint(
       (int) (location.getLatitude()*1E6), 
       (int) (location.getLongitude()*1E6)); 
     mapController.animateTo(p); 
     mapController.setCenter(p); 

     Toast.makeText(this, "Lat= " + lat + " Long= " + lng, Toast.LENGTH_SHORT).show(); 




     } 








    @Override 
    public void onProviderEnabled(String provider) { 
     Log.v(tag, "Enabled"); 
     Toast.makeText(this, "GPS Enabled", Toast.LENGTH_SHORT).show(); 

    } 



    @Override 
    public void onProviderDisabled(String provider) { 
     /* this is called if/when the GPS is disabled in settings */ 
     Log.v(tag, "Disabled"); 

     /* bring up the GPS settings */ 
     Intent intent = new Intent(
       android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
     startActivity(intent); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     /* This is called when the GPS status alters */ 
     switch (status) { 
     case LocationProvider.OUT_OF_SERVICE: 
      Log.v(tag, "Status Changed: Out of Service"); 
      Toast.makeText(this, "GPS prvider: Out of Service", 
        Toast.LENGTH_SHORT).show(); 

      break; 

     case LocationProvider.TEMPORARILY_UNAVAILABLE: 
      Log.v(tag, "Status Changed: Temporarily Unavailable"); 
      Toast.makeText(this, "GPS provider: Temporarily Unavailable", 
        Toast.LENGTH_SHORT).show(); 

      break; 

     case LocationProvider.AVAILABLE: 
      Log.v(tag, "Status Changed: Available"); 
      Toast.makeText(this, "GPS Provider: Available", 
        Toast.LENGTH_SHORT).show(); 
      break; 
     } 
    } 


    @Override 
    protected void onStop() { 
     /* may as well just finish since saving the state is not important for this toy app */ 
     finish(); 
     super.onStop(); 
    } 

    ServiceConnection updateConnection = new ServiceConnection() { 
      public void onServiceConnected1(ComponentName className, IBinder service) { 
        // This is called when the connection with the service has been 
        // established, giving us the service object we can use to 
        // interact with the service. 
//    ServiceMenace ServiceMenace = ((ServiceMenace.UpdateBinder)service).getService(); 
       monInterface = (MyInterface) service ; 
       Log.v("ViewMap.ServiceConnection.onServiceConnected", "connected to the update service"); 
       } 

       public void onServiceDisconnected(ComponentName className) { 
        // This is called when the connection with the service has been 
        // unexpectedly disconnected -- that is, its process crashed. 
        // Because it is running in our same process, we should never 
        // see this happen. 
       Object ServiceMenace = null; 

       Log.v("ViewMap.ServiceConnection.onServiceDisconnected", "disconnected from the update service"); 
       } 

      @Override 
      public void onServiceConnected(ComponentName arg0, IBinder arg1) { 
       // TODO Auto-generated method stub 

      }  
      }; 



    @Override 
    protected boolean isRouteDisplayed() { 
     return false; 
    } 
} 
をあなたがここに私のレイアウト

が必要な場合

知らんが、私のレイアウトである:(あなたはまだあなたがそれらを[email protected]

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 

<LinearLayout android:id="@+id/zoom" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     /> 

    <com.google.android.maps.MapView 
     android:id="@+id/mapView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:enabled="true" 
     android:clickable="true" 
     android:apiKey="0lcrLCvFHx-8bK4lgE307_CyHXxgbjefMiI3w9w" 
     /> 
     <LinearLayout android:id="@+id/zoom" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     /> 

<SlidingDrawer 
     android:id="@+id/drawer" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:handle="@+id/handle" 
     android:content="@+id/content" 
     > 
     <ImageView 
      android:id="@id/handle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/tray_handle_normal" 
     /> 

     <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@id/content" 
android:background="@layout/background" 
> 

    <TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:stretchColumns="1,2" 
    > 
    <TableRow> 
     <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="2dip" 
     android:paddingRight="4dip" 
     android:text="Emplacement : " 
     /> 
     <EditText android:id="@+id/getLat" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:cursorVisible="true" 
     android:editable="true" 
     android:singleLine="true" 
     android:layout_weight="1" 
     android:inputType="numberDecimal" 
     /> 
     <EditText android:id="@+id/getLon" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:cursorVisible="true" 
     android:editable="true" 
     android:singleLine="true" 
     android:layout_weight="1" 
     android:inputType="numberDecimal" 
     /> 
    </TableRow> 
    </TableLayout> 
    <Button android:id="@+id/goMap" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Afficher !" 
    /> 
    <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@id/content" 
> 
<Button 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/sat" 
android:text="Satellite"> 
</Button> 

<Button 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/normal" 
android:text="normal"> 
</Button> 

<Button 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/traffic" 
android:text="traffic"> 
</Button> 
<Button 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/street" 
android:text="street"> 
</Button> 
</LinearLayout> 

</LinearLayout> 

    </SlidingDrawer> 

</RelativeLayout> 
+0

私は既にモックの場所を受け取るようにエミュレータを設定しましたが、それ以降も出力が得られません。あなたのコードは良いですが、それは長いものです。私のコードでprobを修正できますか? – Nikunj

+0

@ m0skit0として、問題は、最新の既知の場所を取得することにあります...あなたが提供する場合は、locationChangedメソッドでは、それはあなたの給餌場所のようなものです。あなたが本当のデバイスを持っていなければ、あなたの最後の知っている場所はおそらくnullになるということです。あなたはそれを持っているために実際のGPS信号で外部の活動をlaucnhする必要があります。 – youssoua

+0

あなたのレイアウトにも。あなたのコードを試して、私は出力を得るかどうかを確認します。 – Nikunj

関連する問題