2011-02-09 79 views
1

嘿,我在運行我的程序時嘗試查找用戶的經度和位置時遇到了問題,併爲模擬位置設置了地理修補程序的telnet命令。在仿真器運行時,我僅爲仿真器設置了模擬座標,使其無響應,並使我的程序在檢測輸入座標時失敗。Android模擬器,查找模擬用戶位置座標。遇到問題

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.location.LocationProvider; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.widget.Toast; 

public class LBSact extends Activity{ 
    /** Called when the activity is first created. */ 
    public double longitude; 
    public double latitude; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     longitude = 0; 
     latitude = 0; 
     //Creating the listener and manager 
     LocationManager LManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

     LocationListener createListen = new LocationListener(){ 
      @Override 
      public void onLocationChanged(Location location) { 
       //longitude = location.getLongitude(); 
       //latitude = location.getLatitude(); 
       String printout1 = "My current location is: " + "Latitude = " + location.getLatitude() + 
       "Longitude = " + location.getLongitude(); 
        Toast.makeText(getApplicationContext(),printout1,Toast.LENGTH_LONG).show(); 

      } 
      @Override 
      public void onProviderDisabled(String provider) { 
       Toast.makeText(getApplicationContext(), "GPS disabled", Toast.LENGTH_LONG).show(); 

      } 
      @Override 
      public void onProviderEnabled(String provider) { 
       Toast.makeText(getApplicationContext(), "GPS enabled", Toast.LENGTH_LONG).show(); 

      } 
      @Override 
      public void onStatusChanged(String provider, int status, 
        Bundle extras) { 
      } 
     }; 

     //Cant use Network with emulator, can only use GPS mock locations 
     //Toast.makeText(getApplicationContext(), "Does this work?", Toast.LENGTH_LONG).show(); 
     LManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, createListen); 
    } 

} 

回答

1

首先。我假設您已閱讀以下內容: http://developer.android.com/guide/topics/location/obtaining-user-location.html

由於您提到了注入模擬位置數據。還有一些其他的東西可以嘗試,例如你可以使用KML文件來注入GPS位置: http://developer.android.com/guide/developing/tools/ddms.html#emulator-control

但是你應該做的主要事情是創建日誌消息,以便更準確地檢測你的位置程序失敗。下面是關於如何設置logcat的文章: http://www.droidnova.com/debugging-in-android-using-eclipse,541.html

我建議以此爲發送文本到屏幕上可能並不總是作爲您的應用程序可能會崩潰它得到這些調用之前。

另外,你有沒有嘗試通過Eclipse調試你的應用程序?它會在崩潰中斷,並給你應用程序失敗的位置。

我會更新這個答案,因爲您在這個問題上提供了更多細節,因爲它很難看到沒有堆棧跟蹤或日誌跟蹤發生了什麼。

+0

當我設置座標,例如-122.084095經度37.422006緯度,我再次運行該程序只體驗模擬器沒有響應和程序無法加載。根據Logcat的說法,「調試人員爲了釋放殭屍而自殺!」我不知道如何解決。 – TLM 2011-02-09 23:22:48

0

幾件事情:

  1. 什麼是你的AVD API級別?像這樣的崩潰是2.3中的一個已知問題。

  2. 你是如何設置模擬座標的?通過Eclipse中的仿真器控件,或通過Telnet?

  3. 讓我們來看看清單中的權限。一些權限是必需經過本地化清單標記正常工作:

    使用許可權的android:NAME =「android.permission.INTERNET對」 使用許可權的android:NAME =「android.permission.ACCESS_COARSE_LOCATION」 使用說明 - 許可安卓:名稱= 「android.permission.ACCESS_FINE_LOCATION」 使用許可權的android:NAME = 「android.permission.ACCESS_LOCATION_EXTRA_COMMANDS」 使用許可權的android:NAME = 「android.permission.ACCESS_MOCK_LOCATION」

這裏的我如何請求更新用戶位置並將POI設置爲覆蓋項目:

package com.snackrocket.location; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

import android.graphics.drawable.Drawable; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 

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.OverlayItem; 
import com.snackrocket.R; 

public class SrPlacesActivity extends MapActivity implements LocationListener { 
    private final static ArrayList<HashMap<String, Object>> POIS = new ArrayList<HashMap<String, Object>>(); 

    static { 
     HashMap<String, Object> row = new HashMap<String, Object>(); 
     row.put("Name", "Sushi Tei"); 
     row.put("Description", "Sushi and Much More"); 
     row.put("Longitude", (101.6769441 * 1E6)); 
     row.put("Latitude", (3.1184736 * 1E6)); 
     POIS.add(row); 
     row = new HashMap<String, Object>(); 
     row.put("Name", "Devi's Corner"); 
     row.put("Description", "Quality Mamak"); 
     row.put("Longitude", (101.6716426 * 1E6)); 
     row.put("Latitude", (3.1313672 * 1E6)); 
     POIS.add(row); 
     row = new HashMap<String, Object>(); 
     row.put("Name", "KFC"); 
     row.put("Description", "The Colonel's Secret Recipe"); 
     row.put("Longitude", (101.650006 * 1E6)); 
     row.put("Latitude", (3.117497 * 1E6)); 
     POIS.add(row); 
     row = new HashMap<String, Object>(); 
     row.put("Name", "McDonalds"); 
     row.put("Description", "I'm lovin it"); 
     row.put("Longitude", (101.6723386 * 1E6)); 
     row.put("Latitude", (3.1328851 * 1E6)); 
     POIS.add(row); 
     row = new HashMap<String, Object>(); 
     row.put("Name", "DHaven"); 
     row.put("Description", "Thai Food and Chicha"); 
     row.put("Longitude", (101.6710465 * 1E6)); 
     row.put("Latitude", (3.1315682 * 1E6)); 
     POIS.add(row); 
    } 

