OnTap on a PinPoint in MapView - android

I have a mapview where I add pins.
Here is the CustomPinPoint class:
public class CustomPinpoint 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);
c = 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();
}
}
and this is the way I add pins :
Touchy t = new Touchy();
overlayList = map.getOverlays();
overlayList.add(t);
compass = new MyLocationOverlay(Map.this, map);
overlayList.add(compass);
controller = map.getController();
OverlayItem overlayItem = new OverlayItem(ourLocation, "What's up", "2nd String");
CustomPinpoint custom = new CustomPinpoint(d, Map.this);
custom.insertPinpoint(overlayItem);
overlayList.add(custom);
Everything is ok but I do not know how I could make a balloon (or another class) appear when I tap the pin. I have a database with description for every pin so I would need to appear the description for every click on a pin.

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..,.

How can i change an icon to a different icon in googlemaps

I would like to know is how can I add various different icons to google maps? at the moment i can add one, but want to add different ones selected from a list or whatever to place on the map.
here is my code for MainActivity:
public class MainActivity extends MapActivity implements LocationListener {
MapView map;
long start;
long stop;
MyLocationOverlay compass;
MapController controller;
int x, y;
GeoPoint touchedPoint;
Drawable d;
List<Overlay> overlayList;
LocationManager lm;
String towers;
int lat;
int longi;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = (MapView) findViewById(R.id.mapview);
map.setBuiltInZoomControls(true);
Touchy t = new Touchy();
overlayList = map.getOverlays();
overlayList.add(t);
compass = new MyLocationOverlay(MainActivity.this, map);
overlayList.add(compass);
controller = map.getController();
GeoPoint point = new GeoPoint((int)(-24.0110 * 1E6), (int)(31.4850 * 1E6));
controller.animateTo(point);
controller.setZoom(10);
d = getResources().getDrawable(R.drawable.icon);
//Placing PinPoint at location
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
towers = lm.getBestProvider(crit, false);
Location location = lm.getLastKnownLocation(towers);
if (location != null){
lat = (int) (location.getLatitude() *1e6);
longi = (int) (location.getLongitude() *1E6);
GeoPoint ourLocation = new GeoPoint(lat, longi);
OverlayItem overlayItem = new OverlayItem(ourLocation, "What's Up", "2nd String");
CustomPinPoint custom = new CustomPinPoint(d, MainActivity.this);
custom.insertPinpoint(overlayItem);
overlayList.add(custom);
}else{
Toast.makeText(MainActivity.this, "Couldn't Get Provider", Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
compass.disableCompass();
super.onPause();
lm.removeUpdates(this);
}
#Override
protected void onResume() {
compass.enableCompass();
// TODO Auto-generated method stub
super.onResume();
lm.requestLocationUpdates(towers, 500, 1, this);
}
#Override
protected boolean isRouteDisplayed() {
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(MainActivity.this).create();
alert.setTitle("Pick an Option");
alert.setMessage("Option has been Picked");
alert.setButton(DialogInterface.BUTTON_POSITIVE, "Place a pinpoint", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//TODO Auto=generated method stub
OverlayItem overlayItem = new OverlayItem(touchedPoint, "What's Up", "2nd String");
CustomPinPoint custom = new CustomPinPoint(d, MainActivity.this);
custom.insertPinpoint(overlayItem);
overlayList.add(custom);
}
});
alert.setButton(DialogInterface.BUTTON_NEUTRAL, "Address", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
//TODO Auto=generated method stub
Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault());
try{
List<Address> address = geocoder.getFromLocation(touchedPoint.getLatitudeE6() / 1E6, touchedPoint.getLongitudeE6() / 1E6, 1);
if (address.size() > 0){
String display = "";
for (int i = 0; i<address.get(0).getMaxAddressLineIndex(); i++){
display += address.get(0).getAddressLine(i) + "\n";
}
Toast t = Toast.makeText(getBaseContext(), display, Toast.LENGTH_LONG);
t.show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
}
});
alert.setButton(DialogInterface.BUTTON_NEGATIVE, "Toggle View", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//TODO Auto=generated method stub
if (map.isSatellite()){
map.setSatellite(false);
}else{
map.setSatellite(true);
}
}
});
alert.show();
{
return true;
}
}
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, "What's Up", "2nd String");
CustomPinPoint custom = new CustomPinPoint(d, MainActivity.this);
custom.insertPinpoint(overlayItem);
overlayList.add(custom);
}
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
}
}
And for my CustomPinPoint class:
import java.util.ArrayList;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class CustomPinPoint 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) {
this(m);
c = context;
// TODO Auto-generated constructor stub
}
#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();
}
}
If someone can point me in the right direction or have a tutorial on hoe to do it would be really helpfull, as i am completey stump from this point on-wards!
Thanks.
You can use this library – Android MapView Balloons. This project provides an easy way to annotate map overlay items with a simple information balloon when using the Android Maps.
Edit: I see in your CustomPinPoint class you do not have the createItem programmed. Technically, your "CustomPinPoint" class is an overlay class; it's a single layer with multiple pinpoints. You create the layer, and add points to that layer.
Use this:
#Override
protected void createItem(Object o, Drawable d) {
OverlayItem i = new OverlayItem(<Geopoint location>,<name>,<title>);
i.setMarker(d);
pinpoints.add(i);
}
This adds a pinpoint to your layer, with a custom marker. You should populate() the layer as soon as you're done adding all the icons.
So:
CustomPinPoint pinpoint = new CustomPinPoint();
[...]
In your Map activity
pinpoint.createItem(o, d); //Where o is the object info you want the marker to be about, and d is the custom drawable.
[...]
In your Map activity
map.getOverlays(pinpoint);
Something like that. I'm really bad at explaining things, but I hope this makes sense.

Getting user current location on google mapview

Hi I have been trying to get and pinpoint the location of the phone (user current location) however I have no luck. I hope somebody can help me with the code.
Just to add some more, I am receiving a "Couldn't get connection factory client" error. Does this have something to do with the ability to return the user location?
If it helps I have been following the tutorial by thenewboston.
Or does this have something to do with my API key? But I can receive the map properly, but cannot pinpoint the user's current location
The main code:
public class Map extends MapActivity implements LocationListener{
MapView map;
long start;
long stop;
MyLocationOverlay compass;
MapController controller;
int x,y;
GeoPoint touchedPoint;
Drawable d;
List<Overlay> overlayList;
LocationManager lm;
LocationListener ll;
String towers;
int lat = 0;
int longi = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
map = (MapView)findViewById(R.id.mapview);
map.setBuiltInZoomControls(true);
touch t = new touch();
overlayList = map.getOverlays();
overlayList.add(t);
compass = new MyLocationOverlay(Map.this, map);
overlayList.add(compass);
controller = map.getController();
GeoPoint point = new GeoPoint(832332,4121093);
controller.animateTo(point);
controller.setZoom(8);
d = getResources().getDrawable(R.drawable.pointer);
//placing location
lm =(LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
towers = lm.getBestProvider(crit, false);
Location loc = lm.getLastKnownLocation(towers);
if(loc != null){
lat = (int) (loc.getLatitude() *1E6);
longi = (int)(loc.getLongitude()*1E6);
GeoPoint location = new GeoPoint(lat, longi);
OverlayItem overlayitem = new OverlayItem(location , "wassup", "2nd string");
CustomPinPoint custom = new CustomPinPoint(d, Map.this);
custom.insertPinPoint(overlayitem);
overlayList.add(custom);
}else{
Toast.makeText(Map.this, "Location is null", Toast.LENGTH_LONG).show();
lm.requestLocationUpdates(towers, 1500, 1, this);
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
compass.disableCompass();
super.onPause();
lm.removeUpdates(this);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
compass.enableCompass();
super.onResume();
lm.requestLocationUpdates(towers, 500, 1, this);
}
I think you can ignore the alertdialog
public class touch 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 >1200){
AlertDialog alert = new AlertDialog.Builder(Map.this).create();
alert.setTitle("Option");
alert.setMessage("Pick the option");
alert.setCanceledOnTouchOutside(true);
alert.setButton("Place pointer pinpoint",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
OverlayItem overlayitem = new OverlayItem(touchedPoint , "wassup", "2nd string");
CustomPinPoint custom = new CustomPinPoint(d, Map.this);
custom.insertPinPoint(overlayitem);
overlayList.add(custom);
}
});
alert.setButton2("Get address",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault());
try{
List<Address> address = geocoder.getFromLocation(touchedPoint.getLatitudeE6()
/1E6, touchedPoint.getLongitudeE6() /1E6, 1);
if (address.size()>0){
String display = "";
for(int i=0 ; i<address.get(0).getMaxAddressLineIndex(); i++){
display +=address.get(0).getAddressLine(i) + "\n";
}
Toast t = Toast.makeText(getBaseContext(), display, Toast.LENGTH_LONG);
t.show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
}
});
alert.setButton3("Toggle View",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if(map.isSatellite()){
map.setSatellite(false);
map.setStreetView(true);
}else{
map.setStreetView(false);
map.setSatellite(true);
}
}
});
alert.show();
return true;
}
return false;
}
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_map, menu);
return true;
}
#Override
public void onLocationChanged(Location l) {
// TODO Auto-generated method stub
lat = (int)(l.getLatitude()*1E6);
longi = (int)(l.getLongitude()*1E6);
GeoPoint location = new GeoPoint(lat, longi);
OverlayItem overlayitem = new OverlayItem(location , "wassup", "2nd string");
CustomPinPoint custom = new CustomPinPoint(d, Map.this);
custom.insertPinPoint(overlayitem);
overlayList.add(custom);
}
#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
}
}
The CustomPinPoint code :
public class CustomPinPoint extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();
private Context c;
// tesst google code
public CustomPinPoint(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public CustomPinPoint(Drawable defaultMarker, Context context) {
// TODO Auto-generated constructor stub
this(defaultMarker); //original code
}
#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(); // original code
// test code
populate();
}
#Override
protected boolean onTap(int index){
OverlayItem item = pinpoints.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(c);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlay) {
// TODO Auto-generated method stub
pinpoints.add(overlay);
populate();
}
}
XML code:
<com.google.android.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mapview"
android:enabled="true"
android:clickable="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0t9rHfGlUPgisa8P9MRAsSxhRXTVCL0XJog7uiA"
/>
Here is the example how to do that easily, try to implement this logic into your project:
public class MyMapActivity extends MapActivity {
private MapView map;
private MapController controller;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mymap);
initMapView();
initMyLocation();
map.invalidate();
}
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(); // does not work in emulator
overlay.runOnFirstFix(new Runnable() {
public void run() {
// Zoom in to current location
controller.setZoom(15);
controller.animateTo(overlay.getMyLocation());
}});
}
}
Well this is embarrassing, it turns out that my code is correct and the problem was that the device's GPS wasn't turned on. Well I hope my code can help anyone in need. Cheers!

onClick listener on a geopoint android and google maps

This is my code:
geopoint = new GeoPoint((int) (1.352566007* 1E6), (int) (103.78921587* 1E6));
mapView.getOverlays().add( new DrawableMapOverlay(this,geopoint,R.drawable.pushpin, "test"));
geopoint.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
System.out.println("Clicked");
}
});
and i am getting this error:
The method setOnClickListener(new View.OnClickListener(){}) is undefined for the type GeoPoint
Isn't on click listener working on a geopoint??
You should extend ItemizedOverlay<> class and overide onTap() method.
Something like this:
public class MapOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlay = new ArrayList<OverlayItem>();
Context mContext;
public MapOverlay(Drawable itemMarker) {
super(boundCenterBottom(itemMarker));
// TODO Auto-generated constructor stub
}
public void addItem(OverlayItem item){
mapOverlay.add(item);
populate();
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mapOverlay.get(i);
}
#Override
public int size() {
// TODO Auto-generated method stub
return mapOverlay.size();
}
public MapOverlay(Drawable itemMarker, Context context){
super(boundCenterBottom(itemMarker));
mContext = context;
}
#Override
protected boolean onTap(int index) {
// TODO Auto-generated method stub
final OverlayItem item = mapOverlay.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
In the activity adding items:
Drawable voltaIcon = this.getResources().getDrawable(R.drawable.arch);
MapOverlay volte = new MapOverlay(voltaIcon, this);
GeoPoint p_Volta = new GeoPoint(45373024, 14348799);
OverlayItem volta = new OverlayItem(p_Volta,
getResources().getStringArray(R.array.volta)[0],
getResources().getStringArray(R.array.volta)[1]);
volte.addItem(volta);
You can only set an onClickListener on an object which is a subclass of a View. Basically this means something like a Button or a TextView or anything which has visibility on the screen, so that you could click or touch it. A GeoPoint has no visibility in itself.

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.

Categories

Resources