set Latitude and Longitude in map - android

i write a map viewer and i want to get Latitude and Longitude from user, then show him the location.
i use menu for this work. but i don't know how show him something to enter the values. this is my code.
public class main extends MapActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView view = (MapView) findViewById(R.id.themap);
view.setBuiltInZoomControls(true);
final MapController control = view.getController();
LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener listener = new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location location) {
control.setCenter(new GeoPoint((int)location.getLatitude(), (int)location.getLongitude()));
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
so i have 2 questions. first how can i create a menu for my locations. i must use context menu or something else.
second, if i get locations, i can enter them directly to my Latitude and Longitude?
in my code i use the default location of user.

Well if I am understanding you correctly you can do this 2 ways.
1) In your xml layout add an input box and button then on click set your location.
2) Override your onTouch and have and alert box pop up and ask for the long and lat. Then set it accordingly.
Both these will be setting your overLay: http://developer.android.com/resources/tutorials/views/hello-mapview.html
one of many overlay tutorials: http://eagle.phys.utk.edu/guidry/android/mapOverlayDemo.html

If u only want to show your current position yo can use MyLoicationOverlay class:
miPunto = new MyLocationOverlay(this, eMapa);
eMapa.getOverlays().add(miPunto);
miPunto.enableCompass();
miPunto.enableMyLocation();
If you wont to use this, yo must to get your location and then use an itemizedoverlay

This is my code, It works fine with me
Drawable marker=getResources().getDrawable(R.drawable.mapmarker);
marker.setBounds( (int) (-marker.getIntrinsicWidth()/2),
-marker.getIntrinsicHeight(),
(int) (marker.getIntrinsicWidth()/2),
0);
InterestingLocations funPlaces =
new InterestingLocations(marker);
Add your overlay Item in here.
mapView.getOverlays().add(funPlaces);
class InterestingLocations extends ItemizedOverlay {
private ArrayList<OverlayItem> locations =
new ArrayList<OverlayItem>();
private GeoPoint center = null;
public InterestingLocations(Drawable marker)
{
super(marker);
// Put your custom Lat, Long in here ,
GeoPoint disneySevenLagoon =
new GeoPoint((int)(28.410067*1000000),
(int)(-81.583699*1000000));
locations.add(new OverlayItem(disneySevenLagoon ,
"Seven Seas Lagoon", "Seven Seas Lagoon"));
populate();
}

Related

Android Map zoom to overlay item

I am trying to use the Google maps API on my android app. I have set an overlay item at a particular geopoint co-ordinate. On opening the activity, it uses the default mapview of the google map with the correct location of the overlay item for that geotpoint. My question is, how do I set the view to be zoomed in for that geopoint so one would actually see the location in a better fashion rather than having to manually zoom in by tracing the overlay item. I wish to zoom it so I can see the local area so the user can at least derive useful information from it.
My code is as follows:
/---- HelloItemizedOverlay.java -------/
public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public void addOverlay(OverlayItem overlay)
{
mOverlays.add(overlay);
populate();
}
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
#Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
/*---- MainActivity.java ---- */
public class MainActivity extends MapActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView view = (MapView)findViewById(R.id.themap);
view.setBuiltInZoomControls(true);
final GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
GeoPoint point2 = new GeoPoint(35410000, 139460000);
OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa!", "I'm in Japan!");
final MapController control = view.getController();
LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener listener = new LocationListener() {
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
control.setCenter(point);
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
List<Overlay> mapOverlays = view.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
itemizedoverlay.addOverlay(overlayitem2);
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
#noobcoder I have used the same example, and if i get your question right, you can use the following method to zoom to a particular point.
https://developers.google.com/maps/documentation/android/reference/com/google/android/maps/MapController#animateTo(com.google.android.maps.GeoPoint, java.lang.Runnable)
It worked at least for me.
view = (MapView)findViewById(R.id.themap);
mapController = view.getController();
point = new GeoPoint((int) (38.897089* 1E6), (int) (-77.051437* 1E6));
mapControler .animateTo(point );
mapControler .setZoom(17); //Set zoom level as you want..,.
Try this..,.

show Progress Dialog in OnLocationChanged

I know there are thousands of questions like this. But I can't find an explanation to mine.
I use the onLocationChanged method to update the user's location on a mapView. Everything's fine; I display a ProgressDialog in the onCreate method and dismiss it at the end of OnlocationChanged and it works.
The problem is when I create and show a Progress Dialog inside the onLocationChanged method. Somehow it doesn't work. I think it's because the method runs on a different thread.
But my question is, if the onLocationChanged method runs on a different thread, why does it let me to dismiss the dialog but not create a new one?
Here's part of my class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tiendas);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
ProgressDialog dialog = ProgressDialog.show(StoresActivity.this, "",
"Getting your location", true, true);
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
/////if i show or initialize the dialog here, doesn't work!
updateMap(location) // this function calls asynctask
// for an HTTPrequest
dialog.dismiss();
}
}
That code works and dismisses the dialog correctly, but if I declare or show the dialog inside the onLocationChanged method, it never displays. Anybody?
Why does it dismiss it but can't show it?
Pass the Context of LocationListener class in ProgressDialog.
Check this code its working fine for me
public class LocationFinderActivity extends Activity implements LocationListener{
LocationManager locationManager = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_finder);
Log.i("inside","oncreate");
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0,this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_location_finder, menu);
return true;
}
#Override
public void onLocationChanged(Location location) {
Log.i("inside","onlocationchange");
ProgressDialog dialog = ProgressDialog.show(LocationFinderActivity.this, "",
"Getting your location", true, true);
}
#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
}
}

set value from a custom dialog

