Skip to content

Instantly share code, notes, and snippets.

@theSuess
Created October 4, 2014 15:35
Show Gist options
  • Save theSuess/c354489ff540154f4215 to your computer and use it in GitHub Desktop.
Save theSuess/c354489ff540154f4215 to your computer and use it in GitHub Desktop.
Android Map start location chooser
package com.trumpstuff.mapdemo;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
/**
* Created on 04.10.2014.
*/
public class CustomPreferenceActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CustomPreferenceFragment customPreferenceFragment = new CustomPreferenceFragment();
getFragmentManager().beginTransaction().replace(android.R.id.content,customPreferenceFragment).commit();
PreferenceManager.setDefaultValues(this,R.xml.preferences,false);
}
}
package com.trumpstuff.mapdemo;
import android.app.Activity;
import android.content.SharedPreferences;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
import java.io.IOException;
import java.util.List;
/**
* Created on 04.10.2014.
*/
public class CustomPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
Preference pref = findPreference(s);
if(pref != null && pref.getKey().equals("start_location")){
updateLocationSetting(pref);
}
//Other preference handling
}
private void updateLocationSetting(Preference preference){
if(((EditTextPreference) preference).getText().equals("Global")){
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(
getActivity());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("start_location_lat", "0");
editor.putString("start_location_lng", "0");
editor.apply();
return;
}
if(Geocoder.isPresent()){
Geocoder geocoder = new Geocoder(getActivity());
try {
List<Address> addresses = geocoder.getFromLocationName(((EditTextPreference) preference).getText(), 1);
if (addresses != null && addresses.size() > 0){
Address address = addresses.get(0);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("start_location_lat", String.valueOf(address.getLatitude()));
editor.putString("start_location_lng", String.valueOf(address.getLongitude()));
editor.apply();
Log.d("pref", sharedPreferences.getAll().toString());
}
else{
Toast.makeText(getActivity(), "Could not resolve Address: " + ((EditTextPreference) preference).getText(), Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
}
}
else{
Toast.makeText(getActivity(),"Geocoder not available on your system",Toast.LENGTH_LONG).show();
}
}
@Override
public void onResume() {
super.onResume();
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
super.onPause();
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
}
package com.trumpstuff.mapdemo;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
/**
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
* installed) and the map has not already been instantiated.. This will ensure that we only ever
* call {@link #setUpMap()} once when {@link #mMap} is not null.
* <p>
* If it isn't installed {@link SupportMapFragment} (and
* {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
* install/update the Google Play services APK on their device.
* <p>
* A user can return to this FragmentActivity after following the prompt and correctly
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not
* have been completely destroyed during this process (it is likely that it would only be
* stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
* method in {@link #onResume()} to guarantee that it will be called.
*/
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
/**
* This is where we can add markers or lines, add listeners or move the camera. In this case, we
* just add a marker near Africa.
* <p>
* This should only be called once and when we are sure that {@link #mMap} is not null.
*/
private void setUpMap() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
Log.e("pref", sharedPreferences.getAll().toString());
double lat = Double.parseDouble(sharedPreferences.getString("start_location_lat","0"));
double lng = Double.parseDouble(sharedPreferences.getString("start_location_lng","0"));
LatLng position = new LatLng(lat,lng);
float zoom = 10;
if(lat == 0 && lng == 0){
zoom = mMap.getMinZoomLevel();
}
Log.d("zoom", String.valueOf(zoom));
CameraUpdate centerStartPosition = CameraUpdateFactory.newLatLngZoom(position,zoom);
mMap.animateCamera(centerStartPosition);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_MENU){
startActivityForResult(new Intent(this,CustomPreferenceActivity.class),42);
return true;
}
return super.onKeyUp(keyCode, event);
}
}
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="start_location"
android:defaultValue="Global"
android:title="Start Location"
android:summary="The start Location for the Map"/>
</PreferenceScreen>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment