Can't get the selected overlay values individually - android

I am working with a Google map oriented android project now i got a problem in this class i class i used certain overlay to mark nearest places when we touch on particular overlay it will show an alert-box contains name and reference now i am retrieve the reference using the getsnipet() method now i don't want to display the alert-dialog box but when i disable that thing i cannot get the reference can anyone help me to fix this error and here is my code...
public class PlacesMapActivity extends MapActivity {
// Nearest places
PlacesList nearPlaces;
// Map view
MapView mapView;
// Map overlay items
List<Overlay> mapOverlays;
AddItemizedOverlay itemizedOverlay;
GeoPoint geoPoint;
// Map controllers
MapController mc;
double latitude;
double longitude;
OverlayItem overlayitem;
String p_u_name;
Place reference;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_places);
// Getting intent data
Intent i = getIntent();
p_u_name=i.getStringExtra("p_u_name");
reference = (Place) i.getSerializableExtra("place_reference");
i.putExtra("p_u_name",p_u_name);
// Users current geo location
String user_latitude = i.getStringExtra("user_latitude");
String user_longitude = i.getStringExtra("user_longitude");
System.out.println("sarath"+user_latitude + user_longitude);
// Nearplaces list
nearPlaces = (PlacesList) i.getSerializableExtra("near_places");
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
// Geopoint to place on map
geoPoint = new GeoPoint((int) (Double.parseDouble(user_latitude) * 1E6),
(int) (Double.parseDouble(user_longitude) * 1E6));
// Drawable marker icon
Drawable drawable_user = this.getResources()
.getDrawable(R.drawable.mark_red);
itemizedOverlay = new AddItemizedOverlay(drawable_user, this);
// Map overlay item
overlayitem = new OverlayItem(geoPoint, "Your Location",
"That is you!");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
itemizedOverlay.populateNow();
// Drawable marker icon
Drawable drawable = this.getResources()
.getDrawable(R.drawable.mark_blue);
itemizedOverlay = new AddItemizedOverlay(drawable, this);
mc = mapView.getController();
// These values are used to get map boundary area
// The area where you can see all the markers on screen
int minLat = Integer.MAX_VALUE;
int minLong = Integer.MAX_VALUE;
int maxLat = Integer.MIN_VALUE;
int maxLong = Integer.MIN_VALUE;
// check for null in case it is null
if (nearPlaces.results != null) {
// loop through all the places
for (Place place : nearPlaces.results) {
latitude = place.geometry.location.lat; // latitude
longitude = place.geometry.location.lng; // longitude
// Geopoint to place on map
geoPoint = new GeoPoint((int) (latitude * 1E6),
(int) (longitude * 1E6));
String s = place.formatted_address;
String p = place.formatted_phone_number;
String n = s;
System.out.println("sarath"+s);
// Map overlay item
//Here it passes the selected overlay name and reference to the alerdialog box
overlayitem = new OverlayItem(geoPoint,place.name,place.reference);
i.putExtra("place_reference", place.reference);
// System.out.println("this is mapactivity and reference is:"+ab);
itemizedOverlay.addOverlay(overlayitem);
// calculating map boundary area
minLat = (int) Math.min( geoPoint.getLatitudeE6(), minLat );
minLong = (int) Math.min( geoPoint.getLongitudeE6(), minLong);
maxLat = (int) Math.max( geoPoint.getLatitudeE6(), maxLat );
maxLong = (int) Math.max( geoPoint.getLongitudeE6(), maxLong );
}
mapOverlays.add(itemizedOverlay);
// showing all overlay items
itemizedOverlay.populateNow();
}
// Adjusting the zoom level so that you can see all the markers on map
mapView.getController().zoomToSpan(Math.abs( minLat - maxLat ), Math.abs( minLong - maxLong ));
// Showing the center of the map
mc.animateTo(new GeoPoint((maxLat + minLat)/2, (maxLong + minLong)/2 ));
mapView.postInvalidate();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}

Your issue is pretty unclear... I think by 'reference' you mean 'description', in which case you were correct in using getsnippet(). If you've already seen the String through an AlertDialog, then taking out the dialog should not prevent you from seeing the snippet. Could you give us a bit more on your precise issue?

Related