    private MapView myMap; 
    private MyLocationOverlay myLocOverlay; 
    private GeoPoint homebase = new GeoPoint((int) (3.1 * 1E6), 
      (int) (101.7 * 1E6)); 
    private int lat; 
    private int lng; 
    private GeoPoint myPoint = homebase;; 
    private Geocoder geocoder; 
    private MapController mapController; 
    private LocationManager locationManager; 
    private TextView locationText; 

    private void initMap() { 
     myMap = (MapView) findViewById(R.id.mapview); 

    } 

    private void initMyLocation() { 
     myLocOverlay = new MyLocationOverlay(this, myMap); 
     myLocOverlay.enableMyLocation(); 
     myMap.getOverlays().add(myLocOverlay); 

    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setContentView(R.layout.mapview); 

     // Get location manager 
     locationManager = (LocationManager) this 
       .getSystemService(LOCATION_SERVICE); 

     // Create geocoder 
     geocoder = new Geocoder(this); 

     GeoPoint userPosition = new GeoPoint((int) (lng * 1E6), (int) (lat * 1E6)); 

     // Prevents NullPointerException 
     if (userPosition != null) { 
      userPosition = myPoint; 
     } 

     // Inits 
     initMap(); 
     initMyLocation(); 

     // Zoom 
     int zoomLevel = 14; 
     myMap.setBuiltInZoomControls(true); 
     MapController mapController = myMap.getController(); 
     mapController.setCenter(myPoint); 
     mapController.setZoom(zoomLevel); 

     // Set custom overlay template 
     List<Overlay> mapOverlays = myMap.getOverlays(); 

     // Create my location overlay 
     myLocOverlay = new MyLocationOverlay(this, myMap); 
     myLocOverlay.enableMyLocation(); 

     // Enable compass 
     myLocOverlay.enableCompass(); 

     // Add my location overlay 
     mapOverlays.add(myLocOverlay); 

     // Cycle through POIs and do some magic 
     GeoPoint point; 
     OverlayItem overlayItem; 
     Drawable drawable = getResources().getDrawable(R.drawable.iconr); 
     for (HashMap<String, Object> poi : POIS) { 
      // Create POI overlay 
      SrItemizedOverlay itemizedOverlay = new SrItemizedOverlay(drawable, this); 
      point = new GeoPoint(((Double) poi.get("Latitude")).intValue(), ((Double) poi.get("Longitude")).intValue()); 
      overlayItem = new OverlayItem(point, (String) poi.get("Name"), (String) poi.get("Description")); 
      itemizedOverlay.addOverlay(overlayItem); 

      // Add POI overlay 
      mapOverlays.add(itemizedOverlay); 
     } 
    } 

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

    @Override 
    protected void onResume() { 
     super.onResume(); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
       1000, 10, this); 

    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     locationManager.removeUpdates(this); 

    } 

    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // 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 onLocationChanged(Location location) { 
     location = locationManager 
       .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     lat = (int) (location.getLatitude()); 
     lng = (int) (location.getLongitude()); 

    } 
} 

這是我的逐項疊加邏輯:

package com.snackrocket.location; 

import java.util.ArrayList; 

import android.app.AlertDialog; 
import android.content.Context; 
import android.graphics.drawable.Drawable; 

import com.google.android.maps.ItemizedOverlay; 
import com.google.android.maps.OverlayItem; 

public class SrItemizedOverlay extends ItemizedOverlay<OverlayItem> { 
    Context mContext; 

    // Creates ArrayList for ItemizedOverlay 
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); 


    public SrItemizedOverlay(Drawable defaultMarker) { 
     super(boundCenterBottom(defaultMarker)); 
    } 

    public SrItemizedOverlay(Drawable defaultMarker, Context context) { 
     super(boundCenterBottom(defaultMarker)); 
     mContext = context; 
    } 

    // Adds OverlayItems to the ArrayList 
    public void addOverlay(OverlayItem overlay) { 
     mOverlays.add(overlay); 
     populate(); 
    } 

    // Returns OverlayItem specified by integer 
    @Override 
    protected OverlayItem createItem(int i) { 
     return mOverlays.get(i); 
    } 

    // Returns current number of OverlayItems in the ArrayList 
    @Override 
    public int size() { 
     return mOverlays.size(); 
    } 

    @Override 
    protected boolean onTap(int index) { 
     OverlayItem item = mOverlays.get(index); 
     AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); 
     dialog.setTitle(item.getTitle()); 
     dialog.setMessage(item.getSnippet()); 
     dialog.show(); 
     return true; 
    } 
} 

希望幫助!