2012-05-30 90 views
0

//這裏是我MapActivity類的代碼爲什麼位置監聽器函數沒有執行?

public class MapActivity extends com.google.android.maps.MapActivity implements LocationListener 
{ 

    MapView mapView; 
    MapController mc; 
    GeoPoint p; 
    MyLocation myLocation = new MyLocation(); 
    LocationManager mlocManager; 
    int distance; 
    public void onCreate(Bundle savedInstanceState) { 

     // Remove title bar 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.map); 
     ImageButton mpl = (ImageButton)findViewById(R.id.nearbybuttn); 
     mapView = (MapView) findViewById(R.id.mv); 
     mapView.setBuiltInZoomControls(true); 
     mapView.displayZoomControls(true); 
     mc = mapView.getController(); 
     mc.setZoom(8); 


LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

LocationListener mlocListener = new MyLocationListener(); 

     mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 200, 
       0, mlocListener); 

    } 

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

    private class MyLocationListener implements LocationListener { 

     @Override 
     public void onLocationChanged(Location loc) { 
      // gets current location on position change 
      loc.getLatitude(); 
      loc.getLongitude(); 

      Double slat = loc.getLatitude(); 

      Double slon = loc.getLongitude(); 
      TextView txlat = (TextView) findViewById(R.id.lat); 
      TextView txlon = (TextView) findViewById(R.id.lon); 
      txlat.setText(slat + "n"); 
      txlon.setText(slon + "n"); 

      // json retrieval 
      JSONObject j2 = JSONfunctions 
        .getJSONfromURL("/merchant/apis/?device=iphone&api=store_details"); 

      try { 
       JSONArray myID = j2.getJSONArray("stores"); 
       for (int i = 0; i < myID.length(); i++) { 
        Log.v("state", "json address being read"); 
        JSONObject j3 = myID.getJSONObject(i); 
        String name = j3.getString("Address"); 
        String id = j3.getString("StoreId"); 
        Log.v("id", id); 
        Log.v("Address", name); 
        JSONObject j4 = j3.getJSONObject("Address"); 
        Double jlat = j4.getDouble("Latitude"); 
        Double jlon = j4.getDouble("Longitude"); 
        Log.v("Latitude", jlat + "n"); 
        Log.v("Longitude", jlon + "n"); 

        // Get the distance between lat long 
        Location locationA = new Location("point A"); 

        locationA.setLatitude(slat); 
        locationA.setLongitude(slon); 

        Location locationB = new Location("point B"); 

        locationB.setLatitude(jlat); 
        locationB.setLongitude(jlon); 

        distance = (int) locationA.distanceTo(locationB); 
        String str = " (" + String.valueOf(distance) + " meters)"; 
        Log.v("Distance", str); 

        // adjust drawable params 
        Drawable marker = getResources().getDrawable(
          android.R.drawable.star_big_on); 
        Drawable user = getResources().getDrawable(
          android.R.drawable.arrow_down_float); 
        int userWidth = user.getIntrinsicWidth(); 
        int userHeight = user.getIntrinsicHeight(); 
        user.setBounds(0, userHeight, userWidth, 0); 
        int markerWidth = marker.getIntrinsicWidth(); 
        int markerHeight = marker.getIntrinsicHeight(); 
        marker.setBounds(0, markerHeight, markerWidth, 0); 

        // refernc to overlay class 
        LocationOverlay myItemizedOverlay = new LocationOverlay(
          marker, MapActivity.this); 
        LocationOverlay myItemizedOverlay1 = new LocationOverlay(
          user, MapActivity.this); 
        mapView.getOverlays().add(myItemizedOverlay); 
        mapView.getOverlays().add(myItemizedOverlay1); 

        // create geopoint for user 
        GeoPoint usr = new GeoPoint((int) (slat * 1e6), 
          (int) (slon * 1e6)); 
        // add overlay(user) to user's location 
        myItemizedOverlay1.addItem(usr, "User"); 
        mc.animateTo(usr); 

        // create geopoint for json 
        GeoPoint jgpt = new GeoPoint((int) (jlat * 1e6), 
          (int) (jlon * 1e6)); 

        // add marker on geopoints from json 
        myItemizedOverlay.addItem(jgpt, "StoreId" + id); 

        mapView.setOnTouchListener(new View.OnTouchListener() { 

         @Override 
         public boolean onTouch(View v, MotionEvent event) { 
          // TODO Auto-generated method stub 
          return false; 
         } 
        }); 

       } 
      } catch (JSONException e) { 
       Log.e("loG_tag", "Error parsing" + e.toString()); 
      } 

     } 

     private TextView findViewById(int lat) { 
      // TODO Auto-generated method stub 
      return null; 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      Toast.makeText(getApplicationContext(), "Gps Disabled", 
        Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public void onProviderEnabled(String provider) { 
      Toast.makeText(getApplicationContext(), "Gps Enabled", 
        Toast.LENGTH_SHORT).show(); 
     } 

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

     } 
    } 
} 
+0

參考[此鏈接](http://stackoverflow.com/a/7644635/779408) – breceivemail

回答

1

不予執行 - 我們應該考慮這是一個編譯或運行時錯誤。 如果是粘貼錯誤日誌。另外

  1. 你在真實設備上試用過嗎?如果不這樣做

  2. 您的設備的GPS是否打開?如果不這樣做

  3. 嘗試使用0參數而不是200.查看其結果。

  4. 也可以嘗試LocationManager.NETWORK_PROVIDER

  5. 請確保您有清單文件中定義足夠的權限。

+0

@javanator ... u能請解釋LocationManager.NETWORK_PROVIDER? – Dext

+0

Provider描述了一個實體,它將根據請求爲您提供設備位置。對於一個設備可以有並且通常有多個位置提供者。你只是從GPS問。試着從兩個方面問他們。 您的移動設備一次連接到您所在位置的各個蜂窩塔。網絡提供商使用這些手機塔ID提供了一個位置。這是不太準確,但非常適合這麼多的要求 – Javanator

+0

我試過所有..但代碼行..「私人類MyLocationListener落實LocationListener {」結束不執行 – Dext