parsed coordinates from geojson, ready to add polyline via JNI #1716

This commit is contained in:
Nicholas Hallahan
2015-07-14 19:18:34 -07:00
parent 7c5fde4a3e
commit 82e88a6a77
7 changed files with 4156 additions and 1 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,7 @@
package com.mapbox.mapboxgl.testapp;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.hardware.GeomagneticField;
@@ -25,6 +26,8 @@ import android.widget.TextView;
import com.mapbox.mapboxgl.annotations.Marker;
import com.mapbox.mapboxgl.annotations.MarkerOptions;
import com.mapbox.mapboxgl.annotations.Polyline;
import com.mapbox.mapboxgl.annotations.PolylineOptions;
import com.mapbox.mapboxgl.geometry.LatLng;
import com.mapbox.mapboxgl.views.MapView;
import com.mapzen.android.lost.api.LocationListener;
@@ -32,6 +35,9 @@ import com.mapzen.android.lost.api.LocationRequest;
import com.mapzen.android.lost.api.LocationServices;
import com.mapzen.android.lost.api.LostApiClient;
import org.json.JSONException;
import java.io.IOException;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
@@ -270,6 +276,7 @@ public class MainActivity extends ActionBarActivity {
if (!mIsMarkersOn) {
mIsMarkersOn = true;
addMarkers();
addPolyline();
}
} else {
if (mIsMarkersOn) {
@@ -288,6 +295,22 @@ public class MainActivity extends ActionBarActivity {
.snippet("The back lot behind my house"));
}
private void addPolyline() {
try {
String geojsonStr = Util.loadStringFromAssets(this, "tillicum.geojson");
LatLng[] latLngs = Util.parseGeoJSONCoordinates(geojsonStr);
MapView map = mMapFragment.getMap();
// Polyline line = map.addPolyline(new PolylineOptions()
// .add(new LatLng(51.5, -0.1), new LatLng(40.7, -74.0))
// .width(5)
// .color(Color.RED));
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
private void removeMarkers() {
marker.remove();
}

View File

@@ -0,0 +1,53 @@
package com.mapbox.mapboxgl.testapp;
import android.content.Context;
import android.text.TextUtils;
import com.mapbox.mapboxgl.geometry.LatLng;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
public class Util {
public static String loadStringFromAssets(final Context context, final String fileName) throws IOException {
if (TextUtils.isEmpty(fileName)) {
throw new NullPointerException("No GeoJSON File Name passed in.");
}
InputStream is = context.getAssets().open(fileName);
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
return readAll(rd);
}
public static LatLng[] parseGeoJSONCoordinates(String geojsonStr) throws JSONException {
JSONObject jsonObject = new JSONObject(geojsonStr);
JSONObject geometry = jsonObject.getJSONObject("geometry");
JSONArray coordinates = geometry.getJSONArray("coordinates");
int len = coordinates.length();
LatLng[] latLngs = new LatLng[coordinates.length()];
for (int i = 0; i < len; ++i) {
JSONArray coord = coordinates.getJSONArray(i);
double lng = coord.getDouble(0);
double lat = coord.getDouble(1);
latLngs[i] = new LatLng(lat, lng);
}
return latLngs;
}
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
}