2013-04-17 2 views
0

Я делаю приложение карты в android. Для этого мне нужно показать текущее местоположение с одним изображением маркера. И когда я набираю любой адрес в верхнем поле editText, мне нужно показать tht с другим маркером + я могу изменить этот второй маркер в выбранные местоположения. Для меня сначала он показывает текущее местоположение с одним маркером. Но когда я набираю., в то время как второй маркер пришел., этот первый маркер (текущее местоположение) будет disapper.I хочу иметь как два маркера на view.How я мог это сделать?Android-карта с разным местоположением с другим изображением маркера

Мой код: Handler ч = Handler() {

// 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

использование Google Map v2 – rajeshwaran

+0

@rajesh, как я мог бы использовать карту v2? – sanjay

+0

см. Этот http://stackoverflow.com/questions/13855049/how-to-show-multiple-markers-on-mapfragment-in-google-map-api-v2 – rajeshwaran

ответ

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(); 
Смежные вопросы