为什么位置监听器功能不执行?

时间:2012-05-30 05:20:41

标签: android google-maps locationmanager locationlistener

//这是我的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) {

        }
    }
}

1 个答案:

答案 0 :(得分:1)

不执行 - 我们应该将其视为编译时或运行时错误。 如果是,请粘贴错误日志。还

  1. 您是否在真实设备上尝试此操作?如果不这样做

  2. 您的设备的GPS是否已打开?如果不这样做

  3. 尝试使用0参数而不是200.查看其结果。

  4. 还可以尝试使用LocationManager.NETWORK_PROVIDER

  5. 确保您在清单文件中定义了足够的权限。