Androidi kaardirakenduse põhisource variant 1: staatilised koordinaadid/markerid

Allikas: Lambda
package com.example.hellogooglemaps;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

import com.google.android.maps.MapView.LayoutParams;  
import android.view.View;
import android.widget.LinearLayout;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;

import java.util.List;

import com.google.android.maps.Overlay;
import com.google.android.maps.MapView.LayoutParams;
 
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import android.widget.Toast;

public class HelloGoogleMapsActivity extends MapActivity { 
	
	MapView mapView; 
    MapController mc;
    GeoPoint p,p2;
    
    /** Called when the activity is first created. */
	@Override
	protected boolean isRouteDisplayed() {
	    return false;
	}
	
    class MapOverlay extends com.google.android.maps.Overlay {
	        @Override
	        public boolean draw(Canvas canvas, MapView mapView, 
	        boolean shadow, long when) 
	        {
	            super.draw(canvas, mapView, shadow);                   
	            
	            //---translate the GeoPoint created in onCreate to screen pixels---
	            Point screenPts = new Point();
	            mapView.getProjection().toPixels(p, screenPts);
	 
	            //---add the marker---
	            Bitmap bmp = BitmapFactory.decodeResource(
	                getResources(), R.drawable.map_marker);            
	            canvas.drawBitmap(bmp, screenPts.x-15, screenPts.y-32, null);	   
	            
	            // add a new marker to coords given below
	            String coordinates[] = {"59.40", "24.80"};
	            double lat = Double.parseDouble(coordinates[0]);
	            double lng = Double.parseDouble(coordinates[1]);
	            p2 = new GeoPoint( (int) (lat * 1E6), (int) (lng * 1E6));
	            Point screenPts2 = new Point();
	            mapView.getProjection().toPixels(p2, screenPts2);	            
	            canvas.drawBitmap(bmp, screenPts2.x-15, screenPts2.y-32, null); 
	            	            
	            Toast.makeText(getBaseContext(), 
                        p2.getLatitudeE6() / 1E6 + "," + 
                        p2.getLongitudeE6() /1E6 , 
                        Toast.LENGTH_SHORT).show();
	            
	            return true;
	        }
	} 
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
	   super.onCreate(savedInstanceState);
	   setContentView(R.layout.main);
	   
	   mapView = (MapView) findViewById(R.id.mapview);
       LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
       View zoomView = mapView.getZoomControls(); 

       zoomLayout.addView(zoomView, 
           new LinearLayout.LayoutParams(
               LayoutParams.WRAP_CONTENT, 
               LayoutParams.WRAP_CONTENT)); 
       mapView.displayZoomControls(true);
       
       mc = mapView.getController();
       String coordinates[] = {"59.3948", "24.662"};
       double lat = Double.parseDouble(coordinates[0]);
       double lng = Double.parseDouble(coordinates[1]);

       p = new GeoPoint(
           (int) (lat * 1E6), 
           (int) (lng * 1E6));
      
       MapOverlay mapOverlay = new MapOverlay();
       List<Overlay> listOfOverlays = mapView.getOverlays();
       listOfOverlays.clear();
       listOfOverlays.add(mapOverlay); 
       
       mc.animateTo(p);
       mc.setZoom(14); 
       mapView.invalidate();
       
   }
}