//這裏是我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) {
}
}
}
參考[此鏈接](http://stackoverflow.com/a/7644635/779408) – breceivemail