How to convert code these to google maps api v2?

I followed one of the tutarial about google maps. Unfortunately it is using google maps v1. Now I'm working on an old android application upgrading it from the Google API v1 to v2. How can I do this on Google Maps API v2? Need same help here.
public class PlacesMapActivity extends MapActivity {
PlacesList nearPlaces;
MapView mapView;
List<Overlay> mapOverlays;
AddItemizedOverlay itemizedOverlay;
GeoPoint geoPoint;
MapController mc;
double latitude;
double longitude;
OverlayItem overlayitem;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_places_map);
Intent i = getIntent();
String user_latitude = i.getStringExtra("user_latitude");
String user_longitude = i.getStringExtra("user_longitude");
nearPlaces = (PlacesList) i.getSerializableExtra("near_places");
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
geoPoint = new GeoPoint((int) (Double.parseDouble(user_latitude) * 1E6),
(int) (Double.parseDouble(user_longitude) * 1E6));
Drawable drawable_user = this.getResources()
.getDrawable(R.drawable.mark_red);
itemizedOverlay = new AddItemizedOverlay(drawable_user, this);
overlayitem = new OverlayItem(geoPoint, "Your Location",
"That is you!");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
itemizedOverlay.populateNow();
Drawable drawable = this.getResources()
.getDrawable(R.drawable.mark_blue);
itemizedOverlay = new AddItemizedOverlay(drawable, this);
mc = mapView.getController();
int minLat = Integer.MAX_VALUE;
int minLong = Integer.MAX_VALUE;
int maxLat = Integer.MIN_VALUE;
int maxLong = Integer.MIN_VALUE;
if (nearPlaces.results != null) {
// loop through all the places
for (Place place : nearPlaces.results) {
latitude = place.geometry.location.lat; // latitude
longitude = place.geometry.location.lng; // longitude
geoPoint = new GeoPoint((int) (latitude * 1E6),
(int) (longitude * 1E6));
overlayitem = new OverlayItem(geoPoint, place.name,
place.vicinity);
itemizedOverlay.addOverlay(overlayitem);
// calculating map boundary area
minLat = (int) Math.min( geoPoint.getLatitudeE6(), minLat );
minLong = (int) Math.min( geoPoint.getLongitudeE6(), minLong);
maxLat = (int) Math.max( geoPoint.getLatitudeE6(), maxLat );
maxLong = (int) Math.max( geoPoint.getLongitudeE6(), maxLong );
}
mapOverlays.add(itemizedOverlay);
// showing all overlay items
itemizedOverlay.populateNow();
}
mapView.getController().zoomToSpan(Math.abs( minLat - maxLat ), Math.abs( minLong - maxLong ));
mc.animateTo(new GeoPoint((maxLat + minLat)/2, (maxLong + minLong)/2 ));
mapView.postInvalidate();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
You may want to check out this post - Android Maps API v1 to v2. It seems to have the same question you have.

How to show Single Place on Map

