100を超える場所の名前、緯度と経度を含む解析jsonファイルがあります。
今までは、場所の名前、緯度、経度を取得できました。
これらすべての値をグーグルマップにプロットし、それぞれにマーカーを配置したいと思います。
また、ユーザーがグーグルマップに配置されたマーカーに触れたときにJSONを解析して得た場所の名前を表示したいと思います。
私はそれをみんな解決します。下記参照:
更新: main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0hlQp-ys3VmVGNLoJ0aGYQhDmBH5KC-ZQmc3yNA"
/>
<LinearLayout
android:id="@+id/zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
ShowMapActivity.java
public class ShowMapActivity extends MapActivity {
private static String url = "http://10.0.2.2/test/myfile.php";
JSONArray root = null;
private static final String TAG_ROOT = "root";
private static final String TAG_LATTITUDE = "lattitude";
private static final String TAG_LONGITUDE = "longitude";
private static final String TAG_NAME="name";
MapController mc;
GeoPoint p;
double lattitudeValue;
double longitudeValue;
String lattitude;
String longitude;
String name;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// showing MapView
MapView mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mc = mapView.getController();
String coordinates[] = { "53.5146152", "-2.2857034" };
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(10);
mapView.invalidate();
// Parsing JSON value and Read location name, lattitude, longitude from JSON
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
System.out.println("Testingggg..." + json.length());
try {
root = json.getJSONArray(TAG_ROOT);
for (int i = 0; i < root.length(); i++) {
JSONObject c = root.getJSONObject(i);
name=c.getString(TAG_NAME); //Getting location name
lattitude = c.getString(TAG_LATTITUDE); //Getting lattitude value in string
longitude = c.getString(TAG_LONGITUDE); //Getting longitude value in string
lattitudeValue = Double.parseDouble(lattitude); //converting string lattitude value to double
longitudeValue=Double.parseDouble(longitude); //converting string longitude value to double
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(
R.drawable.locationmarker);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
drawable, this);
GeoPoint point = new GeoPoint((int) (lattitudeValue * 1E6),
(int) (longitudeValue * 1E6));
OverlayItem overlayitem = new OverlayItem(point, name, "I'm in"
+ name);
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
CustomItemizedOverlay.java
public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private final ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public CustomItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
@Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
@Override
public int size() {
return mapOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
this.populate();
}
}
HelloItemizedOverlay.java
public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}