Maps Place Click save that location to Android Listview? - android

I am displaying the Google maps in the Android program.How can i save the places to Android Listview whenever I Click On One place in the map.
By this I am Getting the search place in the maps
package com.commonsware.android.nooer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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.MyLocationOverlay;
import com.google.android.maps.OverlayItem;
public class SampleMpas extends MapActivity {
MapView mapView;
MapController mapController;
GeoPoint mgeoPoint;
Drawable marker;
MyLocationOverlay mLocationOverlay;
MotionEvent e;
public void changeMap(String area)
{
MapController mc=mapView.getController();
GeoPoint myLocation=null;
double lat = 0;
double lng = 0;
try
{
Geocoder g = new Geocoder(this, Locale.getDefault());
java.util.List<android.location.Address> result=g.getFromLocationName(area, 1);
if(result.size()>0){
Toast.makeText(SampleMpas.this, "country: " + String.valueOf(result.get(0).getCountryName()), Toast.LENGTH_SHORT).show();
lat = result.get(0).getLatitude();
lng = result.get(0).getLongitude();
}
else{
Toast.makeText(SampleMpas.this, "record not found", Toast.LENGTH_SHORT).show();
return;
}
}
catch(IOException io)
{
Toast.makeText(SampleMpas.this, "Connection Error", Toast.LENGTH_SHORT).show();
}
myLocation = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
mc.animateTo(myLocation);
mc.setZoom(15);
mapView.invalidate();
}
#Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.main);
Button btnSearch=(Button) findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText txtSearch=(EditText)findViewById(R.id.txtMapSearch);
String area=txtSearch.getText().toString();
Toast.makeText(SampleMpas.this, "Click-" + String.valueOf(area), Toast.LENGTH_SHORT).show();
SampleMpas.this.changeMap(area);
}
});
mapView = (MapView) findViewById(R.id.map);
mapController = mapView.getController();
mapView.setBuiltInZoomControls(true);
marker = getResources().getDrawable(R.drawable.marker);
marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker
.getIntrinsicHeight());
mapView.getOverlays().add(new MapOverlay(marker));
mLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(mLocationOverlay);
}
#Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch (id) {
case 0:
return new AlertDialog.Builder(this).setTitle("Are You Want save this place As?").setIcon(
R.drawable.icon).setPositiveButton("Favorite",
new OnClickListener() {
public void onClick(DialogInterface dialog , int which) {
// TODO Auto-generated method stub
}
}).setCancelable(true).setNegativeButton("Cancel",
new OnClickListener() {
public void onClick(DialogInterface dialog , int which) {
// TODO Auto-generated method stub
}
}).setCancelable(true).setNeutralButton("Business", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Dismiss",
Toast.LENGTH_SHORT).show();
}
}).create();
default:
break;
}
return null;
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
Point scrPoint;
private GeoPoint getPoint(double lat , double lon) {
return (new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6)));
}
class MapOverlay extends
com.google.android.maps.ItemizedOverlay<OverlayItem> {
List<OverlayItem> ListofGeopoints = new ArrayList<OverlayItem>();
public MapOverlay(Drawable defaultMarker ) {
super(defaultMarker);
double lat = 0;
double lang = 0;
ListofGeopoints.add(new OverlayItem(getPoint(lat, lang),
"IN", "India"));
populate();
}
#Override
protected boolean onTap(int index) {
switch (index) {
case 0:
Toast.makeText(getApplicationContext(), "GeoLocation : 0",
Toast.LENGTH_LONG).show();
showDialog(0);
break;
}
return true;
}
String add = "";
List<Address> add_List = new ArrayList<Address>();
private void getAddress() {
/* add_List = ReverseGeocode
.getFromLocation(35.594227, -105.223618, 2);
*/
}
#Override
protected OverlayItem createItem(int i) {
return (ListofGeopoints.get(i));
}
#Override
public int size() {
return ListofGeopoints.size();
}
}
}
Thanks in Advance.