I am using this tutorial to build Location Based App:
http://www.androidhive.info/2012/08/android-working-with-google-places-and-maps-tutorial/
still i have a button by using that button i am able to view all the nearest places on map.
Here i need one small change, i also want to show single place on map, not all, only selected one....
Therefore, in SinglePlace.xml, i have given an image button to view location for that particular place....
Still i am using below java class to get all, please tell me what are the changes i need to do in my code to get only single place [which place i have selected]
public class PlacesMapActivity extends MapActivity {
// Nearest places
PlacesList nearPlaces;
// Map view
MapView mapView;
// Map overlay items
List<Overlay> mapOverlays;
AddItemizedOverlay itemizedOverlay;
GeoPoint geoPoint;
// Map controllers
MapController mc;
double latitude;
double longitude;
OverlayItem overlayitem;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_places);
// Getting intent data
Intent i = getIntent();
// Users current geo location
String user_latitude = i.getStringExtra("user_latitude");
String user_longitude = i.getStringExtra("user_longitude");
// Nearplaces list
nearPlaces = (PlacesList) i.getSerializableExtra("near_places");
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
// Geopoint to place on map
geoPoint = new GeoPoint((int) (Double.parseDouble(user_latitude) * 1E6),
(int) (Double.parseDouble(user_longitude) * 1E6));
// Drawable marker icon
Drawable drawable_user = this.getResources()
.getDrawable(R.drawable.mark_red);
itemizedOverlay = new AddItemizedOverlay(drawable_user, this);
// Map overlay item
overlayitem = new OverlayItem(geoPoint, "Your Location",
"That is you!");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
itemizedOverlay.populateNow();
// Drawable marker icon
Drawable drawable = this.getResources()
.getDrawable(R.drawable.mark_blue);
itemizedOverlay = new AddItemizedOverlay(drawable, this);
mc = mapView.getController();
// These values are used to get map boundary area
// The area where you can see all the markers on screen
int minLat = Integer.MAX_VALUE;
int minLong = Integer.MAX_VALUE;
int maxLat = Integer.MIN_VALUE;
int maxLong = Integer.MIN_VALUE;
// check for null in case it is null
if (nearPlaces.results != null) {
// loop through all the places
for (Place place : nearPlaces.results) {
latitude = place.geometry.location.lat; // latitude
longitude = place.geometry.location.lng; // longitude
// Geopoint to place on map
geoPoint = new GeoPoint((int) (latitude * 1E6),
(int) (longitude * 1E6));
// Map overlay item
overlayitem = new OverlayItem(geoPoint, place.name,
place.vicinity);
itemizedOverlay.addOverlay(overlayitem);
// calculating map boundary area
minLat = (int) Math.min( geoPoint.getLatitudeE6(), minLat );
minLong = (int) Math.min( geoPoint.getLongitudeE6(), minLong);
maxLat = (int) Math.max( geoPoint.getLatitudeE6(), maxLat );
maxLong = (int) Math.max( geoPoint.getLongitudeE6(), maxLong );
}
mapOverlays.add(itemizedOverlay);
// showing all overlay items
itemizedOverlay.populateNow();
}
// Adjusting the zoom level so that you can see all the markers on map
mapView.getController().zoomToSpan(Math.abs( minLat - maxLat ), Math.abs( minLong - maxLong ));
// Showing the center of the map
mc.animateTo(new GeoPoint((maxLat + minLat)/2, (maxLong + minLong)/2 ));
mapView.postInvalidate();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}

How show multiple points on Google map?

