Add gps button
This commit is contained in:
@@ -1,19 +1,21 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.mapbox.mapboxgl.app" >
|
||||
package="com.mapbox.mapboxgl.app">
|
||||
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
|
||||
<application
|
||||
android:allowBackup="false"
|
||||
android:icon="@drawable/icon_burned"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/AppTheme" >
|
||||
android:theme="@style/AppTheme">
|
||||
<activity
|
||||
android:name="com.mapbox.mapboxgl.app.MainActivity"
|
||||
android:label="@string/app_name"
|
||||
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|smallestScreenSize" >
|
||||
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|smallestScreenSize">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
@@ -1,16 +1,22 @@
|
||||
package com.mapbox.mapboxgl.app;
|
||||
|
||||
import android.location.Criteria;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.support.v7.app.ActionBarActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Spinner;
|
||||
|
||||
import com.mapbox.mapboxgl.lib.MapView;
|
||||
import com.mapbox.mapboxgl.lib.LonLatZoom;
|
||||
|
||||
public class MainActivity extends ActionBarActivity {
|
||||
|
||||
@@ -42,18 +48,70 @@ public class MainActivity extends ActionBarActivity {
|
||||
setContentView(R.layout.activity_main);
|
||||
mMapFragment = (MapFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_map);
|
||||
|
||||
// TODO move to layout file and save state
|
||||
// Add the spinner to select map styles
|
||||
Spinner styleSpinner = (Spinner)findViewById(R.id.spinner_style);
|
||||
ArrayAdapter styleAdapter = ArrayAdapter.createFromResource(this,
|
||||
R.array.style_list, android.R.layout.simple_spinner_dropdown_item);
|
||||
styleSpinner.setAdapter(styleAdapter);
|
||||
styleSpinner.setOnItemSelectedListener(new StyleSpinnerListener());
|
||||
|
||||
// Add a toolbar as the action bar
|
||||
Toolbar mainToolbar = (Toolbar)findViewById(R.id.toolbar_main);
|
||||
setSupportActionBar(mainToolbar);
|
||||
getSupportActionBar().setDisplayShowTitleEnabled(false);
|
||||
|
||||
// TODO move to layout file and save state
|
||||
// Add the spinner to select map styles
|
||||
Spinner styleSpinner = (Spinner)findViewById(R.id.spinner_style);
|
||||
ArrayAdapter styleAdapter = ArrayAdapter.createFromResource(getSupportActionBar().getThemedContext(),
|
||||
R.array.style_list, android.R.layout.simple_spinner_dropdown_item);
|
||||
styleSpinner.setAdapter(styleAdapter);
|
||||
styleSpinner.setOnItemSelectedListener(new StyleSpinnerListener());
|
||||
}
|
||||
|
||||
// Adds items to the action bar menu
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
MenuInflater inflater = getMenuInflater();
|
||||
inflater.inflate(R.menu.menu_main, menu);
|
||||
return super.onCreateOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
// Handle presses on the action bar items
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_gps:
|
||||
// Get a GPS position
|
||||
LocationManager locationManager = (LocationManager)getSystemService(getApplicationContext().LOCATION_SERVICE);
|
||||
String provider = locationManager.getBestProvider(new Criteria(), true);
|
||||
Location location = locationManager.getLastKnownLocation(provider);
|
||||
LonLatZoom coordinate = new LonLatZoom(location.getLongitude(), location.getLatitude(), 15);
|
||||
mMapFragment.getMap().setCenterCoordinate(coordinate, true);
|
||||
locationManager.requestSingleUpdate(provider, new MyLocationListener(), null);
|
||||
return true;
|
||||
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
||||
// This class handles location events
|
||||
private class MyLocationListener implements LocationListener {
|
||||
|
||||
@Override
|
||||
public void onLocationChanged(Location location) {
|
||||
LonLatZoom coordinate = new LonLatZoom(location.getLongitude(), location.getLatitude(), 15);
|
||||
mMapFragment.getMap().setCenterCoordinate(coordinate, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStatusChanged(String provider, int status, Bundle extras) {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProviderEnabled(String provider) {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProviderDisabled(String provider) {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
|
||||
// This class handles navigation events
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 722 B |
Binary file not shown.
|
After Width: | Height: | Size: 484 B |
Binary file not shown.
|
After Width: | Height: | Size: 994 B |
Binary file not shown.
|
After Width: | Height: | Size: 1.4 KiB |
@@ -1,22 +1,22 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<android.support.v7.widget.Toolbar
|
||||
android:id="@+id/toolbar_main"
|
||||
android:layout_height="?attr/actionBarSize"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_width="match_parent"
|
||||
android:minHeight="?attr/actionBarSize"
|
||||
android:background="?attr/colorPrimary">
|
||||
|
||||
<Spinner
|
||||
android:id="@+id/spinner_style"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
</android.support.v7.widget.Toolbar>
|
||||
|
||||
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
|
||||
<fragment
|
||||
tools:context="${packageName}.${activityClass}"
|
||||
android:name="com.mapbox.mapboxgl.app.MapFragment"
|
||||
android:id="@+id/fragment_map"
|
||||
|
||||
7
android/java/app/src/main/res/menu/menu_main.xml
Normal file
7
android/java/app/src/main/res/menu/menu_main.xml
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<item android:id="@+id/action_gps"
|
||||
android:icon="@drawable/ic_action_location_found"
|
||||
android:title="@string/action_gps"
|
||||
app:showAsAction="ifRoom" />
|
||||
</menu>
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
<string name="app_name">Mapbox GL</string>
|
||||
|
||||
<string name="action_gps">Go to GPS location</string>
|
||||
|
||||
<string-array name="style_list">
|
||||
<item>Bright</item>
|
||||
<item>Basic</item>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.mapbox.mapboxgl.lib" >
|
||||
package="com.mapbox.mapboxgl.lib">
|
||||
|
||||
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ public class LonLat implements Parcelable {
|
||||
this.lat = lat;
|
||||
}
|
||||
|
||||
private LonLat(Parcel in) {
|
||||
protected LonLat(Parcel in) {
|
||||
lon = in.readDouble();
|
||||
lat = in.readDouble();
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.mapbox.mapboxgl.lib;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
public class LonLatZoom implements Parcelable {
|
||||
public class LonLatZoom extends LonLat implements Parcelable {
|
||||
|
||||
public static final Parcelable.Creator<LonLatZoom> CREATOR = new Parcelable.Creator<LonLatZoom>() {
|
||||
public LonLatZoom createFromParcel(Parcel in) {
|
||||
@@ -15,44 +15,23 @@ public class LonLatZoom implements Parcelable {
|
||||
}
|
||||
};
|
||||
|
||||
private double lon;
|
||||
private double lat;
|
||||
private double zoom;
|
||||
|
||||
public LonLatZoom(double lon, double lat, double zoom) {
|
||||
this.lon = lon;
|
||||
this.lat = lat;
|
||||
super(lon, lat);
|
||||
this.zoom = zoom;
|
||||
}
|
||||
|
||||
public LonLatZoom(LonLat lonLat, double zoom) {
|
||||
this.lon = lonLat.getLon();
|
||||
this.lat = lonLat.getLat();
|
||||
super(lonLat.getLon(), lonLat.getLat());
|
||||
this.zoom = zoom;
|
||||
}
|
||||
|
||||
private LonLatZoom(Parcel in) {
|
||||
lon = in.readDouble();
|
||||
lat = in.readDouble();
|
||||
super(in);
|
||||
zoom = in.readDouble();
|
||||
}
|
||||
|
||||
public double getLon() {
|
||||
return lon;
|
||||
}
|
||||
|
||||
public void setLon(double lon) {
|
||||
this.lon = lon;
|
||||
}
|
||||
|
||||
public double getLat() {
|
||||
return lat;
|
||||
}
|
||||
|
||||
public void setLat(double lat) {
|
||||
this.lat = lat;
|
||||
}
|
||||
|
||||
public double getZoom() {
|
||||
return zoom;
|
||||
}
|
||||
@@ -61,25 +40,12 @@ public class LonLatZoom implements Parcelable {
|
||||
this.zoom = zoom;
|
||||
}
|
||||
|
||||
public LonLat getLonLat() {
|
||||
return new LonLat(lon, lat);
|
||||
}
|
||||
|
||||
public void setLonLat(LonLat lonLat) {
|
||||
this.lon = lonLat.getLon();
|
||||
this.lat = lonLat.getLat();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(lat);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
temp = Double.doubleToLongBits(lon);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
temp = Double.doubleToLongBits(zoom);
|
||||
temp = super.hashCode();
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
return result;
|
||||
}
|
||||
@@ -93,12 +59,12 @@ public class LonLatZoom implements Parcelable {
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
LonLatZoom other = (LonLatZoom) obj;
|
||||
return Double.doubleToLongBits(lat) == Double.doubleToLongBits(other.lat) && Double.doubleToLongBits(lon) == Double.doubleToLongBits(other.lon) && Double.doubleToLongBits(zoom) == Double.doubleToLongBits(other.zoom);
|
||||
return super.equals(obj) && Double.doubleToLongBits(zoom) == Double.doubleToLongBits(other.zoom);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LonLatZoom [lon=" + lon + ", lat=" + lat + ", zoom=" + zoom
|
||||
return "LonLatZoom [lon=" + super.getLon() + ", lat=" + super.getLat() + ", zoom=" + zoom
|
||||
+ "]";
|
||||
}
|
||||
|
||||
@@ -109,8 +75,7 @@ public class LonLatZoom implements Parcelable {
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel out, int flags) {
|
||||
out.writeDouble(lon);
|
||||
out.writeDouble(lat);
|
||||
super.writeToParcel(out, flags);
|
||||
out.writeDouble(zoom);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user