You can get the geo points of the Touched location by
#Override
public boolean onTap(GeoPoint p , MapView mapView)
Write a class which derives from the Overlay class and override the onTap() method. Then you can add your overlay to the your MapView. A GeoPoint object, which represents the position of you tap, is passed to the onTap() method when you tab somewhere on the map. Save this Geo points in your DB and populate your list view from the database.
Sample Code: Overlay Class:
class MapOverlay extends
com.google.android.maps.ItemizedOverlay<OverlayItem> {
#Override
public boolean onTap(GeoPoint p , MapView mapView) {
// TODO Auto-generated method stub
Log.i("Latitude" , String.valueOf(p.getLatitudeE6()/1E6));
Log.i("Longitude", String.valueOf(p.getLongitudeE6()/1E6));
return super.onTap(p, mapView);
}

Related

Multiple pins on Google Maps Android not working

I have been using a previous question and a Google Tutorial to attempt to add multiple pin images on a Google Map view when the current location of the user changes.
Each pin when tapped displays the Longitude and Latitude of the pin.
However currently my code only displays one pin and does not add another image when the location of the user changes. I am relatively new to Android and can't see why this code wouldn't work?
Map.java:
import java.util.List;
import android.graphics.drawable.Drawable;
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.Overlay;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;
import android.util.Log;
public class Map extends MapActivity {
private MapView map;
private MapController controller;
private double lon, lat;
private Drawable drawable;
private MapOverlay itemizedoverlay;
private List<Overlay> mapOverlays;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maps);
initMapView();
mapOverlays = map.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.pin);
itemizedoverlay = new MapOverlay(drawable, this);
initMyLocation();
}
private void initMapView() {
map = (MapView) findViewById(R.id.map);
controller = map.getController();
map.setSatellite(true);
map.setBuiltInZoomControls(true);
}
private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
overlay.enableMyLocation();
overlay.enableCompass(); // wont work in emulator
overlay.runOnFirstFix(new Runnable() {
public void run() {
// Zoom in to current location - 1 is word view
controller.setZoom(20);
controller.animateTo(overlay.getMyLocation());
lon = overlay.getMyLocation().getLongitudeE6();
lat = overlay.getMyLocation().getLatitudeE6();
setLON(lon);
setLAT(lat);
//-0.959896
//51.742953
Log.w("test-lon", Double.toString(lon/1E6));
Log.w("test-lat", Double.toString(lat/1E6));
GeoPoint point = new GeoPoint((int)(lat),(int)(lon));
mapOverlays.add(itemizedoverlay);
test(point);
}
});
map.getOverlays().add(overlay);
}
public void setLON(double x){ //remove?
lon = x;
}
public void setLAT(double x){ //remove?
lat = x;
}
public void test(GeoPoint px){
OverlayItem overlayitem = new OverlayItem(px,"Test Point" , "Lat: " + Double.toString(lat/1E6) + "\nLon: " + Double.toString(lon/1E6) );
itemizedoverlay.addOverlay(overlayitem);
}
#Override
protected boolean isRouteDisplayed() {
// Required method from MapActivity
return false;
}
}
MapOverlay.java:
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class MapOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mapOverlay = new ArrayList<OverlayItem>();
private Context mContext;
public MapOverlay (Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public MapOverlay(Drawable arg0) {
super(boundCenterBottom(arg0));
}
public void addOverlay(OverlayItem overlay) {
mapOverlay.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return mapOverlay.get(i);
}
#Override
public int size() {
return mapOverlay.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlay.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
u need implements class LocationListener for listen our location changes and update on the map.
class LocationsListeners implements LocationListener {
#Override
public void onLocationChanged(Location location) {
setLAT(location.getLatitude());
setLON(location.getLongitude());
setGeoPoint(lat, lon);
mapOverlays.add(geoPoint);
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
u need method for calc the GeoPoint:
public GeoPoint setGeoPoint(double lat, double lon){
geoPoint = new GeoPoint((int) (lat * 1E6), (int)(lon * 1E6));
return geoPoint;
}
or using Little Fluffy Location - Its library that updates our location and notify u, is broadcast receiver, very simple and fast implementation:
try:
http://code.google.com/p/little-fluffy-location-library/

Getting Arraylist of a class into a ListView (android)

I'm having problems with linking the ArrayList of OverlayItems I have created in my CustomPinpoint class to the Listview. Basically I'm trying to display the markers I added on the map in a small list. But with the code below, I get a NullPointerException error. I understand why(I think its because I create 2 seperate instances of 'custom' which don't refer to each other), but I don't understand how I can solve it...
Thank you in advance.
package com.lars.pinpoint;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
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.Overlay;
import com.google.android.maps.OverlayItem;
import com.lars.pinpoint.R;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;
public class Main extends MapActivity implements OnTabChangeListener{
/** Called when the activity is first created. */
private static final String LIST_TAB_TAG = "List";
private static final String MAP_TAB_TAG = "Map";
MapView map;
ListView listView;
TabHost tabHost;
long start;
long stop;
int x, y;
MyLocationOverlay compass;
MyLocationOverlay MyLoc;
MapController controller;
GeoPoint touchedPoint;
Drawable d;
List<Overlay> overlayList;
CustomPinpoint custom;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
listView = (ListView) findViewById(R.id.list);
listView.setEmptyView((TextView) findViewById(R.id.empty));
d = getResources().getDrawable(R.drawable.ic_launcher);
CustomPinpoint custom = new CustomPinpoint(d,Main.this);
listView.setAdapter(new ArrayAdapter<OverlayItem>(this, android.R.layout.simple_list_item_1, custom.pinpoints));
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
GeoPoint geoPoint = ((OverlayItem) listView.getAdapter().getItem(position)).getPoint();
if(geoPoint != null) {
map.getController().animateTo(geoPoint);
tabHost.setCurrentTab(1);
}
}
});
map = (MapView) findViewById(R.id.mapview);
map.setBuiltInZoomControls(true);
map.postInvalidate();
Touch t = new Touch();
overlayList = map.getOverlays();
overlayList.add(t);
compass = new MyLocationOverlay(Main.this, map);
overlayList.add(compass);
controller = map.getController();
MyLoc = new MyLocationOverlay(Main.this, map);
overlayList.add(MyLoc);
map.postInvalidate();
MyLoc.runOnFirstFix(new Runnable() {
public void run() {
map.getController().animateTo(MyLoc.getMyLocation());
}
});
tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG).setIndicator("List").setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return listView;
}
}));
tabHost.addTab(tabHost.newTabSpec(MAP_TAB_TAG).setIndicator("Map").setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return map;
}
}));
//HACK to get the list view to show up first,
// otherwise the mapview would be bleeding through and visible
tabHost.setCurrentTab(1);
tabHost.setCurrentTab(0);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
compass.disableCompass();
super.onPause();
MyLoc.disableMyLocation();
finish();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
compass.enableCompass();
super.onResume();
MyLoc.enableMyLocation();
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
class Touch extends Overlay {
public boolean onTouchEvent(MotionEvent e, MapView m) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
start = e.getEventTime();
x = (int) e.getX();
y = (int) e.getY();
touchedPoint = map.getProjection().fromPixels(x, y);
}
if (e.getAction() == MotionEvent.ACTION_UP) {
stop = e.getEventTime();
}
if (stop - start > 1500) {
AlertDialog alert = new AlertDialog.Builder(Main.this).create();
alert.setTitle("Pick an option.");
alert.setButton(DialogInterface.BUTTON_POSITIVE,"Place a pinpoint.",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
OverlayItem overlayItem = new OverlayItem(touchedPoint, "Pinpoint", "2nd String");
custom.insertPinpoint(overlayItem);
overlayList.add(custom);
}
});
alert.setButton(DialogInterface.BUTTON_NEUTRAL,"Get address.",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault());
try{
List<Address> address = geocoder.getFromLocation(touchedPoint.getLatitudeE6() /1E6, touchedPoint.getLongitudeE6()/1E6, 1);
if (address.size() > 0){
String display = "";
for (int i = 0; i < address.get(0).getMaxAddressLineIndex(); i++){
display += address.get(0).getAddressLine(i) + "\n";
}
Toast t3 = Toast.makeText(getBaseContext(), display, Toast.LENGTH_LONG);
t3.show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
}
});
alert.setButton(DialogInterface.BUTTON_NEGATIVE,"Toggle View", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if (map.isSatellite()){
map.setSatellite(false);
}else{
map.setSatellite(true);
}
}
});
alert.show();
return true;
}
return false;
}
}
public void gpsCurrentLocation()
{
tabHost.setCurrentTab(1);
GeoPoint p = MyLoc.getMyLocation();
map.getController().animateTo(p);
}
// Menu XML file (menu.xml)
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.activity_main, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.my_location:
Toast.makeText(Main.this, "Moving To Current location", Toast.LENGTH_SHORT).show();
gpsCurrentLocation();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
}
}
Use custom = new CustomPinpoint(d,Main.this);
instead of
CustomPinpoint custom = new CustomPinpoint(d,Main.this);

