how to add balloon overlay in osm map application - android

How do I add balloon overlay in this my osm code? I'm using osm map and I want to show balloon overlay like google maps I want to implement this in my code which is below:
import org.osmdroid.DefaultResourceProxyImpl;
import org.osmdroid.ResourceProxy;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
public class MainActivity extends Activity {
MyItemizedOverlay myItemizedOverlay = null;
private MapController myMapController;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
myMapController = mapView.getController();
myMapController.setZoom(16);
Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
int markerWidth = marker.getIntrinsicWidth();
int markerHeight = marker.getIntrinsicHeight();
marker.setBounds(0, markerHeight, markerWidth, 0);
ResourceProxy resourceProxy = new
DefaultResourceProxyImpl(getApplicationContext());
myItemizedOverlay = new MyItemizedOverlay(marker, resourceProxy);
mapView.getOverlays().add(myItemizedOverlay);
GeoPoint myPoint1 = new GeoPoint(24.893379000000000000, 67.028060900000010000);
myItemizedOverlay.addItem(myPoint1, "myPoint1", "myPoint1");
GeoPoint myPoint2 = new GeoPoint(24.824796300000000000, 67.031565699999990000);
myItemizedOverlay.addItem(myPoint2, "myPoint2", "myPoint2");
}
}
import java.util.ArrayList;
import org.osmdroid.ResourceProxy;
import org.osmdroid.api.IMapView;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.overlay.ItemizedOverlay;
import org.osmdroid.views.overlay.OverlayItem;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();
public MyItemizedOverlay(Drawable pDefaultMarker,
ResourceProxy pResourceProxy) {
super(pDefaultMarker, pResourceProxy);
// TODO Auto-generated constructor stub
}
public void addItem(GeoPoint p, String title, String snippet){
OverlayItem newItem = new OverlayItem(title, snippet, p);
overlayItemList.add(newItem);
populate();
}
#Override
public boolean onSnapToItem(int arg0, int arg1, Point arg2, IMapView arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
protected OverlayItem createItem(int arg0) {
// TODO Auto-generated method stub
return overlayItemList.get(arg0);
}
#Override
public int size() {
// TODO Auto-generated method stub
return overlayItemList.size();
}
}

Related

when pin touch then how to open map balloon (annotation) for more information in android

I make small static map application, in which i take three pin(marker) and set static longitude and latitude. Actually, i want to display balloon annotation for location information display in mapview. when i touch the marker(pin) then balloon annotation is open and display the information for current location. My code is following:
**Source Code: MainActivity.java
----------------------------------**
package com.example.mapapp;
import java.util.List;
import com.google.android.maps.*;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends MapActivity {
MapController mpc;
//Button hotels,theaters,bank;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* hotels = (Button)findViewById(R.id.btnhotels);
hotels.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this,Hotels.class);
startActivity(i);
}
});
theaters = (Button)findViewById(R.id.btntheaters);
theaters.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i1 = new Intent(MainActivity.this,Theaters.class);
startActivity(i1);
}
});
bank = (Button)findViewById(R.id.btnbank);
bank.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i2 = new Intent(MainActivity.this,Bank.class);
startActivity(i2);
}
});*/
MapView mp = (MapView)findViewById(R.id.mapview);
mp.setBuiltInZoomControls(true);
mp.setSatellite(true);
mp.setTraffic(true);
mp.setStreetView(true);
mpc = mp.getController();
//for RL WebSolutions
double rl_lat = Double.parseDouble("23.0355018");
double rl_lon = Double.parseDouble("72.5630625");
GeoPoint point = new GeoPoint((int)(rl_lat*1E6), (int)(rl_lon*1E6));
mpc.animateTo(point);
mpc.setZoom(15);
mp.invalidate();
List<Overlay> mapoverlays = mp.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.redpin);
AddItemizedOverlay itemizedOverlay = new AddItemizedOverlay(drawable,this);
OverlayItem overlayitem = new OverlayItem(point, "Rl Websolution", "Here is RL Websolutions");
itemizedOverlay.addOverlay(overlayitem);
mapoverlays.add(itemizedOverlay);
//for State Bank of India
double stb_lat = Double.parseDouble("23.066519");
double stb_lon = Double.parseDouble("72.56956000000002");
GeoPoint point1 = new GeoPoint((int)(stb_lat*1E6), (int)(stb_lon*1E6));
mpc.animateTo(point1);
mpc.setZoom(15);
mp.invalidate();
List<Overlay> mpoverlays = mp.getOverlays();
Drawable d = this.getResources().getDrawable(R.drawable.poin1);
AddItemizedOverlay itemizeOverlay = new AddItemizedOverlay(d,this);
OverlayItem over = new OverlayItem(point1, "State Bank Of India", "Here is State Bank");
itemizeOverlay.addOverlay(over);
mpoverlays.add(itemizeOverlay);
//for Navrangpura Bus Stop
double bus_lat = Double.parseDouble("23.035626");
double bus_lon = Double.parseDouble("72.5641536");
GeoPoint point2 = new GeoPoint((int)(bus_lat*1E6), (int)(bus_lon*1E6));
mpc.animateTo(point2);
mpc.setZoom(15);
mp.invalidate();
List<Overlay> mboverlays = mp.getOverlays();
Drawable d1 = this.getResources().getDrawable(R.drawable.bus);
AddItemizedOverlay itemizOverlay = new AddItemizedOverlay(d1,this);
OverlayItem over1 = new OverlayItem(point2, "Navrangpura Bus Stop", "Here is Bus Stop");
itemizOverlay.addOverlay(over1);
mboverlays.add(itemizOverlay);
}
protected boolean isRouteDisplayed() {
return true;
}
}
****Here AddItemizedOverlay.java**
-----------------------------------**
package com.example.mapapp;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.Log;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public AddItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public AddItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mapOverlays.get(i);
}
#Override
public int size() {
// TODO Auto-generated method stub
return mapOverlays.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
this.populate();
}
}
I think this is the perfect example you are looking for:
http://wptrafficanalyzer.in/blog/adding-marker-on-touched-location-of-google-maps-using-android-api-v2-with-supportmapfragment/

