@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mMapView = (MapView) findViewById(R.id.mapview); mSearchEditText = (EditText) findViewById(R.id.searchtext); mMapController = mMapView.getController(); mControlPanel = (RelativeLayout) findViewById(R.id.control_panel); mMapModeView = (ImageView) findViewById(R.id.map_mode); mSatelliteModeView = (ImageView) findViewById(R.id.sat_mode); mTrafficModeView = (ImageView) findViewById(R.id.traffic_mode); mSearchbar = (LinearLayout) findViewById(R.id.searchbar); // Pre-load the tiles so that maps can get loaded faster. mMapView.preLoad(); // Turn on the map mode and set the zoom level. mMapController.setZoom(MAP_ZOOM_LEVEL); mMapView.setSatellite(false); mMapView.setBuiltInZoomControls(true); mSearchEditText.setFocusable(true); setSearchEditorActionListner(); }
/** * Search the geo-location and zoom the map to that point. * * @param addressEntered The address entered by the user to search. */ private void goToLocation(final String addressEntered) { if (!TextUtils.isEmpty(addressEntered.trim())) { Geocoder geoCode = new Geocoder(this); try { // Get top 5 matched address for this location. final List<Address> foundAdresses = geoCode.getFromLocationName(addressEntered, 5); if (foundAdresses.isEmpty()) { // If no address found, show error through toast. Toast.makeText(this, getResources().getText(R.string.error_message), Toast.LENGTH_LONG) .show(); } else { // Else display address on map and // store results as longitude and latitude. Address address = foundAdresses.get(ZERO_INDEX); Double latitude = address.getLatitude() * 1E6; Double longitude = address.getLongitude() * 1E6; GeoPoint point = new GeoPoint(latitude.intValue(), longitude.intValue()); List<Overlay> mapOverlays = mMapView.getOverlays(); Drawable drawable = this.getResources().getDrawable(R.drawable.marker); MapsMarkerOverlay itemizedoverlay = new MapsMarkerOverlay(drawable, getApplicationContext()); OverlayItem overlayitem = new OverlayItem(point, null, null); itemizedoverlay.addOverlay(overlayitem); if (mapOverlays.size() > 0) { mapOverlays.remove(0); } mapOverlays.add(itemizedoverlay); mMapView.preLoad(); // Show the location in the center of the map. mMapController.setCenter(point); mMapController.animateTo(point); mMapController.setZoom(LOCATION_ZOOM_LEVEL); mMapView.invalidate(); } } catch (IOException e) { e.printStackTrace(); } } }