Skip to content

Commit

Permalink
Zprovozneni vyberu souradnic jako activity result
Browse files Browse the repository at this point in the history
  • Loading branch information
Saljack committed Nov 18, 2012
1 parent 4195fbf commit a698513
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 4 deletions.
6 changes: 6 additions & 0 deletions res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/ma_settings"
/>
<Button
android:id="@+id/ma_mapSelectCoordinate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/ma_coordinate"
/>
</LinearLayout>

15 changes: 15 additions & 0 deletions res/layout/map_select_coordinates.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.google.android.maps.MapView
android:id="@+id/msc_Map"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:enabled="true"
android:clickable="true"
android:apiKey="0FWL-kvtGWbNHGzJfBDEDEGUfv-3YSFzDjNXJAw"
/>

</RelativeLayout>
6 changes: 6 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
<string name="ma_maps">Mapy</string>
<string name="ma_friends">Přátelé</string>
<string name="ma_settings">Nastavení</string>
<string name="ma_coordinate">Souřadnice</string>

<!-- SettingsActivity -->
<string name="sa_first_option">První volba</string>
<string name="sa_second_option">Druhá volba</string>
<string name="sa_normal_mode">Běžný mód</string>
<string name="sa_blind_mode">Mód naslepo</string>

<!-- MapSelectCoordinates -->
<string name="msc_wantSelect">Chcete vybrat</string>
<string name="msc_point">Chcete vybrat tento bod:</string>

</resources>
46 changes: 42 additions & 4 deletions src/cz/cvut/fel/nur/zavody/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import com.google.android.maps.GeoPoint;
import cz.cvut.fel.nur.zavody.activity.NormalMode;
import cz.cvut.fel.nur.zavody.activity.Friends;
import cz.cvut.fel.nur.zavody.activity.MapSelectCoordinates;
import cz.cvut.fel.nur.zavody.activity.Settings;

/**
Expand All @@ -21,6 +23,7 @@ public class MainActivity extends Activity {
private Button _mapsButton;
private Button _friendsButton;
private Button _settingsButton;
private Button _mapSelectCoordinates;

/**
* Called when the activity is first created.
Expand All @@ -35,20 +38,27 @@ public void onClick(View v) {
startMapsActivity();
}
});

_friendsButton = (Button) findViewById(R.id.ma_button02);
_friendsButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startFriendsActivity();
}
});

_settingsButton = (Button) findViewById(R.id.ma_button03);
_settingsButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startSettingsActivity();
}
});

_mapSelectCoordinates = (Button) findViewById(R.id.ma_mapSelectCoordinate);
_mapSelectCoordinates.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startMapSelectCoordinatesActivity();
}
});
}

private void startMapsActivity() {
Expand All @@ -57,12 +67,12 @@ private void startMapsActivity() {
Intent intent = new Intent(MainActivity.this, NormalMode.class);
startActivity(intent);
}

private void startFriendsActivity() {
Intent intent = new Intent(MainActivity.this, Friends.class);
startActivity(intent);
}

private void startSettingsActivity() {
Intent intent = new Intent(MainActivity.this, Settings.class);
startActivity(intent);
Expand All @@ -74,4 +84,32 @@ public boolean onCreateOptionsMenu(Menu menu) {
inflater.inflate(R.menu.main_menu, menu);
return true;
}

private void startMapSelectCoordinatesActivity() {
Intent intent = new Intent(this, MapSelectCoordinates.class);
startActivityForResult(intent, 1);

}

/**
* @see android.app.Activity
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
if (data.hasExtra(MapSelectCoordinates.POINT)) {
int[] point = data.getIntArrayExtra(MapSelectCoordinates.POINT);
((Zavody) getApplication()).setFinish(new GeoPoint(point[0], point[1]));
}
}

if (resultCode == RESULT_CANCELED) {
//Write your code on no result return
}
}
}
}
77 changes: 77 additions & 0 deletions src/cz/cvut/fel/nur/zavody/activity/MapSelectCoordinates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cz.cvut.fel.nur.zavody.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import cz.cvut.fel.nur.zavody.R;
import cz.cvut.fel.nur.zavody.maps.MapTapOverlay;

/**
* This activity can finish with result of coordinates of selected point. This
* point is save in intent (POINT string) as int array where first is Latitude and second is
* Longitude.
*
* @author saljack
*/
public class MapSelectCoordinates extends MapActivity {

public static String POINT = "cz.cvut.fel.nur.zavody.activity.Point";
private MapView _mapView;

/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.map_select_coordinates);

_mapView = (MapView) findViewById(R.id.msc_Map);
_mapView.setBuiltInZoomControls(true);
_mapView.getOverlays().add(new MapTapOverlay(this));
}

@Override
protected boolean isRouteDisplayed() {
return false;
}

public void acceptPoint(GeoPoint arg0) {
final GeoPoint point = arg0;
DialogInterface.OnClickListener negative = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
};
DialogInterface.OnClickListener positive = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//TODO finisActivity
endActivityWithPoint(point);
}
};
Dialog _dlg = new AlertDialog.Builder(this).setTitle(R.string.msc_wantSelect)
.setMessage(getString(R.string.msc_point) + "\n" + arg0.toString())
.setNegativeButton(android.R.string.cancel, negative)
.setPositiveButton(android.R.string.ok, positive).create();

_dlg.show();
}

private void endActivityWithPoint(GeoPoint point) {
Intent result = new Intent();
result.putExtra(POINT, new int[]{point.getLatitudeE6(), point.getLongitudeE6()});
setResult(Activity.RESULT_OK, result);
finish();
}
}
49 changes: 49 additions & 0 deletions src/cz/cvut/fel/nur/zavody/maps/MapTapOverlay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cz.cvut.fel.nur.zavody.maps;

import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Canvas;
import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import cz.cvut.fel.nur.zavody.activity.MapSelectCoordinates;

/**
*
* @author saljack
*/
public class MapTapOverlay extends Overlay {

public static final String TAG = "MapTapOverlay";
private MapSelectCoordinates _mapsAc;

public MapTapOverlay(MapSelectCoordinates _mapsAc) {
this._mapsAc = _mapsAc;
}

@Override
public void draw(Canvas arg0, MapView arg1, boolean arg2) {

super.draw(arg0, arg1, arg2);
}

@Override
public boolean onTap(GeoPoint arg0, MapView arg1) {
//TODO co se souradnicema
final GeoPoint point = arg0;
if (_mapsAc != null) {
_mapsAc.runOnUiThread(new Runnable() {
public void run() {
_mapsAc.acceptPoint(point);
}
});

}
return true;
}
}

0 comments on commit a698513

Please sign in to comment.