Menampilkan balon pada Google Map

public class Map extends MapActivity {
double x, y;
private Context c;
Timer timer;
LocationManager lm;
MapView mapView;
boolean gps_enabled = false;
boolean network_enabled = false;
private ArrayList<Lokasi> list_lokasi = new ArrayList<Lokasi>();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_gps);
// mapView = (MapView) findViewById(R.id.mapView);

initMap();
cek();
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = lm
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (!gps_enabled && !network_enabled) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "nothing is enabled",
duration);
toast.show();

}

if (gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1000,
locationListenerGps);
if (network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
1000, locationListenerNetwork);
timer = new Timer();
timer.schedule(new GetLastLocation(), 30000);

}

LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer.cancel();
x = location.getLatitude();
y = location.getLongitude();
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
tampilkanPosisikeMap(location);

Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,
"gps enabled " + x + "\n" + y, duration);
toast.show();
}

public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

public void onStatusChanged(String provider, int status, Bundle extras) {
}

};

LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer.cancel();
x = location.getLatitude();
y = location.getLongitude();
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
tampilkanPosisikeMap(location);
Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "network enabled adalah " + x
+ "\n" + y, duration);
toast.show();

}

public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

public void onStatusChanged(String provider, int status, Bundle extras) {
}
};

class GetLastLocation extends TimerTask {
@Override
public void run() {
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);

Location net_loc = null, gps_loc = null;
if (gps_enabled)
gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (network_enabled)
net_loc = lm
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

// if there are both values use the latest one
if (gps_loc != null && net_loc != null) {
if (gps_loc.getTime() > net_loc.getTime()) {
x = gps_loc.getLatitude();
y = gps_loc.getLongitude();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "gps lastknown " + x
+ "\n" + y, duration);
toast.show();
} else {
x = net_loc.getLatitude();
y = net_loc.getLongitude();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "network lastknown "
+ x + "\n" + y, duration);
toast.show();

}

}

if (gps_loc != null) {
{
x = gps_loc.getLatitude();
y = gps_loc.getLongitude();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "gps lastknown " + x
+ "\n" + y, duration);
toast.show();
}

}
if (net_loc != null) {
{
x = net_loc.getLatitude();
y = net_loc.getLongitude();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "network lastknown "
+ x + "\n" + y, duration);
toast.show();

}
}
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "no last know avilable",
duration);
toast.show();

}
}

protected void tampilkanPosisikeMap(Location newLocation) {
List<Overlay> overlays = mapView.getOverlays();

// first remove old overlay
if (overlays.size() > 0) {
for (Iterator iterator = overlays.iterator(); iterator.hasNext();) {
iterator.next();
iterator.remove();
}
}

// transform the location to a geopoint
GeoPoint geopoint = new GeoPoint(
(int) (newLocation.getLatitude() * 1E6),
(int) (newLocation.getLongitude() * 1E6));
GeoPoint myposition = geopoint;
Location locationA = new Location("point A");
Location locationB = new Location("point B");
locationA.setLatitude(myposition.getLatitudeE6() / 1E6);
locationA.setLongitude(myposition.getLongitudeE6() / 1E6);

// initialize icon
Drawable icon = getResources().getDrawable(R.drawable.marker);
icon.setBounds(0, 0, icon.getIntrinsicWidth(),
icon.getIntrinsicHeight());

// create my overlay and show it
MyItemizedOverlay overlay = new MyItemizedOverlay(icon, mapView);
OverlayItem item = new OverlayItem(geopoint, "My Location",
locationA.getLatitude() + "," + locationA.getLongitude());
overlay.addOverlay(item);
mapView.getOverlays().add(overlay);

for (int i = 0; i < list_lokasi.size(); i++) {

geopoint = new GeoPoint((int) (list_lokasi.get(i).lat * 1E6),
(int) (list_lokasi.get(i).lng * 1E6));
locationB.setLongitude(geopoint.getLongitudeE6() / 1E6);
locationB.setLatitude(geopoint.getLongitudeE6() / 1E6);
double distance = locationA.distanceTo(locationB);

// set gambar
if (list_lokasi.get(i).category == 1) {
icon = getResources().getDrawable(R.drawable.ic_map_hospital);
} else if (list_lokasi.get(i).category == 2) {
icon = getResources().getDrawable(R.drawable.ic_map_hospital);
}
icon.setBounds(0, 0, icon.getIntrinsicWidth(),
icon.getIntrinsicHeight());
overlay = new MyItemizedOverlay(icon, mapView);

// information balon di klik
OverlayItem overlayitem = new OverlayItem(geopoint,
list_lokasi.get(i).lokname, +locationA.getLatitude() + ","
+ locationA.getLongitude() + ","
+ list_lokasi.get(i).lat + ","
+ list_lokasi.get(i).lng);

overlay.addOverlay(overlayitem);
mapView.getOverlays().add(overlay);

}

// move to location
mapView.getController().animateTo(geopoint);

// redraw map
mapView.postInvalidate();
}

public void cek() {
String url_select = "http://tes.com/tes.php";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_select);
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();

try {
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpRespose = httpClient.execute(httpPost);
HttpEntity httpEntity = httpRespose.getEntity();

InputStream in = httpEntity.getContent();
BufferedReader read = new BufferedReader(new InputStreamReader(in));

String content = "";
String line = "";

while ((line = read.readLine()) != null) {
content += line;
}

Log.d("ADBUG", "content: " + content);

if (!content.equals("null")) {
try {
JSONArray jArr = new JSONArray(content);
int dbidx = 0;
double dblat = 0;
double dblng = 0;
String dbket = "";
int dbkate = 0;

for (int i = 0; i < jArr.length(); i++) {
JSONObject jObj = jArr.getJSONObject(i);
int idx = jObj.getInt("id");
double lat = jObj.getDouble("latitude");
double lng = jObj.getDouble("longitude");
String keterangan = jObj.getString("keterangan");
int kategori = jObj.getInt("category");

list_lokasi.add(new Lokasi(lat, lng, kategori,
keterangan)); // --> DISINI PARSING KE MAP

}

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private void initMap() {
mapView = (MapView) findViewById(R.id.mapView);
mapView.displayZoomControls(true);
mapView.getController().setZoom(15);
}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s