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()
Related
I have written the code for google maps it is working fine and showing locations also.Now i am trying for reverse geocoding so that i can get the address of the lattitude & longitude coordinates but it is not working.
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();
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;
}
mapView.getOverlays().clear();
mapView.invalidate();
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;
}
}
//
// Write the location name.
//
try {
Geocoder geo = new Geocoder(this.getApplicationContext(), Locale.getDefault());
List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
if (addresses.isEmpty()) {
addres.setText("Waiting for Location");
}
else {
if (addresses.size() > 0) {
addres.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());
//Toast.makeText(getApplicationContext(), "Address:- " + addresses.get(0).getFeatureName() + addresses.get(0).getAdminArea() + addresses.get(0).getLocality(), Toast.LENGTH_LONG).show();
}
}
}
catch (Exception e) {
e.printStackTrace(); // getFromLocation() may sometimes fail
}
I have written this code in my project and it works fine.
Compare this with your code. Hope this will help you....
I'm making a Google Map app. I want to get the address of a point when I click on my screen, but when I click on the screen, it doesn't run, nothing happens. How can I fix this?
This my code:
package app.googlemap;
public class GoogleMapActivity extends MapActivity {
MapView mapView;
View zoomView;
MapController mc;
GeoPoint p;
private List<Overlay> mapOverlays;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView=(MapView)findViewById(R.id.simple_map);
LinearLayout zoomLayout=(LinearLayout)findViewById(R.id.zoom);
zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mapView.setTraffic(true);
String coordinates[]={"21.036074","105.833636"};
double latiTude=Double.parseDouble(coordinates[0]);
double longiTude=Double.parseDouble(coordinates[1]);
p=new GeoPoint((int)(latiTude*1E6), (int)(longiTude*1e6));
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mc=mapView.getController();
mc.animateTo(p);
mc.setZoom(17);
mapView.invalidate();
}
public void draw(GeoPoint g,int a ){
mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(a);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
OverlayItem overlayitem = new OverlayItem(p, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
public class MapOverlay extends com.google.android.maps.Overlay{
private Bitmap bmp;
public boolean draw(Canvas canvas, MapView mapView, boolean shadow ,long when){
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
bmp=BitmapFactory.decodeResource(getResources(), R.drawable.t5);
canvas.drawBitmap(bmp, screenPts.x,screenPts.y-50, null);
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
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() {
// TODO Auto-generated method stub
return false;
}
}
call this method onTap of Map.
/**
* Method to getAddress from particular latitude and longitude.
* */
public static String getAddressFromLocation(double latitude,
double longitude, Context context) {
String locationAddress = "";
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latitude,
longitude, 1);
if (addresses != null && !addresses.isEmpty()) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder(
"Address:\n");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress
.append(returnedAddress.getAddressLine(i)).append(
"\n");
}
locationAddress = strReturnedAddress.toString();
locationAddress = locationAddress.replace("Address:", "");
}
} catch (IOException e) {
e.printStackTrace();
}
return locationAddress;
}
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));
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 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.