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
}
}
Related
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/
i am trying to make my emulator indicate where i am on the map , the problem is i don't get my real location nor the compass
i am only using the emulator on the computer
i integered the google map,the key, the imports inthe manifest.xml
here is the code :
package com.training.surveyproject;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
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;
public class SurveyActivity extends MapActivity implements LocationListener
{
private MapView mapView = null;
private LocationManager lm = null;
private double lat = 0;
private double lng = 0;
private MapController mc = null;
private MyLocationOverlay myLocation = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_survey);
mapView = (MapView) this.findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
lm = (LocationManager) this.getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, this);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, this);
mc = mapView.getController();
mc.setZoom(12);
myLocation = new MyLocationOverlay(getApplicationContext(),mapView);
myLocation.runOnFirstFix(new Runnable(){
public void run(){
mc.animateTo(myLocation.getMyLocation());
mc.setZoom(17);
}
});
mapView.getOverlays().add(myLocation);
myLocation.enableMyLocation();
myLocation.enableCompass();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_S)
{
mapView.setSatellite(!mapView.isSatellite());
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onLocationChanged(Location location)
{
lat = location.getLatitude();
lng = location.getLongitude();
Toast.makeText(getBaseContext(),
"Location change to : Latitude = " + lat + " Longitude = " + lng,
Toast.LENGTH_SHORT).show();
GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mc.animateTo(p);
mc.setCenter(p);
}
#Override
protected boolean isRouteDisplayed()
{
return false;
}
#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
}
}
thank u
You can send simulated GPS fixes to an emulator by opening the DDMS perspective and the 'emulator control' tab in there. That tab has a 'location control' option which will send the lat/long to the emulator.
i am trying google maps api on my android phone via following code. My app locating my phone and working great but gps notification icon not blinking. How can i blink gps icon when locating my phone.
Thanks For Replies.
Manifest:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
And The Code :
import java.util.List;
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 android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.EditText;
import android.widget.Toast;
public class main extends MapActivity {
MapController mControl;
GeoPoint geoP;
MapView mapV;
MyLocationOverlay compass;
EditText etext;
String provider;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etext = (EditText) findViewById(R.id.latLong);
mapV = (MapView) findViewById(R.id.mapView);
mapV.displayZoomControls(true);
mapV.setBuiltInZoomControls(true);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean enable = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enable) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
Criteria criteria = new Criteria();
//criteria.setAccuracy(Criteria.ACCURACY_FINE);
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
Double lat = location.getLatitude() * 1E6;
Double longi = location.getLongitude() * 1E6;
geoP = new GeoPoint(lat.intValue(), longi.intValue());
List<Overlay> mapOverlays = mapV.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
compass = new MyLocationOverlay(this, mapV);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
OverlayItem overlayitem = new OverlayItem(geoP, "Hola, Mundo!","I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
mapV.getOverlays().add(compass);
mapOverlays.add(itemizedoverlay);
mControl = mapV.getController();
mControl.animateTo(geoP);
mControl.setZoom(5);
mControl.setCenter(geoP);
locationManager.requestLocationUpdates(provider, 0, 0,
new LocationListener() {
public void onStatusChanged(String provider, int status,
Bundle extras) {
if (status == LocationProvider.AVAILABLE) {
Toast.makeText(getApplicationContext(),
"LocationProvider.AVAILABLE",
Toast.LENGTH_SHORT).show();
} else if (status == LocationProvider.TEMPORARILY_UNAVAILABLE) {
Toast.makeText(getApplicationContext(),
"LocationProvider.TEMPORARILY_UNAVAILABLE",
Toast.LENGTH_SHORT).show();
} else if (status == LocationProvider.OUT_OF_SERVICE) {
Toast.makeText(getApplicationContext(),
"LocationProvider.OUT_OF_SERVICE",
Toast.LENGTH_SHORT).show();
}
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
public void onLocationChanged(Location location) {
Toast.makeText(getApplicationContext(),
"Location Changed", Toast.LENGTH_SHORT).show();
}
});
//etext.setText(String.valueOf(lat) + "," + String.valueOf(longi));
}
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
compass.disableCompass();
}
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
compass.enableCompass();
}
protected boolean isRouteDisplayed() {
return false;
}
t0mm13b, working on my handset but GPS notification icon not blinking. Virtual Device means Emulator. I solved problem.
Code :
package com.yunusoksuz.gmapstest;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class YnsmapsActivity extends Activity implements LocationListener {
/** Called when the activity is first created. */
private LocationManager locationManager;
private Location location;
private String provider;
private Double lat, longi;
private Criteria criteria;
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.tv1);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
provider = locationManager.getBestProvider(criteria, false);
location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 0, 0, this);
}
public void onLocationChanged(Location location) {
Toast.makeText(
this,
"Lat : " + location.getLatitude() + " LONG : "
+ location.getLongitude(), Toast.LENGTH_SHORT).show();
tv.setText("Lat : " + location.getLatitude() + " LONG : "
+ location.getLongitude());
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
locationManager.removeUpdates(this);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, this);
}
}
Now gps notification blinking.
I just wanted to know if it is possible to force a location update in order to get the position of the mobile once I click on the refresh item in the menu.
Here is the code below that I found in a forum, and I want to add a new function.
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.OverlayItem;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MapLocator extends MapActivity implements LocationListener {
/** Called when the activity is first created. */
private MapView mapView;
private LocationManager lm;
private MapController mc;
private double lat = 0;
private double lng = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
mapView = (MapView) this.findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mc = mapView.getController();
mc.setZoom(14);
int FIVE_MINUTES = 5 /*Minutes*/ * 60 /*sec per min*/ * 1000 /*ms per sec*/;
lm = (LocationManager) this.getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, FIVE_MINUTES, 0, this);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, FIVE_MINUTES, 0, this);
Drawable drawable= this.getResources().getDrawable(R.drawable.loc_seven);
ListItimizedOverlay itemizedoverlay = new ListItimizedOverlay(drawable);
GeoPoint p = new GeoPoint(33000000,84000000);
OverlayItem overlayitem = new OverlayItem(p, "Hello from", "Tahiti");
itemizedoverlay.addOverlayItem(overlayitem);
mapView.getOverlays().add(itemizedoverlay);
}
#Override
protected boolean isRouteDisplayed()
{
return false;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_S)
{
mapView.setSatellite(!mapView.isSatellite());
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onLocationChanged(Location location)
{
Log.d("msg","1");
lat = location.getLatitude();
lng = location.getLongitude();
Toast.makeText(
getBaseContext(),
"Location change to : Latitude = " + lat + " Longitude = "
+ lng, Toast.LENGTH_SHORT).show();
GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mc.animateTo(p);
mc.setCenter(p);
mc.setZoom(14);
Drawable drawable= this.getResources().getDrawable(R.drawable.loc_seven);
ListItimizedOverlay itemizedoverlay = new ListItimizedOverlay(drawable);
OverlayItem overlayitem = new OverlayItem(p, "", "");
itemizedoverlay= (ListItimizedOverlay ) mapView.getOverlays().get(0);
itemizedoverlay.clear();
itemizedoverlay.addOverlayItem(overlayitem);
mapView.getOverlays().add(itemizedoverlay);
}
#Override
public void onProviderDisabled(String provider)
{
}
#Override
public void onProviderEnabled(String provider)
{
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
//Menu with 2 items Refresh and exit
public boolean onCreateOptionsMenu(Menu menu2) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menumap, menu2);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
/*
---------------------------------------------------------------------------------------
code to add in order to refrech the location and position the mobile
on the map using the drawable in the itemizedoverlay. I have tried this :
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
and nothing is changed i still see the old position on the map
and I have tested the app on a sony Ericsson Xperia x10.
I am using google API 10 if this might help.
------------------------------------------------------------------------------------------
*/
return true;
case R.id.exit:
finish();
return true;
}
return false;}
}
Can anyone help me please?
This code should do the trick:
lm.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null);
Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
lat = loc.getLatitude();
lng = loc.getLongitude();
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);
}