Display alert dialog box based on Maps place Click?

I am practicing the Android maps now i am getting the maps successfully in my emulator when i search in the Google market i found one Interesting App i am try for to develop the example app is
https://play.google.com/store/apps/details?id=streetdirectory.mobile&feature=search_result#?t=W251bGwsMSwxLDEsInN0cmVldGRpcmVjdG9yeS5tb2JpbGUiXQ..
package com.example.tutorials;
import java.io.IOException;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Geocoder;
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.MapView.LayoutParams;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class GoogleMap extends MapActivity
{
MapView mapView;
/** Called when the activity is first created. */
#Override
protected boolean isRouteDisplayed()
{
return false;
}
public void changeMap(String area)
{
mapView = (MapView) findViewById(R.id.mapview);
MapController mc=mapView.getController();
GeoPoint myLocation=null;
double lat = 0;
double lng = 0;
try
{
Geocoder g = new Geocoder(this, Locale.getDefault());
java.util.List<android.location.Address> result=g.getFromLocationName(area, 1);
if(result.size()>0){
Toast.makeText(GoogleMap.this, "country: " + String.valueOf(result.get(0).getCountryName()), Toast.LENGTH_SHORT).show();
lat = result.get(0).getLatitude();
lng = result.get(0).getLongitude();
}
else{
Toast.makeText(GoogleMap.this, "record not found", Toast.LENGTH_SHORT).show();
return;
}
}
catch(IOException io)
{
Toast.makeText(GoogleMap.this, "Connection Error", Toast.LENGTH_SHORT).show();
}
myLocation = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
mc.animateTo(myLocation);
mc.setZoom(10);
mapView.invalidate();
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnSearch=(Button) findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText txtSearch=(EditText)findViewById(R.id.txtMapSearch);
String area=txtSearch.getText().toString();
Toast.makeText(GoogleMap.this, "Click-" + String.valueOf(area), Toast.LENGTH_SHORT).show();
GoogleMap.this.changeMap(area);
}
});
mapView = (MapView) findViewById(R.id.mapview);
MapController mapController = mapView.getController();
mapController.setZoom(14);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
}
}
by this code i am getting search place in Android maps.how can i provide dialog box .when we click on the maps place?
Thanks in Advance......
public class MapViewer extends MapActivity {
MapView mapView;
MapController mapController;
GeoPoint mgeoPoint;
Drawable marker;
MyLocationOverlay mLocationOverlay;
MotionEvent e;
#Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
mapController = mapView.getController();
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
marker = getResources().getDrawable(R.drawable.pushpin);
marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker
.getIntrinsicHeight());
mapView.getOverlays().add(new MapOverlay(marker));
mLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(mLocationOverlay);
setViewLocation();
}
#Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch (id) {
case 0:
return new AlertDialog.Builder(this).setTitle("Hello").setIcon(
R.drawable.ic_launcher).setPositiveButton("Yes",
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog , int which) {
// TODO Auto-generated method stub
}
}).setCancelable(true).setNegativeButton("Cancel",
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog , int which) {
// TODO Auto-generated method stub
}
})
.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Dismiss",
Toast.LENGTH_SHORT).show();
}
}).create();
default:
break;
}
return null;
}
private void setViewLocation() {
String[] coordinates = { "22.716221", "75.896816" };
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
mgeoPoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mapController.animateTo(mgeoPoint);
mapController.setZoom(15);
mapView.invalidate();
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
Point scrPoint;
private GeoPoint getPoint(double lat , double lon) {
return (new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6)));
}
class MapOverlay extends
com.google.android.maps.ItemizedOverlay<OverlayItem> {
List<OverlayItem> ListofGeopoints = new ArrayList<OverlayItem>();
public MapOverlay(Drawable defaultMarker ) {
super(defaultMarker);
ListofGeopoints.add(new OverlayItem(getPoint(22.716221, 75.896816),
"IN", "India"));
populate();
}
#Override
protected boolean onTap(int index) {
switch (index) {
case 0:
Toast.makeText(getApplicationContext(), "GeoLocation : 0",
Toast.LENGTH_LONG).show();
showDialog(0);
break;
}
return true;
}
String add = "";
List<Address> add_List = new ArrayList<Address>();
private void getAddress() {
add_List = ReverseGeocode
.getFromLocation(35.594227, -105.223618, 2);
}
#Override
protected OverlayItem createItem(int i) {
return (ListofGeopoints.get(i));
}
#Override
public int size() {
return ListofGeopoints.size();
}
}
}
You can use popup for this .Popup can be displayed any where on screen .just note the c0ordinates of click and display popup on that place.
Below are general example for the same.
Create a layout for your popup
private PopupWindow mPopup;
//Some code here
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Inflate the view from a predefined XML layout
//This is how your popup will look like
View layout = inflater.inflate(R.layout.popup_layout,
(ViewGroup) findViewById(R.id.popup_element));
mPopup = new PopupWindow(layout, "width", "height", true);
//Use below when you want to display at specific x-y coord on screen
mPopup .showAtLocation(layout, Gravity.CENTER, "x-coord", "y-coord");
//You can use some thing like below to get views from your layout
TextView mText = (TextView) layout.findViewById(R.id."id for textview");
mText.setText("Just an example you can set buttons and listviews in similar fashion");
mPopup.dismiss();//When you want to dismiss Popup
You want to have listView so your layout xml will have a listview. use adapter to fill the listview same way as you do normally.

