我在android中做地图应用程序。为此,我需要用一个标记图像显示当前位置。当我在顶部editText框中键入任何地址时,我需要显示具有不同标记图像的位置+我可以将第二个标记更改为tapped位置。对我来说,最初它显示当前位置有一个标记。但是当我键入。,而第二个标记已经到来。,第一个标记(当前位置)将消失。我想要两个视图上的标记。我怎么能这样做?
我的代码: 处理程序h =新处理程序(){
// Invoked by the method onTap()
// in the class CurrentLocationOverlay
@Override
public void handleMessage(Message msg) {
Bundle data = msg.getData();
// Getting the Latitude of the location
int latitude = data.getInt("latitude");
// Getting the Longitude of the location
int longitude = data.getInt("longitude");
// Show the location in the Google Map
showLocation(latitude, longitude);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting reference to map_view available in activity_main.xml
mapView = (MapView) findViewById(R.id.map_view);
// Getting reference to tv_location available in activity_main.xml
tvLocation = (TextView) findViewById(R.id.tv_location);
initLocationManager();
// Default Latitude
int latitude = 28426365;
// Default Longitude
int longitude = 77320393;
// Show the location in the Google Map
showLocation(latitude, longitude);
}
private void initLocationManager() {
mapView.setBuiltInZoomControls(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
}
@Override
public void onLocationChanged(Location location) {
TextView tvLocation = (TextView) findViewById(R.id.tv_location);
// Getting latitude
double latitude = location.getLatitude();
// Getting longitude
double longitude = location.getLongitude();
// Setting latitude and longitude in the TextView tv_location
tvLocation.setText("Latitude:" + latitude + ", Longitude:" + longitude);
// Creating an instance of GeoPoint corresponding to latitude and
// longitude
GeoPoint point = new GeoPoint((int) (latitude * 1E6),
(int) (longitude * 1E6));
// Getting MapController
MapController mapController = mapView.getController();
// Locating the Geographical point in the Map
mapController.animateTo(point);
// Applying a zoom
mapController.setZoom(15);
// Redraw the map
mapView.invalidate();
// Getting list of overlays available in the map
List<Overlay> mapOverlays = mapView.getOverlays();
// Creating a drawable object to represent the image of mark in the map
Drawable drawable = this.getResources().getDrawable(
R.drawable.ic_launcher);
// Creating an instance of ItemizedOverlay to mark the current location
// in the map
CurrentLocationOverlay currentLocationOverlay = new CurrentLocationOverlay(
drawable);
// Creating an item to represent a mark in the overlay
OverlayItem currentLocation = new OverlayItem(point,
"Current Location", "Latitude : " + latitude + ", Longitude:"
+ longitude);
// Adding the mark to the overlay
currentLocationOverlay.addOverlay(currentLocation);
// Clear Existing overlays in the map
mapOverlays.clear();
// Adding new overlay to map overlay
mapOverlays.add(currentLocationOverlay);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
private void showLocation(int latitude, int longitude) {
// Setting Latitude and Longitude in TextView
tvLocation.setText("Latitude:" + latitude / 1e6 + "," + "Longitude:"
+ longitude / 1e6);
// Setting Zoom Controls
mapView.setBuiltInZoomControls(true);
// Getting the MapController
MapController mapController = mapView.getController();
// Getting Overlays of the map
List<Overlay> overlays = mapView.getOverlays();
// Getting Drawable object corresponding to a resource image
Drawable drawable = getResources().getDrawable(R.drawable.marker);
// Creating an ItemizedOverlay
TouchedLocationOverlay locationOverlay = new TouchedLocationOverlay(
drawable, h);
// Getting the MapController
MapController mc = mapView.getController();
// Creating an instance of GeoPoint, to display in Google Map
GeoPoint p = new GeoPoint(latitude, longitude);
// Locating the point in the Google Map
mc.animateTo(p);
// Creating an OverlayItem to mark the point
OverlayItem overlayItem = new OverlayItem(p, "Item", "Item");
// Adding the OverlayItem in the LocationOverlay
locationOverlay.addOverlay(overlayItem);
// Clearing the overlays
overlays.clear();
// Adding locationOverlay to the overlay
overlays.add(locationOverlay);
// Redraws the map
mapView.invalidate();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return false;
}
private GeoPoint getPoint(double lat, double lon) {
return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
}
private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items = new ArrayList<OverlayItem>();
public SitesOverlay(Drawable marker, double lat, double lang) {
super(marker);
boundCenterBottom(marker);
items.add(new OverlayItem(getPoint(lat, lang), "", ""));
populate();
}
public SitesOverlay(Drawable marker, double[] latitude,
double[] longitude) {
super(marker);
// boundCenterBottom(marker);
for (int i = 0; i < latitude.length; i++) {
items.add(new OverlayItem(getPoint(latitude[i], longitude[i]),
"", ""));
}
populate();
}
@Override
protected OverlayItem createItem(int i) {
return (items.get(i));
}
@Override
protected boolean onTap(int i) {
/*
* Toast.makeText(LocationBasedServicesV2.this,
* items.get(i).getSnippet(), Toast.LENGTH_SHORT).show();
*/
return (true);
}
@Override
public int size() {
return (items.size());
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
TochedLocation:
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Handler handler;
public TouchedLocationOverlay(Drawable defaultMarker,Handler h) {
super(boundCenterBottom(defaultMarker));
// Handler object instantiated in the class MainActivity
this.handler = h;
}
// Executed, when populate() method is called
@Override
protected OverlayItem createItem(int arg0) {
return mOverlays.get(arg0);
}
@Override
public int size() {
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay){
mOverlays.add(overlay);
populate(); // Invokes the method createItem()
}
// This method is invoked, when user tap on the map
@Override
public boolean onTap(GeoPoint p, MapView map) {
List<Overlay> overlays = map.getOverlays();
// Creating a Message object to send to Handler
Message message = new Message();
// Creating a Bundle object ot set in Message object
Bundle data = new Bundle();
// Setting latitude in Bundle object
data.putInt("latitude", p.getLatitudeE6());
// Setting longitude in the Bundle object
data.putInt("longitude", p.getLongitudeE6());
// Setting the Bundle object in the Message object
message.setData(data);
// Sending Message object to handler
handler.sendMessage(message);
return super.onTap(p, map);
}
感谢。
答案 0 :(得分:0)
如果您希望修复first overlay
这是您当前的位置叠加层,请尝试使用以下代码并告知我们。
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.remove(1); // Here instead of clearing all overlays, just clear the last added overlay.
listOfOverlays.add(mapOverlay); // Then you can add a new overlay.
mapView.invalidate();
或
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
if(listOfOverlays.size() > 1)
listOfOverlays.remove(listOfOverlays.size()-1); // Here instead of clearing all overlays, just clear the last added overlay.
listOfOverlays.add(mapOverlay); // Then you can add a new overlay.
mapView.invalidate();