0

// here is my code of MapActivity class

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) {

        }
    }
}
Lucifer
  • 29,392
  • 25
  • 90
  • 143
Dext
  • 1
  • 2

1 Answers1

1

Not Executing - What should we consider this as a Compile Time or a Runtime Error. If yes paste the error logs. Also

  1. Are You Trying this on Real Device ? If not do so

  2. Is GPS of your device is ON ? if not do so

  3. Try using 0 parameter instead of 200. See its results.

  4. Also try LocationManager.NETWORK_PROVIDER

  5. Make Sure You have sufficient permission defined in Manifest File.

Rohit Sharma
  • 13,787
  • 8
  • 57
  • 72
  • @javanator... can u please explain LocationManager.NETWORK_PROVIDER? – Dext May 30 '12 at 05:41
  • Provider describe a entity that will provide you device location on request. For One Device There can be and usually there are more than one provider of location. You are asking from GPS only. try asking them from both. At a time your mobile device is connected to various cell towers across your location. network provider using those cell tower id provides a location. It is less accurate but fits very well for so many requirements – Rohit Sharma May 30 '12 at 05:52
  • i tried all..but the code from line.."private class MyLocationListener implements LocationListener {" to end is not executing – Dext May 30 '12 at 07:10
  • instead of trying your code . comment it out whole. try simple log line or toast in the expected method. – Rohit Sharma May 30 '12 at 07:25