i have questions that i am really need some help on it:
i develop application and i want :
1-retrieve the user location and show it in the map.
2- the user can put pin in any place in the map and i should take the coordination to this pin and store it in variable
Note:my application in 2.1 should i use google API? if yes, does the google API support any activity rather than maps?
i search in google and find this code :
package com.java.locate;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class AndroidLbsGeocodingProjectActivity extends Activity {
/** Called when the activity is first created. */
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
protected LocationManager locationManager;
protected Button retrieveLocationButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
retrieveLocationButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showCurrentLocation();
}
});
}
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(AndroidLbsGeocodingProjectActivity.this, message,
Toast.LENGTH_LONG).show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(AndroidLbsGeocodingProjectActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(AndroidLbsGeocodingProjectActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(AndroidLbsGeocodingProjectActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(AndroidLbsGeocodingProjectActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}
}
this is another code, this by using google API, but i have 2 issues in using this code :
1-it does not retrieve the user location, it is just show me the maps.
2-does google API working with another activity not just map?
package our.google.maps;
import java.util.List;
import java.util.Locale;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;
public class MapsActivity extends MapActivity {
/** Called when the activity is first created. */
long start;
long stop;
MyLocationOverlay compass;
MapController controller;
MapView map;
//136
int x,y;
GeoPoint touchedPoint;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map=(MapView) findViewById(R.id.mvMain);
map.setBuiltInZoomControls(true);
Touchy t = new Touchy();
List<Overlay> overlaylist= map.getOverlays();
overlaylist.add(t);
compass = new MyLocationOverlay (MapsActivity.this, map);
overlaylist.add(compass);
//map controller to go to Specific Location n36eeh el 6ol & el 3r'9 135
controller=map.getController();
GeoPoint point = new GeoPoint (51643234 , 7848593);
controller.animateTo(point);
controller.setZoom(6);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
compass.disableCompass();
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
compass.enableCompass();
super.onResume();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
class Touchy extends Overlay {
public boolean onTouchEvent(MotionEvent e,MapView m){
if(e.getAction() == MotionEvent.ACTION_DOWN){
start = e.getEventTime();
x = (int) e.getX();
y = (int) e.getY();
touchedPoint = map.getProjection().fromPixels(x, y);
}
if (e.getAction() == MotionEvent.ACTION_UP) {
stop =e.getEventTime();
}
if(stop - start >1500) {
AlertDialog alert = new AlertDialog.Builder(MapsActivity.this).create();
alert.setTitle("Pick an option");
alert.setMessage(" i told u to pick an option");
alert.setButton("place a pin", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
} );
alert.setButton2("get an address", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Geocoder geocode = new Geocoder (getBaseContext(),Locale.getDefault());
try {
}
finally{}}}
);
alert.setButton3("option3 ", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
} );
alert.show();
return true;
}
return false;
}
}
private class MyLocationListener implements LocationListener{
public void onLocationChanged1(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(MapsActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(MapsActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(MapsActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(MapsActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
}
}
also,if my code is not correct, can you please give me the correct coed?
StackOverFlow members you are My HERO! help me PLEASE!
You can Use Google Maps.
You will require Map Api key.
If you are coding Using Eclipse then you can follow the procedure given on link
http://mobiforge.com/developing/story/using-google-maps-android