How get direction between two point on google map in android?

I searched for two hours but only found draw Line, i want get direction as this form Click here! I have ten point and when i click the point i want to show direction from my location to the point.
My code look:
package de.vogella.android.locationapi.maps;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;
public class ShowMapActivity extends MapActivity {
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private MyOverlays itemizedoverlay,itemizedoverlay_central;
private MyLocationOverlay myLocationOverlay;
private String nameOfOffice_AZ[];
private String addressOfOffice_AZ[];
private String phoneOfOffice_AZ[];
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main); // bind the layout to the activity
// Configure the Map
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
Projection projection = mapView.getProjection();
nameOfOffice_AZ= getResources().getStringArray(R.array.NamesOFObject_AZ);
addressOfOffice_AZ = getResources().getStringArray(R.array.AddressOfOffice_AZ);
phoneOfOffice_AZ = getResources().getStringArray(R.array.PhoneOfOffice_AZ);
mapController = mapView.getController();
mapController.setZoom(16); // Zoon 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new GeoUpdateHandler());
myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mapView.getController().animateTo(myLocationOverlay.getMyLocation());
}
});
Drawable drawable = this.getResources().getDrawable(R.drawable.office_icon);
Drawable drawable_central = this.getResources().getDrawable(R.drawable.central_office);
itemizedoverlay = new MyOverlays(this, drawable);
itemizedoverlay_central = new MyOverlays(this, drawable_central);
createMarker();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
public class GeoUpdateHandler implements LocationListener {
#Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
createMarker();
mapController.animateTo(point); // mapController.setCenter(point);
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
private void createMarker() {
//CENTRAL OFFICE
GeoPoint p_central = new GeoPoint((int)(40.38292884084829 * 1e6),(int)(49.84223246574 * 1e6));
OverlayItem overlayitem = new OverlayItem(p_central, nameOfOffice_AZ[0] ,addressOfOffice_AZ[0]+"\n"+phoneOfOffice_AZ[0] );
itemizedoverlay_central.addOverlay(overlayitem);
mapView.getOverlays().add(itemizedoverlay_central);
//END CENTRALL OFFICE
GeoPoint p = new GeoPoint((int)(40.376455916943236 * 1e6),(int)(49.84803676605224 * 1e6));
overlayitem = new OverlayItem(p, nameOfOffice_AZ[0] ,addressOfOffice_AZ[0]+"\n"+phoneOfOffice_AZ[0] );
itemizedoverlay.addOverlay(overlayitem);
mapView.getOverlays().add(itemizedoverlay);
}
#Override
protected void onResume() {
super.onResume();
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
}
#Override
protected void onPause() {
super.onPause();
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
}
}
and MyOverlays.java
package de.vogella.android.locationapi.maps;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class MyOverlays extends ItemizedOverlay<OverlayItem> {
private static int maxNum = 5;
private OverlayItem overlays[] = new OverlayItem[maxNum];
private int index = 0;
private boolean full = false;
private Context mContext;
private OverlayItem previousoverlay;
public MyOverlays(Context context, Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
this.mContext = context;
}
private ArrayList< OverlayItem > mOverlays = new ArrayList< OverlayItem >();
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void name() {
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
#Override
protected boolean onTap(int i) {
//when you tap on the marker this will show the informations provided by you when you create in the
//main class the OverlayItem
OverlayItem item = mOverlays.get(i);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setInverseBackgroundForced(true);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.setCancelable(true);
dialog.setPositiveButton("Go To", new OkOnClickListener());
/// dialog.setNegativeButton("No, no", new CancelOnClickListener());
dialog.show();
// nese bax = new nese();
// bax.oldu(mContext);
return true;
}
private final class CancelOnClickListener implements
DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, "You clicked yes", Toast.LENGTH_LONG)
.show();
}
}
private final class OkOnClickListener implements
DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(mContext, "You clicked no", Toast.LENGTH_LONG).show();
}
}
public class RoutePath extends MapActivity {
/** Called when the activity is first created. */
MapView mapView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
double src_lat = 25.04202; // the testing source
double src_long = 121.534761;
double dest_lat = 25.05202; // the testing destination
double dest_long = 121.554761;
GeoPoint srcGeoPoint = new GeoPoint((int) (src_lat * 1E6),
(int) (src_long * 1E6));
GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6),
(int) (dest_long * 1E6));
DrawPath(srcGeoPoint, destGeoPoint, Color.GREEN, mapView);
mapView.getController().animateTo(srcGeoPoint);
mapView.getController().setZoom(15);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private void DrawPath(GeoPoint src, GeoPoint dest, int color,
MapView mMapView01) {
// code in section 2.2
}
}
}
Less efforts on search..
Anyway look at Google Driving Directions - MapView overlayed Also The Google Directions API
and using intent
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=xx.xx,xx.xx&daddr=xx.xx,xx.xx"));
startActivity(intent);
This blogpost can be a good startpoint: Android driving direction Route-Path
Worked fine for me.