When start Google Maps,I focus to a point on map as:
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.BLUE, mapView);
mapView.getController().animateTo(destGeoPoint);
If I use mapView.getController().animateTo(destGeoPoint); the map only focuses to point destGeoPoint.
I want Map to show 2 points: srcGeoPoint and destGeoPoint when start Google Map.
This 2 point had draw on Map. I want to can see 2 point when map open.
Create your mapOverlay:
public class MapOverlay extends Overlay {
MapView mapView;
GeoPoint p;
private Context c;
public MapOverlay(Context c, MapView m, GeoPoint p){
this.c=c;
mapView=m;
this.p=p;
}
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(c.getResources(), R.drawable.icon_notif);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
This is a method that starts the map:
private void initMapa() {
try{
//If you want zoom controls
mapa.setBuiltInZoomControls(true);
GeoPoint loc;
Double lat;
Double long;
MapController controlMapa = mapa.getController();
controlMapa.setZoom(13); // de 1 a 21
mapa.setSatellite(true);
mapa.displayZoomControls(true);
// First point
ArrayList<GeoPoint> ap=new ArrayList<GeoPoint>();
latitud = (double) lat1 *1E6;
longitud = (double) long1 *1E6;
loc = new GeoPoint(latitud.intValue(), longitud.intValue());
controlMapa.animateTo(loc);
ap.add(loc);
// Second point
latitud = (double) fichaOtraPersona.getLoc_lan()*1E6;
longitud = (double) fichaOtraPersona.getLoc_lon()*1E6;
loc = new GeoPoint(latitud.intValue(), longitud.intValue());
ap.add(loc);
controlMapa.animateTo(loc);
addPointInTheMap(ap);
} catch (Exception e) {
showToast("Error");
}
}
And the addPointInTheMap method:
private void anadirPuntoEnMapa(ArrayList<GeoPoint> points){
//---Add a location marker---
List<Overlay> listOfOverlays = mapa.getOverlays();
listOfOverlays.clear();
Iterator<GeoPoint> it=points.iterator();
while (it.hasNext()){
MapOverlay mapOverlay = new MapOverlay(getApplicationContext(), mapa, it.next());
listOfOverlays.add(mapOverlay);
}
mapa.invalidate();
}
Try to read the Location and Map Applications Chapter from the Android Cookbook. The main idea is to add an inner class to your MapActivity which extends ItemizedOverlay and implement the abstract methods and the default constructor. The ItemizedOverlay uses your implementations of the createItem and size() methods to get hold of all the overlay items in your implementation and do the aggregation.

Cannot send a string to another class

i am working with android program here i have two classes PlaceMapActivity and AddItemizedOverlay i want to send a string from PlaceMapActivity to the AddItemizedOverlay class can any one help me to solve this and here is my two classes
AddItemizedOverlay
public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
String reference;
private Activity activity;
String p_u_name;
String username;
public AddItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public AddItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
if (event.getAction() == 1) {
GeoPoint geopoint = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
// latitude
double lat = geopoint.getLatitudeE6() / 1E6;
// longitude
double lon = geopoint.getLongitudeE6() / 1E6;
//Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();
}
return false;
}
#Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
#Override
public int size() {
return mapOverlays.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
dialog.setTitle(item.getTitle());
dialog.setMessage("Do you want ot park here ?");
reference = item.getSnippet();
dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SharedPreferences prefs = context.getSharedPreferences("myprefs", 0);
SharedPreferences.Editor editor =prefs.edit();
editor.putString("KEY_REFERENCE", reference);
editor.commit();
Intent intent = new Intent(context, SinglePlaceActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
// MainActivity.this.finish();
}
});
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
}
public void populateNow(){
this.populate();
}
}
and the second class is here
PlacesMapActivity.java
public class PlacesMapActivity extends MapActivity {
// Nearest places
PlacesList nearPlaces;
// Map view
MapView mapView;
// Map overlay items
List<Overlay> mapOverlays;
AddItemizedOverlay itemizedOverlay;
GeoPoint geoPoint;
// Map controllers
MapController mc;
double latitude;
double longitude;
OverlayItem overlayitem,wma;
String p_u_name;
Place reference;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_places);
// Getting intent data
Intent i = getIntent();
p_u_name = i.getExtras().getString("KEY_USERNAME");
// AddItemizedOverlay its_obj = new AddItemizedOverlay(null);
// its_obj.getstring(p_u_name);
System.out.println("place map activity :got username"+p_u_name);
reference = (Place) i.getSerializableExtra("place_reference");
// Users current geo location
String user_latitude = i.getStringExtra("user_latitude");
String user_longitude = i.getStringExtra("user_longitude");
System.out.println("sarath"+user_latitude + user_longitude);
// Nearplaces list
nearPlaces = (PlacesList) i.getSerializableExtra("near_places");
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
// Geopoint to place on map
geoPoint = new GeoPoint((int) (Double.parseDouble(user_latitude) * 1E6),(int) (Double.parseDouble(user_longitude) * 1E6));
// Drawable marker icon
Drawable drawable_user = this.getResources().getDrawable(R.drawable.mark_red);
itemizedOverlay = new AddItemizedOverlay(drawable_user, this);
// Map overlay item
overlayitem = new OverlayItem(geoPoint, "Your Location","That is you!");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
itemizedOverlay.populateNow();
// Drawable marker icon
Drawable drawable = this.getResources().getDrawable(R.drawable.mark_blue);
itemizedOverlay = new AddItemizedOverlay(drawable, this);
mc = mapView.getController();
// These values are used to get map boundary area
// The area where you can see all the markers on screen
int minLat = Integer.MAX_VALUE;
int minLong = Integer.MAX_VALUE;
int maxLat = Integer.MIN_VALUE;
int maxLong = Integer.MIN_VALUE;
// check for null in case it is null
if (nearPlaces.results != null) {
// loop through all the places
for (Place place : nearPlaces.results) {
latitude = place.geometry.location.lat; // latitude
longitude = place.geometry.location.lng; // longitude
// Geopoint to place on map
geoPoint = new GeoPoint((int) (latitude * 1E6),
(int) (longitude * 1E6));
// Map overlay item
overlayitem = new OverlayItem(geoPoint,place.name,place.reference);
itemizedOverlay.addOverlay(overlayitem);
// calculating map boundary area
minLat = (int) Math.min( geoPoint.getLatitudeE6(), minLat );
minLong = (int) Math.min( geoPoint.getLongitudeE6(), minLong);
maxLat = (int) Math.max( geoPoint.getLatitudeE6(), maxLat );
maxLong = (int) Math.max( geoPoint.getLongitudeE6(), maxLong );
}
mapOverlays.add(itemizedOverlay);
// showing all overlay items
itemizedOverlay.populateNow();
}
// Adjusting the zoom level so that you can see all the markers on map
mapView.getController().zoomToSpan(Math.abs( minLat - maxLat ), Math.abs( minLong - maxLong ));
// Showing the center of the map
mc.animateTo(new GeoPoint((maxLat + minLat)/2, (maxLong + minLong)/2 ));
mapView.postInvalidate();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
Semd String when you create AddItemizedOverlay class object.
Write this in your activity class
itemizedOverlay = new AddItemizedOverlay(drawable_user, this,StringData);
Write this in your OverLay class
private String StringData;
public AddItemizedOverlay(Drawable defaultMarker, Context context,String StringData) {
this(defaultMarker);
this.context = context;
this.StringData=StringData;
}
Try this way....
Thanks

