0

我想从地址显示自己的位置是由用户使用 LocationOverlay 输入的,并通过一个小点或任何图标显示位置....(例如:可以通过 flag.png 显示这个位置)但现在我不知道如何在地图上显示。你能帮帮我吗!

源代码:

public class Main extends MapActivity implements LocationListener {
private static double lat;
private static double lon;
private MapController mapControl;
private MapView mapView;
LocationManager locman;
Location loc;
String provider = LocationManager.GPS_PROVIDER;
String TAG = "GPStest";
Bundle locBundle;
private int numberSats = -1;
private float satAccuracy = 2000;
private float bearing;
private double altitude;
private float speed;
private String currentProvider;
private TextView txt;
private double lat1;
private double lon1;
private Drawable marker;
private Geocoder gcoder;
private MyLocationOverlay me = null;
long GPSupdateInterval; // In milliseconds
float GPSmoveInterval; // In meters
private MyMyLocationOverlay myLocationOverlay;
private List<Overlay> mapOverlays;
public DisplayOverlay displayOverlay;
private EditText address;
Button test;
private Button btnMylocation;
private Button btnAddrress;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_long_van_map);
    gps();
    btnMylocation = (Button) findViewById(R.id.button1);
    // get my location
    btnMylocation.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            gps();
        }
    });
    gcoder = new Geocoder(this);
    me = new MyLocationOverlay(this, mapView);
    address = (EditText) findViewById(R.id.editText1);
    btnAddrress = (Button) findViewById(R.id.button2);
    //Get postion from address
    btnAddrress.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String add = address.getText().toString();
            try {

                List<Address> addresses = gcoder
                        .getFromLocationName(add, 1);
                if (addresses == null) {
                    // return point;
                }

                // just get first item of list address
                Address add2 = addresses.get(0);

                lat1 = add2.getLatitude();
                lon1 = add2.getLongitude();

            } catch (IOException e) {

            }

            GeoPoint newPoint = new GeoPoint((int) (lat1 * 1e6),
                    (int) (lon1 * 1e6));
            mapControl.animateTo(newPoint);
            marker = getResources().getDrawable(R.drawable.startpoint);

            marker.setBounds(0, 0, marker.getIntrinsicWidth(),
                    marker.getIntrinsicHeight());

            //mapView.getOverlays().add(new SitesOverlay(marker));
            mapView.getOverlays().add(me);

        }
    });

}

//Info gps of me
public void gps() {
    updateGPSprefs();
    locman = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    // Listener for GPS Status...
    final GpsStatus.Listener onGpsStatusChange = new GpsStatus.Listener() {
        public void onGpsStatusChanged(int event) {
            switch (event) {
            case GpsStatus.GPS_EVENT_STARTED:
                // Started...
                break;
            case GpsStatus.GPS_EVENT_FIRST_FIX:
                // First Fix...
                Toast.makeText(Main.this, "GPS has First fix",
                        Toast.LENGTH_LONG).show();
                break;
            case GpsStatus.GPS_EVENT_STOPPED:
                // Stopped...
                break;
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                // Satellite update
                break;
            }
            //GpsStatus status = locman.getGpsStatus(null);
            // Not presently doing anything with following status list for
            // individual satellites
            // Iterable<GpsSatellite> satlist = status.getSatellites();
        }
    };
    locman.addGpsStatusListener(onGpsStatusChange);
    locman.requestLocationUpdates(provider, GPSupdateInterval,
            GPSmoveInterval, this);
    Log.i(TAG, locman.toString());
    // Add map controller with zoom controls
    mapView = (MapView) findViewById(R.id.Mapview);
    mapView.setSatellite(false);
    mapView.setTraffic(false);
    mapView.setBuiltInZoomControls(true); // Set android:clickable=true in
                                            // main.xml
    int maxZoom = mapView.getMaxZoomLevel();
    int initZoom = (int) (0.95 * (double) maxZoom);
    mapControl = mapView.getController();
    mapControl.setZoom(initZoom);
    // Set up compass and dot for present location map overlay
    List<Overlay> overlays = mapView.getOverlays();
    myLocationOverlay = new MyMyLocationOverlay(this, mapView);
    overlays.add(myLocationOverlay);

    // Set up overlay for data display
    displayOverlay = new DisplayOverlay();
    mapOverlays = mapView.getOverlays();
    mapOverlays.add(displayOverlay);
    txt = (TextView) findViewById(R.id.textView1);
    txt.setText(LongVanMap.gettext());

}



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

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    // Called when location has changed
    centerOnLocation();

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
    locman.removeUpdates(this);

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
    locman.requestLocationUpdates(provider, GPSupdateInterval,
            GPSmoveInterval, this);
}

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

    centerOnLocation();

}

// Method to assign GPS prefs
public void updateGPSprefs() {
    int gpsPref = Integer.parseInt(Prefs
            .getGPSPref(getApplicationContext()));
    switch (gpsPref) {
    case 1:
        GPSupdateInterval = 5000; // milliseconds
        GPSmoveInterval = 1; // meters
        break;
    case 2:
        GPSupdateInterval = 10000;
        GPSmoveInterval = 100;
        break;
    case 3:
        GPSupdateInterval = 125000;
        GPSmoveInterval = 1000;
        break;
    }
}

}

4

1 回答 1

0
@Override
protected void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.maps_screen);
    initActionBar();

    String address = getIntent().getExtras().getString("address");

    String title = getIntent().getExtras().getString("title"); 

    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    Geocoder geoCoder = new Geocoder(this);
    List<Address> addresses;

    try {
        Log.i("maps", "getting geopoint for address: " + address);

        addresses = geoCoder.getFromLocationName(address, 1);
        if (addresses == null || addresses.isEmpty()) { 
            Log.i("maps", "couldn't get any matches for the following address: " + address);    
            isInvalidLocation = true;
            showInvalidAddressDialog();
            return;
        }
        Address location = addresses.get(0);

        GeoPoint point = coordinatesToGeoPoint(location.getLatitude(), location.getLongitude());

        Drawable marker = getResources().getDrawable(R.drawable.da_marker_red);
        MapOverlay adPos = new MapOverlay(marker,mapView);

        List<Overlay> overlays = mapView.getOverlays();
        OverlayItem overlayItem = new OverlayItem(point, title, address);
        adPos.addOverlay(overlayItem);

        overlays.add(adPos);    

        MapController mapcontroller = mapView.getController();
        mapcontroller.animateTo(point);
        mapcontroller.setCenter(point);
        mapcontroller.setZoom(12);

    } catch (Exception ex) {        
        Log.e("geocoder", "error using geocoder", ex);
    }

}
于 2012-11-16T08:21:58.913 回答