In my code I have one textbox and one button. Whatever place you type into the textbox, that place has to get searched from Google maps on clicking the button. And an icon has to be shown to the searched place.
In my code I am getting the map, but I am not able to search for the location. Please help.
public class MapActivity extends com.google.android.maps.MapActivity implements
OnClickListener {
/** Called when the activity is first created. */
MapView view;
Button search;
EditText location;
MapController controller;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = (MapView) findViewById(R.id.themap);
location = (EditText) findViewById(R.id.editText1);
search = (Button) findViewById(R.id.search);
search.setOnClickListener(this);
view.setBuiltInZoomControls(true);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == search) {
Geocoder geo = new Geocoder(getApplicationContext(),
Locale.getDefault());
try {
List<Address> addresses = geo.getFromLocationName(location
.getText().toString(), 5);
if (addresses.size() > 0) {
GeoPoint p = new GeoPoint((int) (addresses.get(0)
.getLatitude() * 1E6), (int) (addresses.get(0)
.getLongitude() * 1E6));
controller.animateTo(p);
controller.setZoom(12);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> lisOverlays = view.getOverlays();
lisOverlays.clear();
lisOverlays.add(mapOverlay);
} else {
AlertDialog.Builder adb = new AlertDialog.Builder(
MapActivity.this);
adb.setTitle("Google Map");
adb.setMessage("please provide proper place");
adb.setPositiveButton("Close", null);
adb.show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
MapOverlay.java
public class MapOverlay extends com.google.android.maps.Overlay
{
Context context;
public boolean draw(Canvas canvas,MapView mapView,boolean shadow,long when)
{
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
GeoPoint p = null;
mapView.getProjection().toPixels(p, screenPts);
Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
return true;
}
}
When I compile this, I am getting an error in the line: controller.animateTo(p);
In oncreate() instantiate the Mapcontroller as below so that you will not get an error in animateTo method.
controller= view.getController();
also GeoPioint p =null; so replace the MAp overlay class with below
MapOverlay.java
public class MapOverlay extends com.google.android.maps.Overlay
{
GeoPoint p;
Context context;
public MyOverLay(Context context,GeoPoint p) // GeoPoint is a int. (6E)
{
this.p = p;
this.context = context;
}
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);
Bitmap bitmap=BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
return true;
}
then in your main code pass geopoint like below it will solve your problem....
MapOverlay mapOverlay = new MapOverlay(getApplicationContext(),p);
Related
ok as the question says I have a mapview and a OnTouchListener set to it and in the Ontouch function i set a overlay image on the map. But in this process now the zoom controls and drag functionality in the mapview is removed somehow. please help me out with it.
I have already setzoomcontrolson to true (no effect). Help please.
public class marker extends ItemizedOverlay {
Context mContext;
OverlayItem overlayitem;
Drawable marker;
private int xDragImageOffset=0;
private int yDragImageOffset=0;
private int xDragTouchOffset=0;
private int yDragTouchOffset=0;
OverlayItem inDrag;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public marker(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
marker=defaultMarker;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.clear();
mOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
#Override
public boolean onTap(GeoPoint geoPoint, MapView mapView){
boolean tappedAnOverlay = super.onTap(geoPoint, mapView);
if (tappedAnOverlay) {
places.gp=geoPoint; // do your thing if hit an overlay
}
else {
// no overlay found in that location
}
MapController mc=mapView.getController();
mc.animateTo(geoPoint);
return true;
}
}
this is my marker overlays file
and
public class places extends MapActivity implements android.view.View.OnClickListener/*,OnTouchListener*/ {
MapView mapView;
MapController mc;
Button srchbtn;
EditText searchstring;
marker itemizedoverlay;
public static List<Overlay> mapOverlays;
OverlayItem overlayitem;
public static GeoPoint gp;
Button back;
String s1;
String s2="yourprofile";
String s3="partnerprofile";
public int zoom=5;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.place);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
srchbtn=(Button)findViewById(R.id.searchbtn);
searchstring=(EditText)findViewById(R.id.edplnm);
srchbtn.setOnClickListener(this);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
itemizedoverlay = new marker(drawable, this);
back=(Button)findViewById(R.id.btnbk);
back.setOnClickListener(this);
s1=getIntent().getStringExtra("ClassName");
mc=mapView.getController();
mc.setZoom(zoom);
gp=new GeoPoint((int)(21.7679 * 1E6), (int)(78.8718 * 1E6));
mc.animateTo(gp);
// mapView.setOnTouchListener(this);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.searchbtn:String s = searchstring.getText().toString();
String[] vals =s.split(":");
float lat;
float logi;
lat=Float.parseFloat(vals[0]);
logi=Float.parseFloat(vals[1]);
gp = new GeoPoint((int)(lat * 1E6), (int)(logi * 1E6));
MapController mc=mapView.getController();
mc.setZoom(80);
mc.animateTo(gp);
overlayitem = new OverlayItem(gp, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
break;
case R.id.btnbk: int Lat;
int Logi;
Lat=gp.getLatitudeE6();
Logi=gp.getLongitudeE6();
boolean val=s1.equals(s2);
boolean val2=s1.equals(s3);
Intent I1=new Intent(places.this,yourprofile.class);
Intent I2=new Intent(places.this,partnerprofile.class);
if(val)
{
I1.putExtra("LAT",Lat);
I1.putExtra("LONG",Logi);
setResult(RESULT_OK,I1);
finish();
}else
if(val2)
{
I2.putExtra("LAT",Lat);
I2.putExtra("LONG",Logi);
setResult(RESULT_OK,I2);
finish();
}
break;
}
}
/*public boolean onTouch(View v, MotionEvent e) {
if(e.getAction()==MotionEvent.ACTION_UP){
Projection p =((MapView)v).getProjection();
gp = p.fromPixels((int) e.getX(), (int) e.getY());
overlayitem = new OverlayItem(gp, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
mc.setZoom(zoom);
mc.animateTo(gp);
searchstring.setText(((float)gp.getLatitudeE6())/1000000 + ":" +
((float)gp.getLongitudeE6())/1000000);
}
return true;
}*/
}
this is my places class file
Here is the Sample Working Demo which you may want to Use.
It will help you to Drag&Drop the Image with Zooming and Touch Functionality for Mapview.
Hope it will Help.
Hey this is what i have written to get the radius as well as the current location point. but it does not center and show. what have i done wrong?
as shown as bellow
please be kind enough to help me with the cording. thank you
public class MappingActivity extends MapActivity {
/** Called when the activity is first created. */
MapController mControl;
GeoPoint GeoP;
MapView mapV;
Drawable d;
List<Overlay> overlaylist;
public double lat;
public double lng;
Button checkin, addplace;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in
// Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 10000; // in
// Milliseconds
protected LocationManager locationManager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapV = (MapView) findViewById(R.id.mapview);
checkin = (Button) findViewById(R.id.check);
addplace = (Button) findViewById(R.id.addp);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener());
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
lat = 6;//location.getLatitude();
lng = 77.6;//location.getLongitude();
}
Button check = (Button) findViewById(R.id.check);
Button addplace = (Button) findViewById(R.id.addp);
Button nearby = (Button) findViewById(R.id.point);
check.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView result = (TextView) findViewById(R.id.result);
result.setText("Checked the Plce");
}
});
addplace.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView result = (TextView) findViewById(R.id.result);
result.setText("Added the Plce");
}
});
nearby.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView result = (TextView) findViewById(R.id.result);
result.setText("Nearby the Plce");
}
});
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
String message = String.format("You are Here");
Toast.makeText(MappingActivity.this, message, Toast.LENGTH_LONG)
.show();
GeoP = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mControl = mapV.getController();
mControl.animateTo(GeoP);
mControl.setZoom(19);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapV.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapV.invalidate();
}
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
}
class MapOverlay extends com.google.android.maps.Overlay
{
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
Projection projection = mapView.getProjection();
Point pt = new Point();
projection.toPixels(GeoP ,pt);
float circleRadius = projection.metersToEquatorPixels(50);
Paint innerCirclePaint;
innerCirclePaint = new Paint();
innerCirclePaint.setColor(Color.BLUE);
innerCirclePaint.setAlpha(25);
innerCirclePaint.setAntiAlias(true);
innerCirclePaint.setStyle(Paint.Style.FILL);
Paint CirclePaint;
CirclePaint = new Paint();
CirclePaint.setColor(Color.BLUE);
CirclePaint.setAlpha(100);
CirclePaint.setAntiAlias(true);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.point);
canvas.drawBitmap(bmp, pt.x, pt.y-bmp.getHeight(), CirclePaint);
super.draw(canvas,mapView,shadow);
canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, innerCirclePaint);
}
}
}
}
Your sample code works fine for me but I used it a little differently
public class Map extends MapActivity{
private double latitude, longitude, accuracy;
private int centerLatitude, centerLongitude;
private MyLocationOverlay userLocationOverlay;
private MapView mapView;
private GeoPoint point, center;
private MapController mapController;
private CountDownTimer timer;
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.map_view);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.dotpin);
MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable, this);
//Open Database
DbHelper dbHelper = new DbHelper(getApplicationContext());
SQLiteDatabase db = dbHelper.getReadableDatabase();
//Get Values
String[] Labels = {DbHelper.Latitude, DbHelper.Longitude, DbHelper.Accuracy};
Cursor cursor = db.query(DbHelper.TABLE_NAME, Labels, null, null, null, null, null);
if(cursor.moveToFirst()){
do{
latitude = cursor.getDouble(cursor.getColumnIndex(Labels[0])) *1000000;
longitude = cursor.getDouble(cursor.getColumnIndex(Labels[1])) *1000000;
accuracy = cursor.getDouble(cursor.getColumnIndex(Labels[2]));
}while(cursor.moveToNext());
}
//Close Database
cursor.close();
db.close();
dbHelper.close();
//User Location Overlay
userLocationOverlay = new MyLocationOverlay(getApplicationContext(), mapView);
//Set Location Overlay
point = new GeoPoint((int)(latitude),(int)(longitude));
OverlayItem overlayitem = new OverlayItem(point, "Radius", String.format("%.0f m", accuracy));
itemizedoverlay.addOverlay(overlayitem);
//Add overlays
MapOverlay mapOverlay = new MapOverlay();
mapOverlays.add(mapOverlay);
mapOverlays.add(userLocationOverlay);
mapOverlays.add(itemizedoverlay);
}
#Override
protected void onResume() {
super.onResume();
userLocationOverlay.enableMyLocation();
//Pan to location
mapController = mapView.getController();
//Update the map view
zoomUpdate();
//Refresh view
mapView.postInvalidate();
}
#Override
protected void onPause() {
super.onPause();
userLocationOverlay.disableMyLocation();
if(timer!=null)
timer.cancel();
}
protected void zoomUpdate(){
if(timer != null)
timer.cancel();
timer = new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished){}
public void onFinish() {
if(userLocationOverlay.getMyLocation() != null)
zoomInOnPoints(point, userLocationOverlay.getMyLocation());
else
Toast.makeText(getApplicationContext(), "Waiting for location", Toast.LENGTH_SHORT).show();
zoomUpdate();
}
};
timer.start();
}
#SuppressWarnings("deprecation")
private void zoomInOnPoints(GeoPoint setLocation, GeoPoint userLocation) {
int x_pixels = getWindowManager().getDefaultDisplay().getWidth();
int y_pixels = getWindowManager().getDefaultDisplay().getHeight();
boolean set = false;
int latSpan, longSpan, rightEdge, leftEdge, topEdge, bottomEdge;
boolean setIsIn, userIsIn;
center = new GeoPoint((point.getLatitudeE6() + userLocationOverlay.getMyLocation().getLatitudeE6())/2,(point.getLongitudeE6() + point.getLongitudeE6())/2);
centerLatitude = center.getLatitudeE6();
centerLongitude = center.getLongitudeE6();
mapController.animateTo(center);
while(!set){
latSpan = mapView.getLatitudeSpan();
longSpan = mapView.getLongitudeSpan();
rightEdge = centerLongitude+longSpan/2;
leftEdge = centerLongitude-longSpan/2;
topEdge = centerLatitude+latSpan/2;
bottomEdge = centerLatitude-latSpan/2;
setIsIn = setLocation.getLatitudeE6() > bottomEdge && setLocation.getLatitudeE6() < topEdge && setLocation.getLongitudeE6() < rightEdge && setLocation.getLongitudeE6() > leftEdge;
userIsIn = userLocation.getLatitudeE6() > bottomEdge && userLocation.getLatitudeE6() < topEdge && userLocation.getLongitudeE6() < rightEdge && userLocation.getLongitudeE6() > leftEdge;
if(setIsIn && userIsIn && mapView.getZoomLevel() < mapView.getMaxZoomLevel()){
mapController.zoomInFixing(x_pixels/2, y_pixels/2);
mapController.animateTo(center);
mapView.postInvalidate();
}
else{
while(!set){
latSpan = mapView.getLatitudeSpan();
longSpan = mapView.getLongitudeSpan();
rightEdge = centerLongitude+longSpan/2;
leftEdge = centerLongitude-longSpan/2;
topEdge = centerLatitude+latSpan/2;
bottomEdge = centerLatitude-latSpan/2;
setIsIn = setLocation.getLatitudeE6() > bottomEdge && setLocation.getLatitudeE6() < topEdge && setLocation.getLongitudeE6() < rightEdge && setLocation.getLongitudeE6() > leftEdge;
userIsIn = userLocation.getLatitudeE6() > bottomEdge && userLocation.getLatitudeE6() < topEdge && userLocation.getLongitudeE6() < rightEdge && userLocation.getLongitudeE6() > leftEdge;
if(!(setIsIn && userIsIn) && mapView.getZoomLevel() > 1){
mapController.zoomOutFixing(x_pixels/2, y_pixels/2);
mapController.animateTo(center);
mapView.postInvalidate();
}
else{
set = true;
}
}
}
}
}
public class MyItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public MyItemizedOverlay(Drawable arg0, Context context) {
super(boundCenter(arg0));
mContext = context;
// TODO Auto-generated constructor stub
}
#Override
protected OverlayItem createItem(int arg0) {
return mOverlays.get(arg0);
}
#Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
#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;
}
}
class MapOverlay extends com.google.android.maps.Overlay
{
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
Projection projection = mapView.getProjection();
Point pt = new Point();
projection.toPixels(point ,pt);
float circleRadius = projection.metersToEquatorPixels((float)accuracy);
Paint innerCirclePaint;
innerCirclePaint = new Paint();
innerCirclePaint.setColor(Color.BLUE);
innerCirclePaint.setAlpha(25);
innerCirclePaint.setAntiAlias(true);
innerCirclePaint.setStyle(Paint.Style.FILL);
Paint CirclePaint;
CirclePaint = new Paint();
CirclePaint.setColor(Color.BLUE);
CirclePaint.setAlpha(100);
CirclePaint.setAntiAlias(true);
canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, innerCirclePaint);
}
}
}
I can view the map in android mobile now i put one edit text for search area. And create one button for search the place
enter code here
public class MapActivity extends com.google.android.maps.MapActivity implements
OnClickListener {
/** Called when the activity is first created. */
MapView view;
Button search;
EditText location;
MapController controller;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = (MapView) findViewById(R.id.themap);
location = (EditText) findViewById(R.id.editText1);
search = (Button) findViewById(R.id.search);
search.setOnClickListener(this);
view.setBuiltInZoomControls(true);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == search) {
Geocoder geo = new Geocoder(getApplicationContext(),
Locale.getDefault());
try {
List<Address> addresses = geo.getFromLocationName(location
.getText().toString(), 5);
if (addresses.size() > 0) {
GeoPoint p = new GeoPoint((int) (addresses.get(0)
.getLatitude() * 1E6), (int) (addresses.get(0)
.getLongitude() * 1E6));
controller.animateTo(p);
controller.setZoom(12);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> lisOverlays = view.getOverlays();
lisOverlays.clear();
lisOverlays.add(mapOverlay);
} else {
AlertDialog.Builder adb = new AlertDialog.Builder(
MapActivity.this);
adb.setTitle("Google Map");
adb.setMessage("please provide proper place");
adb.setPositiveButton("Close", null);
adb.show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Mapoverlay.java
public class MapOverlay extends com.google.android.maps.Overlay {
Context context;
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
GeoPoint p = null;
mapView.getProjection().toPixels(p, screenPts);
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_launcher);
canvas.drawBitmap(bitmap, screenPts.x, screenPts.y, null);
return true;
}
}
My log cat shows error in controller.animateTo(p);
How can i solve the error in the above line.
How do i provide search option in the map.
Could someone tell me where the mistake is in the above code?
In oncreate() instantiate the Mapcontroller as below so that you will not get an error in animateTo method.
controller= view.getController();
I have created googlemap in my android application. Now i want to draw pin at my location.
This is my code for that:-
public class GooglemapActivity extends MapActivity {
private List<Overlay> mapOverlays;
private Drawable drawable;
private MyOverlay itemizedOverlay;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mMapView;
mMapView = (MapView) findViewById(R.id.mapview);
mMapView.setBuiltInZoomControls(true);
mapOverlays = mMapView.getOverlays();
drawable = getResources().getDrawable(R.drawable.c_map_pin_blue);
itemizedOverlay = new MyOverlay(drawable);
GeoPoint point = new GeoPoint((int) 26.863966000000000,
(int) 75.766938300000000);
itemizedOverlay.addItem(point, "Quippelin", "0");
mapOverlays.add(itemizedOverlay);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
and this is my another class:-
public class MyOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();
public MyOverlay(Drawable marker) {
super(boundCenterBottom(marker));
// TODO Auto-generated constructor stub
populate();
}
public void addItem(GeoPoint p, String title, String snippet) {
OverlayItem newItem = new OverlayItem(p, title, snippet);
overlayItemList.add(newItem);
populate();
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return overlayItemList.get(i);
}
#Override
public int size() {
// TODO Auto-generated method stub
return overlayItemList.size();
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);
// boundCenterBottom(marker);
}
}
my map is displaying correctly but my pin is not displaying, please help me to findout the mistake which i have done.
Thank you in advance.
Everything looks good except your point is way "off".
Your calculation is wrong as your pin should be displayed near coordinates 0,0. Try that:
GeoPoint point = new GeoPoint((int) (26.863966 * 1E6), (int) (75.7669383 * 1E6));
Reason: your (int) cast results in 26 and 75 which should be somewhere near the north pole. Check the documentation:
Constructs a GeoPoint with the given latitude and longitude, measured in microdegrees (degrees * 1E6).
I used this code to resolve my problem.
double mLatitude = 26.863966000000000;
double mLongtitude = 75.766938300000000;
GeoPoint point = new GeoPoint((int) (mLatitude * 1E6),(int) (mLongtitude * 1E6));
Now its working fine.
and answer of WarrenFaith is also correct.
The issue is with your point. Try these points.
GeoPoint point = new GeoPoint((int) 19240000,(int) -99120000);
I am triyng to add multiple marker on google map. Here is my code section
public class GoogleMap extends MapView
{
MapController mc;
MapView mapView;
GeoPoint p;
#Override
public void onCreate(Bundle savedInstanceState)
{
....
double lat = Double.parseDouble(bundle.getString("paramLat"));
double lng = Double.parseDouble(bundle.getString("paramLong"));
mc = mapView.getController();
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();
}
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.pushpin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
I have two question here. When i tried to add only one marker, it works but draw method is invoked many times. But why? and when is it invoked?
The second question is how can i add new marker. I created second geoPoint named p2 and after that what should i do? Thank you very much.
I have implemented the multiple markers in my project. Here is the sample code; some things that you need to change is the marker image, the length (number of marker you want define in the for loop). Hope this will help!!!
public class ShowMapActivity extends MapActivity{
private MapController mapControll;
private GeoPoint geoPoint=null;
private MapView mapview;
private MyItemizedOverlay userPicOverlay;
private MyItemizedOverlay nearPicOverlay;
private Drawable userPic,atmPic;
private OverlayItem nearatms[] = new OverlayItem[50];
public static Context context;
#Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
context = getApplicationContext();
setContentView(R.layout.your layout xml);
showMap();
}
public void showMap() {
// TODO Auto-generated method stub
try {
geoPoint = new GeoPoint((int)(latitude * 1E6),(int)(longitude * 1E6));
mapview = (MapView)findViewById(R.id.mapview);
mapControll= mapview.getController();
mapview.setBuiltInZoomControls(true);
mapview.setStreetView(true);
mapControll.setZoom(16);
mapControll.animateTo(geoPoint);
userPic = this.getResources().getDrawable(R.drawable.your pic);
userPicOverlay = new MyItemizedOverlay(userPic);
OverlayItem overlayItem = new OverlayItem(geoPoint, "I'm Here!!!", null);
userPicOverlay.addOverlay(overlayItem);
mapview.getOverlays().add(userPicOverlay);
atmPic = this.getResources().getDrawable(R.drawable.your pic);
nearPicOverlay = new MyItemizedOverlay(atmPic);
for (int i = 0; i < define your length here; i++) {
nearatms[i] = new OverlayItem(new GeoPoint((int)((latitude) * 1E6)),(int)(longitude) * 1E6)),"Name", null);//just check the brackets i just made change here so....
nearPicOverlay.addOverlay(nearatms[i]);
}
mapview.getOverlays().add(nearPicOverlay);
//Added symbols will be displayed when map is redrawn so force redraw now
mapview.postInvalidate();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
Itemized Class for placing the marker
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> myOverlays ;
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
myOverlays = new ArrayList<OverlayItem>();
populate();
}
public void addOverlay(OverlayItem overlay){
myOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return myOverlays.get(i);
}
// Removes overlay item i
public void removeItem(int i){
myOverlays.remove(i);
populate();
}
// Returns present number of items in list
#Override
public int size() {
return myOverlays.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
myOverlays.add(overlayItem);
populate();
}
public void addOverlayItem(int lat, int lon, String title) {
try {
GeoPoint point = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(point, title, null);
addOverlayItem(overlayItem);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
#Override
protected boolean onTap(int index) {
// TODO Auto-generated method stub
String title = myOverlays.get(index).getTitle();
Toast.makeText(ShowMapActivity.context, title, Toast.LENGTH_LONG).show();
return super.onTap(index);
}
}
To prevent the multiple drawing you need a cache. This is a bug in the draw method of MapOverlay
To add multiple markers you have to use ItemizedOverlay. This may help you.
You should follow the Android Map View tutorial on the developers site.
Part 2 has the section for building an Overlay.
http://developer.android.com/resources/tutorials/views/hello-mapview.html
Minimal work should be done in the Draw method; it is called a lot including everytime the map is moved/zoomed/"invalidated"
Your going to want to start with an ItemizedOverlay which is an array of points. You can find the documentation here http://code.google.com/android/add-ons/google-apis/reference/index.html . Then your going to want to invoke the ItemizedOverlay.draw() method which will draw all the points within it based on their position. Hope this helps.