Android google maps show geo points which are closest to current location

Is there any way If I have like 100 geo points added in my MapActivity and I want to show only these which are like 1000m away from my current location. The other ones should not be visible on the map.
Any suggestions?
Ok, here is the example. You can subclass Overlay class and override draw method. Whenever the Overlay is invalidated, you should check if your points are within 1000m from your location. Off course, you should know your location within your overlay class. The flowing is the example:
public class MyMapOverlay extends Overlay {
Bitmap bmp;
Context context;
public MyMapOverlay()
{
bmp = BitmapFactory.decodeResource(MyApplication.getContext().getResources(), R.drawable.myplace);
}
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
for(int i = 0;i<points.size(); i++)
{
GeoPoint point = points.get(i);
Location locationA = new Location("point " + String.valueOf(i));
locationA.setLatitude(point.getLatitudeE6());
locationA.setLongitude(point.getLongitudeE6());
float distance = myLocation.distanceTo(locationA);
if(distance < 1000.0)
{
mapView.getProjection().toPixels(p, screenPts);
canvas.drawBitmap(bmp, screenPts.x-bmp.getWidth()/2, screenPts.y-bmp.getHeight(), null);
}
}
return true;
}
With some help of the first answer I find this solution :
if(location != null){
Location locA = new Location("gps");
GeoPoint myLoc = new GeoPoint((int)(location.getLatitude() * 1E6), (int)(location.getLongitude() * 1E6));
mapController.animateTo(myLoc);
String sqlbars = "SELECT DISTINCT id, latitude, longitude, address, " +
" (SELECT category FROM bars_categories WHERE bar_id=bars.id) AS category " +
" FROM bars WHERE is_active=1";
Cursor curBars = dbHelper.executeSQLQuery(sqlbars);
if(curBars != null){
if(curBars.getCount() > 0){
for(curBars.move(0);curBars.moveToNext();curBars.isAfterLast()){
double latitude = curBars.getDouble(curBars.getColumnIndex("latitude"));
double longitude = curBars.getDouble(curBars.getColumnIndex("longitude"));
final List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = SofiaLiveNearbyActivity.this.getResources().getDrawable(R.drawable.pin_map_icon);
int markerWidth = drawable.getIntrinsicWidth();
int markerHeight = drawable.getIntrinsicHeight();
drawable.setBounds(0, markerHeight, markerWidth, 0);
final HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, SofiaLiveNearbyActivity.this);
GeoPoint point = new GeoPoint((int)(latitude * 1E6),(int)(longitude *1E6));
OverlayItem overlayitem = new OverlayItem(point, "Type: "+category+"\n----------------\nAddress: "+address, id);
locA.setLatitude(latitude);
locA.setLongitude(longitude);
float distance = location.distanceTo(locA);
if(distance < 1000.0){
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
}
}
curBars.close();
}
and it worked.

Categories

Resources