i write a program that you can give location from user, then show him the location. for get location i use a custom dialog, but i don't know how use the location that user entered. for example i use a button for accept values but every time i use this method i get an error.
this is the source that i use a button and get error :
public class main extends MapActivity {
/** Called when the activity is first created. */
EditText lat;
EditText lang;
Integer latitude;
Integer longtitude;
Button b = (Button) findViewById(R.id.button1);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView view = (MapView) findViewById(R.id.themap);
view.setBuiltInZoomControls(true);
final MapController control = view.getController();
LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener listener = new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location location) {
latitude = Integer.valueOf(lat.getText().toString());
longtitude = Integer.valueOf(lang.getText().toString());
control.setCenter(new GeoPoint(latitude, longtitude));
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Dialog d = new Dialog(main.this);
d.setContentView(R.layout.choose);
d.setTitle("Location");
d.show();
lat = (EditText) findViewById(R.id.editText1);
lang = (EditText) findViewById(R.id.editText2);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
return super.onOptionsItemSelected(item);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
this is my permissions :
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
so this is my problems :
why i get error and how i can fix that, i mean how i can use this button to close my custom dialog and set the location.
logcat :
05-23 13:15:10.953: W/KeyCharacterMap(13490): No keyboard for id 131074
05-23 13:15:10.953: W/KeyCharacterMap(13490): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-23 13:15:21.434: W/MapActivity(13490): Recycling dispatcher android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher#40719228
05-23 13:15:21.444: V/MapActivity(13490): Recycling map object.
05-23 13:15:21.524: I/MapActivity(13490): Handling network change notification:CONNECTED
05-23 13:15:21.524: E/MapActivity(13490): Couldn't get connection factory client

How to remove current location pin from map in android, keeping other pins

I have a map which at the moment I place a custom pin on the map every so often from the GPS position of the user, this is also dependent on when their position changes but with the GPS being a bit choppy here it does it quite a lot. At the moment a new pin is placed on the map every time this happens. I want to remove the previous pin so that just the one pin for the users location is shown on the map, like a live representation of the users position.
In addition to this. Is it possible to have a separate set of pins on the map (which their positions never change) and ensure they are not also cleared when the users position is updated.
Any other comments on the code would be appreciated.
Imports...
public class MapOne extends MapActivity implements LocationListener {
... fields
#Override
protected void onCreate(Bundle icicle) {
....onCreate initialisations
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
compass.disableCompass();
lm.removeUpdates(this);
super.onPause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
compass.enableCompass();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 20, this);
super.onResume();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
public void onLocationChanged(Location l) {
// TODO Auto-generated method stub
lat = (int)(l.getLatitude() *1E6);
longi = (int)(l.getLongitude() *1E6) ;
GeoPoint ourLocation = new GeoPoint(lat, longi);
OverlayItem overlayItem = new OverlayItem(ourLocation, "Hey!", "What's up!?");
CustomPinpoint custom = new CustomPinpoint(customMarker, Map.this);
custom.insertPinpoint(overlayItem);
overlayList.add(custom);
}
}
Pinpoint class for reference if required.
public class Pinpoint extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();
private Context c;
public CustomPinpoint(Drawable defaultMarker) {
super(boundCenter(defaultMarker));
// TODO Auto-generated constructor stub
}
public CustomPinpoint(Drawable m, Context context) {
// TODO Auto-generated constructor stub
this(m);
setC(context);
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return pinpoints.get(i);
}
#Override
public int size() {
// TODO Auto-generated method stub
return pinpoints.size();
}
public void insertPinpoint(OverlayItem item) {
pinpoints.add(item);
this.populate();
}
public Context getC() {
return c;
}
public void setC(Context c) {
this.c = c;
}
}
If you want to add a Pin that follows the users current location,you can use com.google.android.maps.MyLocationOverlay ....this provides you with the required listeners also.You can also override its onDraw() method to draw your custom pin.You can then add this overlay just like anyother overlay...don't forget to call myLocationOverlay.enableMyLocation() so that you recieve the location changes.

Fake location using GPS in android

Hi am developing an app that will set the coordinates(latitude and longitude). And it has to show my location as i am at that coordinates..It is similar to location spoofer.. http://www.androidzoom.com/android_applications/tools/location-spoofer_gkmc.html
But I am failing to do that.. here is my code..Please any one help me.
public class Mock extends MapActivity
{
private LocationManager lm;
private LocationListener locationListener;
private MapView mapView;
String mocLocationProvider;
private MapController mc;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//---use the LocationManager class to obtain GPS locations---
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
mocLocationProvider=LocationManager.GPS_PROVIDER;
lm.addTestProvider(mocLocationProvider, false, false,false, false, true, true, true, 0, 5);
lm.setTestProviderEnabled(mocLocationProvider,true);
lm.requestLocationUpdates(mocLocationProvider,0,0,locationListener);
mapView = (MapView) findViewById(R.id.mapview1);
mc = mapView.getController();
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location loc) {
loc = new Location(mocLocationProvider);
Double latitude = 1.352566007;
Double longitude = 103.78921587;
//Double altitude = Double.valueOf(parts[2]);
loc.setLatitude(latitude);
loc.setLongitude(longitude);
loc.setTime(System.currentTimeMillis());
lm.setTestProviderLocation(mocLocationProvider, loc);
mc.setZoom(16);
mapView.invalidate();
}
#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
}
}
}
I would use this method http://developer.android.com/guide/developing/tools/ddms.html#emulator-control. Haven't tried this one: http://code.google.com/p/android/issues/detail?id=915
You basically set the coordinates on real device in the same way you do in the emulator by using DDMS in Eclipse.
Just make sure you enable "fake locations" in your device. You can find the setting at Settings > Applications > Development > Allow mock locations.

Categories

Resources