I am trying to put the marker on the map and i have done it but the problem is that it is showing all the markers.previous & recent also.I dont want to show the previous markers instead i just want to show the marker on the place which is clicked recently.All other markers should not be seen
I have written the code with the help of follwing tutorial
http://mobiforge.com/developing/story/using-google-maps-android
And my Code is
public class SelectLocation extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;
String coordinates[];
class MapOverlay extends com.google.android.maps.Overlay
{
private GeoPoint p;
public MapOverlay(GeoPoint p){
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(
getResources(), R.drawable.pushpin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
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());
Toast.makeText(getBaseContext(),
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
mapView.getOverlays().add(new MapOverlay(p));
Intent i=new Intent(SelectLocation.this,SetLocat.class);
startActivity(i);
}
return false;
}
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.selectlocation);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mc = mapView.getController();
String coordinates[] = {"1.352566007", "103.78921587"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
MapOverlay mapOverlay = new MapOverlay(p);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
}
/*protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}*/
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
before this code
mapView.getOverlays().add(new MapOverlay(p));
try,
mapView.getOverlays().clear();
mapView.invalidate();
mapView.getOverlays().add(new MapOverlay(p));
Related
I m trying to use google maps in my Android App. If I click on any position in the map, i want the search editbox to dispay its location but somehow its not getting updated. I can dispaly a toast bt not the text in my Search box.Please tell me what I am doing wrong. Any help will be highly appreciated. Thanks in advance.
Here is my code
public class GooglemapsActivity extends MapActivity
{
EditText txtSearch;
MapView mapView;
MapController mc;
GeoPoint p;
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//EditText name = (EditText) this.findViewById(R.id.travelNameText);
// name.setText(txtSearch.getText().toString());
}
class MapOverlay extends com.google.android.maps.Overlay
{
#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(
getResources(), R.drawable.icon);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
//---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";
}
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
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(GooglemapsActivity.this, "country: " +
String.valueOf(result.get(0).getCountryName()), Toast.LENGTH_SHORT).show();
lat = result.get(0).getLatitude();
lng = result.get(0).getLongitude();
EditText name = (EditText) this.findViewById(R.id.txtMapSearch);
name.setText("country: " +
String.valueOf(result.get(0).getCountryName()).toString());
}
else{
Toast.makeText(GooglemapsActivity.this, "record not found"
,Toast.LENGTH_SHORT).show();
return;
}
}
catch(IOException io)
{
Toast.makeText(GooglemapsActivity.this, "Connection Error"
, Toast.LENGTH_SHORT).show();
}
myLocation = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
//Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
mc.animateTo(myLocation);
mc.setZoom(10);
mapView.invalidate();
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
Button btnSearch=(Button) findViewById(R.id.btnSearch);
//txtSearch=(EditText)findViewById(R.id.c);
btnSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtSearch=(EditText)findViewById(R.id.txtMapSearch);
String area=txtSearch.getText().toString();
GooglemapsActivity.this.changeMap(area);
}
});
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();
String coordinates[] = {"1.352566007", "103.78921587"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
mapView.invalidate();
mc.animateTo(p);
mc.setZoom(17);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
}
use:
EditText name = (EditText) GooglemapsActivity.this.findViewById(R.id.txtMapSearch);
instead of
EditText name = (EditText) this.findViewById(R.id.txtMapSearch);
I got a solution to the problem. In the OnTouchEvent where i am creating the toast, i needed to set the text in my edit box at that position. Thanks for trying though.
here is the code:
public class GooglemapsActivity extends MapActivity
{
EditText txtSearch;
EditText TravelFrom;
MapView mapView;
MapController mc;
GeoPoint p;
class MapOverlay extends com.google.android.maps.Overlay
{
#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(
getResources(), R.drawable.icon);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
//---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; <addresses.get(0).getMaxAddressLineIndex();
i++)
add += addresses.get(0).getAddressLine(i) + "\n";
}
txtSearch = (EditText) GooglemapsActivity.this.
findViewById(R.id.txtMapSearch);
txtSearch.setText(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
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(GooglemapsActivity.this, "country: " + String.
valueOf(result.get(0).getCountryName()), Toast.LENGTH_SHORT).show();
lat = result.get(0).getLatitude();
lng = result.get(0).getLongitude();
//EditText name = (EditText) this.findViewById(R.id.txtMapSearch);
txtSearch = (EditText)GooglemapsActivity.this.findViewById(R.id.txtMapSearch);
txtSearch.setText("country: " + String.valueOf(result.get(0).
getCountryName()).toString());
}
else{
Toast.makeText(GooglemapsActivity.this,"record not
found", Toast.LENGTH_SHORT).show();
return;
}
}
catch(IOException io){
Toast.makeText(GooglemapsActivity.this,"ConnectionError",
Toast.LENGTH_SHORT).show();
}
myLocation = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
// Drawable drawable = this.getResources().getDrawable(R.drawable.bubble);
mc.animateTo(myLocation);
mc.setZoom(10);
mapView.invalidate();
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
Button btnSearch=(Button) findViewById(R.id.btnSearch);
//txtSearch=(EditText)findViewById(R.id.c);
btnSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtSearch=(EditText)findViewById(R.id.txtMapSearch);
String area=txtSearch.getText().toString();
GooglemapsActivity.this.changeMap(area);
}
});
Button btnDone=(Button) findViewById(R.id.btnDone);
//txtSearch=(EditText)findViewById(R.id.c);
btnDone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtSearch=(EditText)findViewById(R.id.txtMapSearch);
String area=txtSearch.getText().toString();
//GooglemapsActivity.this.changeMap(area);
TravelFrom=(EditText)findViewById(R.id.travelFromEntry);
TravelFrom.setText(area);
}
});
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();
String coordinates[] = {"1.352566007", "103.78921587"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
mapView.invalidate();
mc.animateTo(p);
mc.setZoom(17);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
}
I want to find current location and make marker on that location.
plz tell me
if any example is there then please tell me..
like this
Thanks in Advance...
for find current location use gps. You can get more examples for that and for marker see this links. https://github.com/jgilfelt/android-mapviewballoons
Here we have few simple steps
class MapOverlay extends com.google.android.maps.Overlay
{
#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(
getResources(), R.drawable.pin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
after abv code call your marker
/** 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);
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocationName(getIntent().getStringExtra("address"), 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Address address = addresses.get(0);
double lng = address.getLongitude();
double lat = address.getLatitude();
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
mapView.invalidate();
}
Hope I have cleared you some what. Keep smiling :)
i m testing an app that uses the google maps api. My app is working but my problem is:
i want the gps to find my location and present it.If my gps in enabled,it wants some seconds to find my location.In these seconds, my app starts and uses the default long and lat..How could i add something like progress bar until my location found?thanks
this is a part of my code:
private double locationLat=37.979116;
private double locationLon=23.717766;
MapView mapView;
MapController mc;
GeoPoint p;
//..........
//in the onCreate
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,0,0, mlocListener);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
// mapView.setSatellite(true);
// mapView.setStreetView(true);
mc = mapView.getController();
String lata = String.valueOf(locationLat);
String lnga = String.valueOf(locationLon);
String coordinates[] = {lata, lnga};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
//out of onCreate
class MapOverlay extends com.google.android.maps.Overlay
{
#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(
getResources(), R.drawable.avatar);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
#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";
}
Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
e.printStackTrace();
}
return true;
}
else
return false;
}
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
//String Text = "My current location is: " +
//"Latitud = " + loc.getLatitude() +
//"Longitud = " + loc.getLongitude();
locationLat=loc.getLatitude();
locationLon=loc.getLongitude();
// Toast.makeText( getApplicationContext(),
//
// Text,
//
// Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Disabled",
Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Enabled",
Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}/* End of Class MyLocationListener */
}/* End of Activity */
I would just put it into an ASyncTask and then pop up a Dialog Box saying finding your location...once found dismiss the dialog box...Google ASyncTask examples...that should help you out.
^^ Agreed with that not sure if you are just updating the overlay or calling your map controller to follow the lat and lon that gets updated but that is an idea you could use.
Also please read the documentation of "requestLocationUpdates" 0,0 will DESTROY a real phones battery. that is updating location every 0 milliseconds and/or 0 ft change.
i need to mark two points on map one is current location and second is what i give. in my code the second point only shown. first point does not show. please help me.
my code:
public class MapsActivity extends MapActivity
{
MapView mapView;
MapController mc;
GeoPoint p;
double latPoint, lngPoint;
class MapOverlay extends com.google.android.maps.Overlay
{
#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(
getResources(), R.drawable.pin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
/** 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);
if(myManager != null){
//List list = myManager.getAllProviders();
String param = (String)myManager.getProviders(true).get(0);
Location loc = myManager.getLastKnownLocation(param);
if(loc != null){
latPoint = loc.getLatitude();
lngPoint = loc.getLongitude();
Log.e("map",latPoint+" "+lngPoint);
}
else
Log.e("RootDraw ","Error: Location is null");
}
else
Log.e("RootDraw ","Error: Location Manager is null");
p = new GeoPoint((int) (latPoint * 1E6), (int) (lngPoint * 1E6));
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
// second point:
latPoint=x.xxxxxxxxxxxx; // nearest to current locattion
lngPoint=x.xxxxxxxxxxxx; // nearest to current locattion
p = new GeoPoint((int) (latPoint * 1E6), (int) (lngPoint * 1E6));
mc.animateTo(p);
mc.setZoom(9);
//---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;
}
}
Try creating 2 GeoPoints objects and then in draw function, use following code:
#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(
getResources(), R.drawable.pin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
mapView.getProjection().toPixels(p1, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.pin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
i have got the following codes but my gps is not getting the current location, showing me the address on touch and no overlay. I got the latitude and longitude as 0.0. Basically only the map is showing up. Help is greatly appreciated. Thank you!
public class Map extends MapActivity{
MapView mapView;
MapController mc;
GeoPoint p;
private LocationManager lm;
private LocationListener locationListener;
Button btn_send;
int lat, lng;
class MapOverlay extends com.google.android.maps.Overlay
{
#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(
getResources(), R.drawable.pin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
#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";
}
Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
e.printStackTrace();
}
return true;
}
else
return false;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
MapController mc = mapView.getController();
switch (keyCode)
{
case KeyEvent.KEYCODE_3:
mc.zoomIn();
break;
case KeyEvent.KEYCODE_1:
mc.zoomOut();
break;
}
return super.onKeyDown(keyCode, event);
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new GeoUpdateHandler());
Button btnSend = (Button) findViewById(R.id.btn_send);
btnSend.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent new_intent = new Intent("net.learn2develop.SendSMS");
startActivity(new_intent);
}
});
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);
mapView.setSatellite(false);
mapView.setStreetView(true);
mc = mapView.getController();
String latitude = Integer.toString(lat);
String longitude = Integer.toString(lng);
String coordinates[] = {latitude, longitude};
double lati = Double.parseDouble(coordinates[0]);
double lngi = Double.parseDouble(coordinates[1]);
System.out.println("lat" + lati + "lng" + lngi );
p = new GeoPoint(
(int) (lati * 1E6),
(int) (lngi * 1E6));
mc.animateTo(p);
mc.setZoom(17);
mapView.invalidate();
//---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;
}
public class GeoUpdateHandler implements LocationListener {
#Override
public void onLocationChanged(Location location) {
lat = (int) (location.getLatitude() * 1E6);
lng = (int) (location.getLongitude() * 1E6);
System.out.println("lat shld b smth " + lat + "lng smth " +lng);
//GeoPoint point = new GeoPoint(lat, lng);
//mc.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) {
}
}
}
If you want to display the current location of the user, you might be better off using the MyLocationOverlay. You can also subclass it and override onLocationChanged, to insert custom code.
Enable it by calling enableMyLocation onResume() and disableMyLocation onPause().
It take 2 to 15 minutes to get a satellite GPS fix. You should move the code for rendering the point to onLocationChanged()