place click dialog box in Android?

I am give the maps search place in EditText and then I am showing the place in the map. What I need is this, I want display a dialog when we click on the place in maps? So that I can move that place to Android ListView.
package com.example.tutorials;
import java.io.IOException;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Geocoder;
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.MapView.LayoutParams;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class GoogleMap extends MapActivity
{
MapView mapView;
/** Called when the activity is first created. */
#Override
protected boolean isRouteDisplayed()
{
return false;
}
public void changeMap(String area)
{
mapView = (MapView) findViewById(R.id.mapview);
MapController mc=mapView.getController();
GeoPoint myLocation=null;
double lat = 0;
double lng = 0;
try
{
Geocoder g = new Geocoder(this, Locale.getDefault());
java.util.List<android.location.Address> result=g.getFromLocationName(area, 1);
if(result.size()>0){
Toast.makeText(GoogleMap.this, "country: " + String.valueOf(result.get(0).getCountryName()), Toast.LENGTH_SHORT).show();
lat = result.get(0).getLatitude();
lng = result.get(0).getLongitude();
}
else{
Toast.makeText(GoogleMap.this, "record not found", Toast.LENGTH_SHORT).show();
return;
}
}
catch(IOException io)
{
Toast.makeText(GoogleMap.this, "Connection Error", Toast.LENGTH_SHORT).show();
}
myLocation = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
mc.animateTo(myLocation);
mc.setZoom(10);
mapView.invalidate();
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnSearch=(Button) findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText txtSearch=(EditText)findViewById(R.id.txtMapSearch);
String area=txtSearch.getText().toString();
Toast.makeText(GoogleMap.this, "Click-" + String.valueOf(area), Toast.LENGTH_SHORT).show();
GoogleMap.this.changeMap(area);
}
});
mapView = (MapView) findViewById(R.id.mapview);
MapController mapController = mapView.getController();
mapController.setZoom(14);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
}
}
Please see once and let me know where can I do changes in the above code.
To show Dialog box follow this code:
public class MapViewer extends MapActivity {
MapView mapView;
MapController mapController;
GeoPoint mgeoPoint;
Drawable marker;
MyLocationOverlay mLocationOverlay;
MotionEvent e;
#Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
mapController = mapView.getController();
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
marker = getResources().getDrawable(R.drawable.pushpin);
marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker
.getIntrinsicHeight());
mapView.getOverlays().add(new MapOverlay(marker));
mLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(mLocationOverlay);
setViewLocation();
}
#Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch (id) {
case 0:
return new AlertDialog.Builder(this).setTitle("Hello").setIcon(
R.drawable.ic_launcher).setPositiveButton("Yes",
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog , int which) {
// TODO Auto-generated method stub
}
}).setCancelable(true).setNegativeButton("Cancel",
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog , int which) {
// TODO Auto-generated method stub
}
})
.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Dismiss",
Toast.LENGTH_SHORT).show();
}
}).create();
default:
break;
}
return null;
}
private void setViewLocation() {
String[] coordinates = { "22.716221", "75.896816" };
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
mgeoPoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mapController.animateTo(mgeoPoint);
mapController.setZoom(15);
mapView.invalidate();
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
Point scrPoint;
private GeoPoint getPoint(double lat , double lon) {
return (new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6)));
}
class MapOverlay extends
com.google.android.maps.ItemizedOverlay<OverlayItem> {
List<OverlayItem> ListofGeopoints = new ArrayList<OverlayItem>();
public MapOverlay(Drawable defaultMarker ) {
super(defaultMarker);
ListofGeopoints.add(new OverlayItem(getPoint(22.716221, 75.896816),
"IN", "India"));
populate();
}
#Override
protected boolean onTap(int index) {
switch (index) {
case 0:
Toast.makeText(getApplicationContext(), "GeoLocation : 0",
Toast.LENGTH_LONG).show();
showDialog(0);
break;
}
return true;
}
String add = "";
List<Address> add_List = new ArrayList<Address>();
private void getAddress() {
add_List = ReverseGeocode
.getFromLocation(35.594227, -105.223618, 2);
}
#Override
protected OverlayItem createItem(int i) {
return (ListofGeopoints.get(i));
}
#Override
public int size() {
return ListofGeopoints.size();
}
}
}

