Androidi kaardirakenduse põhisource variant 2: võrgust loetavalt koordinaadilt marker
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; import java.net.*; import java.io.*; import android.util.Log; 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); // fetch coordinates from appengine Log.i ("info","algus"); URL coordurl; BufferedReader in=null; try { coordurl = new URL("http://vorgurak2.appspot.com/"); in = new BufferedReader( new InputStreamReader( coordurl.openStream())); } catch (Exception ex) { Log.i ("info","err opening url"); } String inputLine; String[] lines; lines=new String[100]; int i=0; try { while ((inputLine = in.readLine()) != null) { //System.out.println(inputLine); lines[i]=inputLine; i++; } in.close(); } catch (Exception ex) { Log.i ("info","err reading from url"); } // add a new marker to coords read from URL String line=lines[0]; int pos; pos=line.indexOf(' '); String lats=line.substring(0,pos); String lngs=line.substring(pos+1,line.length()); double lat=Double.parseDouble(lats); double lng=Double.parseDouble(lngs); 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 is one way to put messages onscreen, // probably not a good choice for this app 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(); } }