How to add a marker when I touch the map in android ?

I have a map activity which displays the map, I want to add a marker when I touch on the screen ..
This is my Activity ...
package com.adhamenaya.android;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class MapApp extends MapActivity {
private MapView mapView;
private MapController mapController;
private LocationManager locationManager;
private Context context;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context=this;
initLayout();
initMap();
}
private void initLayout(){
mapView = (MapView) findViewById(R.id.mapview);
//blueIcon = (Drawable)this.getResources().getDrawable(R.drawable.blueicon);
}
private void initMap(){
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
mapController=mapView.getController();
mapController.setZoom(10);// 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, new GeoUpdateHandler());
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
class GeoUpdateHandler implements LocationListener {
#Override
public void onLocationChanged(Location location) {
int lat=(int)(location.getLatitude()*1E6);
int lng=(int)(location.getLongitude()*1E6);
GeoPoint point=new GeoPoint(lat,lng);
//GeoPoint point = new GeoPoint(19240000,-99120000);
mapController.animateTo(point);
mapController.setCenter(point);
Drawable redIcon = context.getResources().getDrawable(R.drawable.redicon);
List<Overlay> mapOverlays = mapView.getOverlays();
MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(redIcon);
OverlayItem overlayitem = new OverlayItem(point, "Hello !", "I'm here");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
#Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
public boolean onTouchEvent(MotionEvent event){
int x=(int)event.getX();
int y=(int)event.getY();
Toast.makeText(context, x, Toast.LENGTH_LONG).show();
GeoPoint point = mapView.getProjection().fromPixels(x, y);
Drawable redIcon = context.getResources().getDrawable(R.drawable.blueicon);
List<Overlay> mapOverlays = mapView.getOverlays();
MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(redIcon);
OverlayItem overlayitem = new OverlayItem(point, "New Place", "I clicked here !");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
return false;
}
}
}
Edit : This is the class for ItemizedOverlay, where I have implemented onTap() method , but nothing happens ...
package com.adhamenaya.android;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.widget.Toast;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class MyItemizedOverlay extends ItemizedOverlay{
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public MyItemizedOverlay(Drawable defaultMarker, Context context) {
super(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;
}
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
int x=(int)event.getX();
int y=(int)event.getY();
Toast.makeText(mContext, x, Toast.LENGTH_LONG).show();
return super.onTouchEvent(event, mapView);
}
}
Hello, MapView! tutorial covers showing markers on MapView. In the tutorial markers are added as soon as the activity starts, but you can also add markers at later time, for example, when user touches screen. If you try this approach, you'll likely want to override onTap in your subclass of ItemizedOverlay.
Update:
If you follow the tutorial, you'll have your own subclass of ItemizedOverlay. onTap is one of its methods. Now that I look at the documentation, unfortunately onTap(GeoPoint p, MapView mapView) is not public or protected so you cannot override it.
What you can do, is have another overlay, solely for detecting taps. Instead of subclassing ItemizedOverlay, subclass Overlay.
private class TapOverlay extends Overlay {
public boolean onTap(GeoPoint p, MapView mapView) {
// code that adds item in your ItemizedOverlay
// goes here
return true;
}
}

Android MapView

Please help me out, I am working on a Project Xyz having the mapView with add multiple overlays and remove overlays as per userChoice.
To be more clear it filters the overlays as per user choice,,,,
Suppose showing favorite places, friends of a user...
PostLoginServiceHomeBean is my ArrayList with an ArrayList NearByFavoritePlacesList object
entry.getNearByFavoritePlacesList().get(i).getGpsLatitude()
.trim()
i.e by this i'm getting the multiple lat long form server...
The code runs f9 and add the multiple overlays to my map and shows name and address of repective place on overlay tap.But how could I remove the overlays form my map...
I am enclosing the code:Please find the code below:
Please help me out....
package com.gogozing.app;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import com.gogozing.service.LocateServices;
import com.gogozing.util.Data;
import com.gogozing.util.PostLoginServiceHomeBean;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class HomeMapActivity extends MapActivity {
private MapView myMapView;
private MapController myMapController;
private List<Overlay> mapOverlays;
private HomeMapItemizedOverlay itemizedOverlay;
private Context context;
private PostLoginServiceHomeBean entry;
private GeoPoint initGeoPoint;
private CheckBox peopleCheckBox, friendsCheckBox, favoritesCheckBox;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_map_layout);
context = this;
backButton = (Button) findViewById(R.id.HomeMapLeftBackButton);
eventsBtn = (Button) findViewById(R.id.EventsBtn);
profileBtn = (Button) findViewById(R.id.ProflieBtn);
homeBtn = (Button) findViewById(R.id.HomeBtn);
locateButton = (Button) findViewById(R.id.LocateBtn);
socializeButton = (Button) findViewById(R.id.SocializeBtn);
peopleCheckBox = (CheckBox) findViewById(R.id.HomeMapViewPeopleCheckBox);
favoritesCheckBox = (CheckBox) findViewById(R.id.HomeMapViewFavoritesCheckBox);
friendsCheckBox = (CheckBox) findViewById(R.id.HomeMapViewFriendsCheckBox);
homeBtn.setBackgroundResource(R.drawable.home_f);
myMapView = (MapView) findViewById(R.id.mapviewHome);
myMapView.setBuiltInZoomControls(true);
// Set satellite view
myMapController = myMapView.getController();
// Get the current location in start-up
try {
entry = Data.postLoginServiceHomeMain.get(0);
favoritesCheckBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
viewFavoritePlaces();
} else
removeFavoritePlaces();
}
});
} catch (Exception e) {
Log.e(Data.LOG, e.getMessage(), e);
}
}
void viewFavoritePlaces() {
for (int i = 0; i < entry.getNearByFavoritePlacesList().size(); i++) {
double latitude = Double.valueOf(
**entry.getNearByFavoritePlacesList().get(i).getGpsLatitude()
.trim()**).doubleValue();
double longitude = Double.valueOf(
entry.getNearByFavoritePlacesList().get(i)
.getGpsLongitude().trim()).doubleValue();
Log.v(Data.LOG1, " Latitude " + latitude + " Longitude "
+ longitude);
String name = entry.getNearByFavoritePlacesList().get(i)
.getMerchantName();
String address = entry.getNearByFavoritePlacesList().get(i)
.getAddress();
initGeoPoint = new GeoPoint((int) (latitude * 1E6),
(int) (longitude * 1E6));
mapOverlays = myMapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(
R.drawable.map_store_place);
itemizedOverlay = new HomeMapItemizedOverlay(drawable, this);
OverlayItem overlayItem = new OverlayItem(initGeoPoint, name,
address);
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);
myMapController.animateTo(initGeoPoint);
myMapController.setZoom(15);
}
}
void removeFavoritePlaces() {
//List<Overlay> mapOverlays = myMapView.getOverlays();
mapOverlays.remove(itemizedOverlay);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
};
public class HomeMapItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public HomeMapItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void removeOverlay(OverlayItem overlay)
{
mOverlays.remove(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;
}
}
}
Here I am giving one solution what I am getting from your question... You need to add Lat Long into Overlay item which is coming from server and add it to itemized overlay, after that when ever you would like to remove any Itemizedoverlay with specific lat long you can check and remove from map overlay as well.