latitude and longitude change in android and map not shown

latitude and longitude change in android and map not shown
package net.learn2develop.GoogleMaps;
import java.io.IOException;
import java.util.*;
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.MapView.LayoutParams;
import android.app.LocalActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MapsActivity extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;
double latPoint, lngPoint;
LocationManager myManager;
class MapOverlay extends com.google.android.maps.Overlay
{
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
p.getLatitudeE6() / 1E6,
p.getLongitudeE6() / 1E6, 1);
String add = "";
if (addresses.size() > 0)
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();
i++)
add += addresses.get(0).getAddressLine(i) + "\n";
}
Log.e("address", add);
Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
e.printStackTrace();
}
return true;
}
else
return false;
}
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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();
LocationManager myManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,new myLocationListener());
mc.setZoom(10);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
class myLocationListener implements LocationListener {
public void ListLocationUpdater() {
}
#Override
public void onLocationChanged(Location loc) {
if (myManager != null) {
// List list = myManager.getAllProviders();
String param = (String) myManager.getProviders(true).get(0);
loc = myManager.getLastKnownLocation(param);
if (loc != null) {
latPoint = loc.getLatitude();
lngPoint = loc.getLongitude();
p = new GeoPoint((int) (latPoint * 1E6), (int) (lngPoint * 1E6));
mc.animateTo(p); Log.e("RootDrawApplication",String.valueOf(latPoint)+" , "+String.valueOf(lngPoint));
} else
Log.e("GoogleMaps ", "Error: Location is null");
} else
Log.e("GoogleMaps ", "Error: Location Manager is null");
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
Create a backgroud thread and check the gps data there.
And use a LocationListener:
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location l) {
TextView tv = new TextView(gpstracker.this);
tv.setText("lat: " + l.getLatitude() + "\nlon: " + l.getLongitude());
setContentView(tv);
}
...
This can also help...
Add a location listener to your location manager with this code:
myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new myLocationListener());
This way, your myLocationListener will implement the locationListener interface and change Location in onLocationChangedFunction.
public class myLocationListener implements LocationListener {
public ListLocationUpdater() {
}
#Override
public void onLocationChanged(Location location) {
//assign location here
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}

Categories

Resources