-
Notifications
You must be signed in to change notification settings - Fork 2
Android Geo howto
k3b edited this page Nov 18, 2023
·
4 revisions
- (1) GEOClientActivity creates and sends an android ACTION_VIEW intent with coordinate in geo: format.
- (2) Android checks the manifests which apps-activities want "ACTION_VIEW" with uri-schema "geo:" and presents an Activity chooser with all capable apps-activities.
- Lets assume that the user chooses to view the item in GEOServiceActivity.
- Android creates GEOServiceActivity
- (3) In GEOServiceActivity.onCreate()
- the intenet is retrieved
- the intenet-s geo-uri is parsed
- and processed (i.e. displayed in a map)
Note: Instead of ACTION_VIEW (to view something on the screen) you can also use ACTION_SEND if you want to share/process a shared location (i.e. to be send as email/sms).
GEOClientActivity .java : wants to "view geo"
import de.k3b.geo.io.GeoUri;
import de.k3b.geo.api.GeoPointDto;
public class GEOClientActivity extends Activity {
...
private void onButtonClick[] {
double lat = 52.1;
double lon = 9.2;
// no need for de.k3b.geo-lib for simple request:
String uriGeoSimple = String.format(Locale.ENGLISH, "geo:%f,%f", lat, lon);
String uriGoogleMaps = String.format(Locale.ENGLISH, "http://maps.google.com/maps?q=loc:%f,%f", lat, lon);
// use de.k3b.geo.io.GeoUri for more complex parameters
GeoUri formater = new GeoUri(GeoUri.OPT_DEFAULT);
GeoPointDto geo = new GeoPointDto()
.setLatitude(52.1)
.setLongitude(9.2)
.setName("My geo point");
.setZoomMin(14);
String geoUri = formater.toUriString(geo);
// (1) GEOClientActivity creates and sends an android ACTION_VIEW intent
// with coordinate
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(geoUri));
startActivity(intent);
}
]
(2) AndroidManifest.xml tells android that GEOServiceActivity whants to process "view geo:"
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<application ... >
<activity android:name=".GEOServiceActivity" ... >
<intent-filter>
<!-- (2) GEOServiceActivity wants to process "android.intent.action.VIEW" -->
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- (2) GEOServiceActivity wants to process any mime type "*/*" -->
<data android:mimeType="*/*" />
<!-- (2) GEOServiceActivity wants to process uri schema "geo:" -->
<data android:scheme="geo" />
</intent-filter>
</activity>
</application>
</manifest>
(3) GEOServiceActivity.java in onCreate() geo data is retrieved and processed
import de.k3b.geo.io.GeoUri;
import de.k3b.geo.api.GeoPointDto;
public class GEOServiceActivity extends Activity {
...
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
// (3) get geo-uri
final Intent intent = getIntent();
Uri uri = intent.getData();
if (uri != null) {
// (3) parse and process geo-uri
GeoUri parser = new GeoUri(GeoUri.OPT_DEFAULT);
IGeoPointInfo geo = parser.fromUri(uri.toString());
if (geo != null) {
// (3) process geo-data
System.out.print(String.format("got lat=%f lon=%f", geo.getLatitude(),geo.getLongitude()));
} // null means no geo data found
}
}
}
Here is example geo-picker-client code that asks for geo data:
private static final int GEO_RESULT_ID = 572;
public void showGeoPicker() {
final Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setData(Uri.parse("geo:"));
// optional: show user defined title in picker
// intent.putExtra(EXTRA_TITLE, getString(R.string.geo_picker_title));
try {
startActivityForResult(Intent.createChooser(intent, getString(R.string.geo_edit_menu_title)), GEO_RESULT_ID);
// this.startActivityForResult(intent, GEO_RESULT_ID);
} catch (ActivityNotFoundException ex) {
Toast.makeText(this, R.string.geo_picker_err_not_found,Toast.LENGTH_LONG).show();
}
}
/**
* Call back from sub-activities.
*/
@Override
protected void onActivityResult(final int requestCode,
final int resultCode, final Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case GEO_RESULT_ID:
String pickedGeoUri = (intent != null) ? intent.getData() : null;
onGeoChanged(pickedGeoUri);
break;
default:break;
}
}