how to show multiple overlay on map in Android?

I have item list with latitude and longitude .I want to show the item list on Google map..
so, how to create overlay list and how to show overlays on Google map ?
This is what i did on my project...
package org.nip.gmap;
import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
public class Main extends MapActivity {
/** Called when the activity is first created. */
MapView map;
MapController controller;
List<Overlay> overlayList;
int lat=0;
int lng=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map = (MapView) findViewById(R.id.mapView);
map.setBuiltInZoomControls(true);
overlayList = map.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.pushpin2);
CustomPinpoint itemizedoverlay = new CustomPinpoint(drawable,this);
double lat_coordinates[] ={27.700556,28.2642635,30.0018168,29.776669,29.4096819,29.4560611};
double lng_coordinates[] ={85.3630,83.9735195,80.7382742,81.2518833,81.8115051,80.5403779};
String place_name[] ={"kathmandu","Pokhara","Darchula","Bajhang","Bajura","Baitadi"};
String place_info[] ={"Its an capital of Nepal","Its and tourist place of Nepal","Its one of the beautiful place in country side","CHD District Target:10 51,960, VDCs/Muncipalities reported:41/41","CHD District Target: 71,280, VDCs/Muncipalities reported: 47/47","CHD District Target:10 51,960, VDCs/Muncipalities reported:41/41","CHD District Target: 71,280, VDCs/Muncipalities reported: 7/7","CHD District Target:10 21,960, VDCs/Muncipalities reported:44/41","CHD District Target: 33,3123, VDCs/Muncipalities reported: 47/47"};
try{
for(int i=0; i<place_name.length; i++)
{
GeoPoint point = new GeoPoint((int)(lat_coordinates[i]*1E6),(int)(lng_coordinates[i]*1E6));
OverlayItem overlayitem = new OverlayItem(point, place_name[i], place_info[i]);
itemizedoverlay.addOverlay(overlayitem);
}
}catch(NullPointerException e){
e.getStackTrace();
}
finally{
overlayList.add(itemizedoverlay);
}
controller = map.getController();
controller.animateTo(new GeoPoint((int)(lat_coordinates[0]*1E6),(int)(lng_coordinates[0]*1E6)));
controller.setZoom(8);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
Create new class...
package org.nip.gmap;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
//import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class CustomPinpoint extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();
private Context c;
public CustomPinpoint(Drawable defaultMarker, Context context) {
super(boundCenter(defaultMarker));
c= context;
}
// public CustomPinpoint(Drawable M, Context context) {
//
// this(M);
// c= context;
// }
public void addOverlay (OverlayItem overlay)
{
pinpoints.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return pinpoints.get(i);
}
#Override
public int size() {
// TODO Auto-generated method stub
return pinpoints.size();
}
#Override
protected boolean onTap(int index) {
// TODO Auto-generated method stub
OverlayItem item = pinpoints.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(c);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
OnClickListener for pushpin

Categories

Resources