【问题标题】:Why is location listener function not executing?为什么位置侦听器功能不执行?
【发布时间】:2012-06-04 08:09:11
【问题描述】:

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

        }
    }
}

【问题讨论】:

标签: android google-maps locationmanager locationlistener


【解决方案1】:

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

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

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

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

  4. 也可以试试 LocationManager.NETWORK_PROVIDER

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

【讨论】:

  • @javanator...你能解释一下LocationManager.NETWORK_PROVIDER吗?
  • Provider 描述了一个实体,它将根据请求为您提供设备位置。一台设备 可以并且通常有不止一个位置提供者。您仅从 GPS 询问。尝试从两者中询问他们。一次,您的移动设备连接到您所在位置的各种手机信号塔。使用这些手机信号塔 ID 的网络提供商提供了一个位置。它不太准确,但非常适合这么多要求
  • 我尝试了所有..但是行中的代码.."private class MyLocationListener implements LocationListener {" to end 没有执行
  • 而不是尝试您的代码。把它全部注释掉。在预期的方法中尝试简单的日志行或 toast。
猜你喜欢
  • 2016-10-12
  • 1970-01-01
  • 2023-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-07
  • 2013-05-02
  • 1970-01-01
相关资源
最近更新 更多