Skip to content

Commit

Permalink
Merge pull request #14 from mg6maciej/feature/working_on_api_8_and_re…
Browse files Browse the repository at this point in the history
…factoring

Refactoring of demos. Change demo minSdkVersion to 8.
  • Loading branch information
broady committed Aug 16, 2013
2 parents 6d41bbe + c9318e8 commit ecf92e5
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 95 deletions.
2 changes: 1 addition & 1 deletion demo/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:versionName="1.0">

<uses-sdk
android:minSdkVersion="14"
android:minSdkVersion="8"
android:targetSdkVersion="17"/>

<permission
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package com.google.maps.android.utils.demo;

import android.util.JsonReader;
import java.io.InputStream;
import java.util.List;
import java.util.Scanner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.maps.android.clustering.ClusterItem;
import com.google.maps.android.clustering.ClusterManager;
import com.google.maps.android.clustering.algo.SimpleDistanceBased;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.google.maps.android.utils.demo.model.MyItem;

public class BigClusteringDemoActivity extends BaseDemoActivity {
private ClusterManager<MyItem> mClusterManager;
Expand All @@ -25,53 +27,25 @@ protected void startDemo() {
mClusterManager.setAlgorithm(new SimpleDistanceBased<MyItem>());

getMap().setOnCameraChangeListener(mClusterManager);
for (int i = 0; i < 10; i++) {
try {
readItems(i / 60d);
} catch (IOException e) {
Toast.makeText(this, "Problem reading list of markers.", Toast.LENGTH_LONG).show();
}
try {
readItems();
} catch (JSONException e) {
Toast.makeText(this, "Problem reading list of markers.", Toast.LENGTH_LONG).show();
}
}

private void readItems(double offset) throws IOException {
private void readItems() throws JSONException {
InputStream inputStream = getResources().openRawResource(R.raw.radar_search);
JsonReader reader = new JsonReader(new InputStreamReader(inputStream));

reader.beginArray();

while (reader.hasNext()) {
reader.beginObject();
double lat = 0, lng = 0;
while (reader.hasNext()) {
String name = reader.nextName();
if ("lat".equals(name)) {
lat = reader.nextDouble() + offset;
} else if ("lng".equals(name)) {
lng = reader.nextDouble() + offset;
}
List<MyItem> items = new MyItemReader().read(inputStream);
for (int i = 0; i < 10; i++) {
double offset = i / 60d;
for (MyItem item : items) {
LatLng position = item.getPosition();
double lat = position.latitude + offset;
double lng = position.longitude + offset;
MyItem offsetItem = new MyItem(lat, lng);
mClusterManager.addItem(offsetItem);
}
mClusterManager.addItem(new MyItem(lat, lng));
reader.endObject();
}
reader.endArray();
}

private class MyItem implements ClusterItem {
private final LatLng mPosition;

public MyItem(double lat, double lng) {
mPosition = new LatLng(lat, lng);
}

@Override
public LatLng getPosition() {
return mPosition;
}

@Override
public MarkerOptions getMarkerOptions() {
return new MarkerOptions().position(mPosition);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package com.google.maps.android.utils.demo;

import android.util.JsonReader;
import java.io.InputStream;
import java.util.List;
import java.util.Scanner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.maps.android.clustering.ClusterItem;
import com.google.maps.android.clustering.ClusterManager;
import com.google.maps.android.clustering.algo.SimpleDistanceBased;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.google.maps.android.utils.demo.model.MyItem;

public class ClusteringDemoActivity extends BaseDemoActivity {
private ClusterManager<MyItem> mClusterManager;
Expand All @@ -28,49 +30,16 @@ protected void startDemo() {

try {
readItems();
} catch (IOException e) {
} catch (JSONException e) {
Toast.makeText(this, "Problem reading list of markers.", Toast.LENGTH_LONG).show();
}
}

private void readItems() throws IOException {
private void readItems() throws JSONException {
InputStream inputStream = getResources().openRawResource(R.raw.radar_search);
JsonReader reader = new JsonReader(new InputStreamReader(inputStream));

reader.beginArray();

while (reader.hasNext()) {
reader.beginObject();
double lat = 0, lng = 0;
while (reader.hasNext()) {
String name = reader.nextName();
if ("lat".equals(name)) {
lat = reader.nextDouble();
} else if ("lng".equals(name)) {
lng = reader.nextDouble();
}
}
mClusterManager.addItem(new MyItem(lat, lng));
reader.endObject();
}
reader.endArray();
}

private class MyItem implements ClusterItem {
private final LatLng mPosition;

public MyItem(double lat, double lng) {
mPosition = new LatLng(lat, lng);
}

@Override
public LatLng getPosition() {
return mPosition;
}

@Override
public MarkerOptions getMarkerOptions() {
return new MarkerOptions().position(mPosition);
List<MyItem> items = new MyItemReader().read(inputStream);
for (MyItem item : items) {
mClusterManager.addItem(item);
}
}
}
35 changes: 35 additions & 0 deletions demo/src/com/google/maps/android/utils/demo/MyItemReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.google.maps.android.utils.demo;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.maps.android.utils.demo.model.MyItem;

public class MyItemReader {

/*
* This matches only once in whole input,
* so Scanner.next returns whole InputStream as a String.
* http://stackoverflow.com/a/5445161/2183804
*/
private static final String REGEX_INPUT_BOUNDARY_BEGINNING = "\\A";

public List<MyItem> read(InputStream inputStream) throws JSONException {
List<MyItem> items = new ArrayList<MyItem>();
String json = new Scanner(inputStream).useDelimiter(REGEX_INPUT_BOUNDARY_BEGINNING).next();
JSONArray array = new JSONArray(json);
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
double lat = object.getDouble("lat");
double lng = object.getDouble("lng");
items.add(new MyItem(lat, lng));
}
return items;
}
}
23 changes: 23 additions & 0 deletions demo/src/com/google/maps/android/utils/demo/model/MyItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.google.maps.android.utils.demo.model;

import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.maps.android.clustering.ClusterItem;

public class MyItem implements ClusterItem {
private final LatLng mPosition;

public MyItem(double lat, double lng) {
mPosition = new LatLng(lat, lng);
}

@Override
public LatLng getPosition() {
return mPosition;
}

@Override
public MarkerOptions getMarkerOptions() {
return new MarkerOptions().position(mPosition);
}
}

0 comments on commit ecf92e5

Please sign in to comment.