001.
package
app.meter.com.meter;
002.
003.
import
android.os.Bundle;
004.
import
android.os.StrictMode;
005.
import
android.support.v4.app.FragmentActivity;
006.
import
android.util.Log;
007.
008.
import
com.google.android.gms.maps.CameraUpdateFactory;
009.
import
com.google.android.gms.maps.GoogleMap;
010.
import
com.google.android.gms.maps.SupportMapFragment;
011.
import
com.google.android.gms.maps.model.LatLng;
012.
import
com.google.android.gms.maps.model.MarkerOptions;
013.
014.
import
org.apache.http.HttpEntity;
015.
import
org.apache.http.HttpResponse;
016.
import
org.apache.http.StatusLine;
017.
import
org.apache.http.client.ClientProtocolException;
018.
import
org.apache.http.client.HttpClient;
019.
import
org.apache.http.client.methods.HttpGet;
020.
import
org.apache.http.impl.client.DefaultHttpClient;
021.
import
org.json.JSONArray;
022.
import
org.json.JSONException;
023.
import
org.json.JSONObject;
024.
025.
import
java.io.BufferedReader;
026.
import
java.io.IOException;
027.
import
java.io.InputStream;
028.
import
java.io.InputStreamReader;
029.
import
java.util.ArrayList;
030.
import
java.util.HashMap;
031.
032.
033.
public
class
MapsActivity
extends
FragmentActivity {
034.
035.
036.
private
GoogleMap googleMap;
037.
038.
039.
private
Double LAT =
0.00
;
040.
private
Double LONG =
0.00
;
041.
042.
protected
void
onCreate(Bundle savedInstanceState) {
043.
super
.onCreate(savedInstanceState);
044.
setContentView(R.layout.activity_maps);
045.
046.
047.
if
(android.os.Build.VERSION.SDK_INT >
9
) {
048.
StrictMode.ThreadPolicy policy =
new
StrictMode.ThreadPolicy.Builder().permitAll().build();
049.
StrictMode.setThreadPolicy(policy);
050.
}
051.
052.
ArrayList<HashMap<String, String>> location =
null
;
054.
try
{
055.
056.
JSONArray data =
new
JSONArray(getHttpGet(url));
057.
058.
location =
new
ArrayList<HashMap<String, String>>();
059.
HashMap<String, String> map;
060.
061.
for
(
int
i =
0
; i < data.length(); i++){
062.
JSONObject c = data.getJSONObject(i);
063.
064.
map =
new
HashMap<String, String>();
065.
map.put(
"FID"
, c.getString(
"FID"
));
066.
map.put(
"LAT"
, c.getString(
"LAT"
));
067.
map.put(
"LONG"
, c.getString(
"LONG"
));
068.
map.put(
"PEANO"
, c.getString(
"PEANO"
));
069.
location.add(map);
070.
071.
}
072.
073.
}
catch
(JSONException e) {
074.
075.
e.printStackTrace();
076.
}
077.
078.
079.
080.
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap)).getMap();
081.
082.
083.
LAT = Double.parseDouble(location.get(
0
).get(
"LAT"
).toString());
084.
LONG = Double.parseDouble(location.get(
0
).get(
"LONG"
).toString());
085.
LatLng coordinate =
new
LatLng(LAT, LONG);
086.
googleMap.setMapType(com.google.android.gms.maps.GoogleMap.MAP_TYPE_HYBRID);
087.
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate,
17
));
088.
089.
090.
for
(
int
i =
0
; i < location.size(); i++) {
091.
LAT = Double.parseDouble(location.get(i).get(
"LAT"
).toString());
092.
LONG = Double.parseDouble(location.get(i).get(
"LONG"
).toString());
093.
String name = location.get(i).get(
"PEANO"
).toString();
094.
MarkerOptions marker =
new
MarkerOptions().position(
new
LatLng(LAT, LONG)).title(name);
095.
googleMap.addMarker(marker);
096.
}
097.
098.
}
099.
100.
public
static
String getHttpGet(String url) {
101.
StringBuilder str =
new
StringBuilder();
102.
HttpClient client =
new
DefaultHttpClient();
103.
HttpGet httpGet =
new
HttpGet(url);
104.
try
{
105.
HttpResponse response = client.execute(httpGet);
106.
StatusLine statusLine = response.getStatusLine();
107.
int
statusCode = statusLine.getStatusCode();
108.
if
(statusCode ==
200
) {
109.
HttpEntity entity = response.getEntity();
110.
InputStream content = entity.getContent();
111.
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader(content));
112.
String line;
113.
while
((line = reader.readLine()) !=
null
) {
114.
str.append(line);
115.
}
116.
}
else
{
117.
Log.e(
"Log"
,
"Failed to download result.."
);
118.
}
119.
}
catch
(ClientProtocolException e) {
120.
e.printStackTrace();
121.
}
catch
(IOException e) {
122.
e.printStackTrace();
123.
}
124.
return
str.toString();
125.
}
126.
127.
}