I have code for displaying two locations on the map with markers but it shows the markers only, and instead of the map it shows a blue screen only. I am running on mobile, not emulator.
My code:
public class HelloGoogleMaps3 extends MapActivity {
private MapView map=null;
private MyLocationOverlay me=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map=(MapView)findViewById(R.id.map);
map.getController().setCenter(getPoint
(40.76793169992044,-173.98180484771729));
map.getController().setZoom(17);
map.setBuiltInZoomControls(true);
Drawable marker=getResources().getDrawable(R.drawable.marker);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
map.getOverlays().add(new SitesOverlay(marker));
me=new MyLocationOverlay(this, map);
map.getOverlays().add(me);
}
#Override
public void onResume() {
super.onResume();
me.enableCompass();
}
#Override
public void onPause() {
super.onPause();
me.disableCompass();
}
#Override
protected boolean isRouteDisplayed() {
return(false);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_S) {
map.setSatellite(!map.isSatellite());
return(true);
}
else if (keyCode == KeyEvent.KEYCODE_Z) {
map.displayZoomControls(true);
return(true);
}
return(super.onKeyDown(keyCode, event));
}
private GeoPoint getPoint(double lat, double lon) {
return(new GeoPoint((int)(lat*1000000.0),
(int)(lon*1000000.0)));
}
private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items=new ArrayList<OverlayItem>();
private Drawable marker=null;
public SitesOverlay(Drawable marker) {
super(marker);
this.marker=marker;
items.add(new OverlayItem(getPoint
(40.748963847316034,-173.96807193756104),
"UN", "United Nations"));
items.add(new OverlayItem(getPoint
(40.76866299974387,-173.98268461227417),
"Lincoln Center",
"Home of Jazz at Lincoln Center"));
items.add(new OverlayItem(getPoint
(40.765136435316755,-173.97989511489868),
"Carnegie Hall",
"Where you go with practice, practice, practice"));
items.add(new OverlayItem(getPoint
(40.70686417491799,-174.01572942733765),
"The Downtown Club",
"Original home of the Heisman Trophy"));
populate();
}
#Override
protected OverlayItem createItem(int i) {
return(items.get(i));
}
#Override
public void draw(Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
}
#Override
protected boolean onTap(int i) {
Toast.makeText(HelloGoogleMaps3.this,
items.get(i).getSnippet(),
Toast.LENGTH_SHORT).show();
return(true);
}
#Override
public int size() {
return(items.size());
}
}
}
What did I do wrong?
That could mean either of the following two things:
a. Your API Key is incorrect
b. Your coordinates are set to (0, 0) and the map marker is pointing in the ocean. Try zooming the map in. You'd probably see the land. :)
This can happen if you get a location from the location manager and try and use the coordinates without first converting them:
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// location.getLatitude() is, say, 56.8751841974
// location.getLongitude() is, say, -3.9548756201
GeoPoint mapPoint = new GeoPoint((int)(location.getLatitude() * 1E6), (int)(location.getLongitude() * 1E6));
Basically, you need to multiply the values by 1 million to map to the correct coordinates.
You can customize the MapView color with different sets of coordinates, for example :
40.76793169992044,-173.98180484771729 is blue, as you experienced
-77.199943,11.913063 is light gray
75.913515,-30.443115 is green
43.318418,11.331644 is Siena
Related
Now I can show the map and markers that are defined in the program, but I need to add markers to touched location.
I think I know the way to do this, which is:
1 To set something like onTouchEventListener to the MapView;
2 Get the position information from Listener;
3 Set the marker according to the position information.
But I am too new to this to find out how to write code to achieve it, especially the step 1. I mean I don't understand how to use the solution like Add marker on touched location using google map in Android .My problem is more fundamental. I dont know how to set eventListener for my MapView and which eventListener should I use. https://developer.mapquest.com/content/mobile/android/documentation/api/com/mapquest/android/maps/Overlay.OverlayTouchEventListener.html This OverlayTouchEventListener seems to be the one but cant find an useful guide for it.
So could some one tell me how to do this in detail?
Eg.How to set the listener and which listener to choose?
Try this...
1. Create interface MyGeoPointListener.java
public interface MyGeoPointListener {
public void GetGeoPoint(GeoPoint geopoint);
}
2. Create Overlay class:
import com.mapquest.android.maps.ItemizedOverlay;
import com.mapquest.android.maps.OverlayItem;
class DynamicMarkerOverlay extends ItemizedOverlay<OverlayItem> {
private boolean isPinch = false;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private MyGeoPointListener geoPointListener;
public DynamicMarkerOverlay(Drawable defaultMarker,
MyGeoPointListener geoPointListener) {
super(boundCenterBottom(defaultMarker));
this.geoPointListener = geoPointListener;
}
public void addOverlay(OverlayItem overlay) {
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 map) {
if (isPinch) {
return false;
} else {
if (geoPoint != null) {
if (null != geoPointListener) {
geoPointListener.GetGeoPoint(geoPoint);
}
return true;
} else {
if (null != geoPointListener) {
geoPointListener.GetGeoPoint(null);
}
return false;
}
}
}
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
int fingers = event.getPointerCount();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isPinch = false; // Touch DOWN, don't know if it's a pinch yet
}
if (event.getAction() == MotionEvent.ACTION_MOVE && fingers == 2) {
isPinch = true; // Two fingers, def a pinch
}
return super.onTouchEvent(event, mapView);
}
}
3. Implement MyGeoPointListener in activity
like,
public class MainActivity extends MapActivity implements MyGeoPointListener {
4. Override MyGeoPointListener:
.....
#Override
public void GetGeoPoint(GeoPoint geopoint) {
if (null != geopoint) {
String msg = "Lat: " + geopoint.getLatitudeE6() / 1E6 + " - "
+ "Lon: " + geopoint.getLongitudeE6() / 1E6;
Toast toast = Toast.makeText(MyLocationMap.this, msg,
Toast.LENGTH_SHORT);
toast.show();
List<Overlay> mapOverlays = myMap.getOverlays();
OverlayItem overlayitem = new OverlayItem(geopoint, "address_name",
"address");
Drawable icon = getResources().getDrawable(
R.drawable.location_marker);
DynamicMarkerOverlay customoverlay = new DynamicMarkerOverlay(icon,
this);
customoverlay.addOverlay(overlayitem);
mapOverlays.add(customoverlay);
myMap.getController().animateTo(geopoint);
}
}
.....
5. In OnCreate()
......
public MapView myMap;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_map_layout);
.......
.......
myMap = (MapView) findViewById(R.id.map);
Drawable marker = getResources().getDrawable(R.drawable.ic_launcher);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
myMap.getOverlays().add(new DynamicMarkerOverlay(marker, this));
......
}
6. Result:
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.
It´s possible that overlays on a map could receive focus from DPAD/Tab?
I have two fragments, a listview and mapview, I want to get focus from the drawable of the overlay, but I´m not sure if it´s possible...
Yes, you can move from one overlay item on MapView to other but there are few things which you should consider.
If you want your MapView to steer according to the Dpad directions while pressing up/down/left on Dpad, then your map will go up/down/left direction showing the map and you wont able to Dpad on overlay items since MapView is having the focus.
But if you want overlay items to be focused, then you have to manually define which overlay item it should focus on which D-Pad direction using setFocus, nextFocus and getFocus methods of ItemizedOverlay class.
Also you said you have listview and MapView in your activity and in order to get the focus back to listview or any other view which is outside MapView will also have to be done programmatically and it could be little tricky.
You can use StateListDrawable to define the different states on overlaid drawable for focus, pressed and default state.
Hope this answers your query.
I created a sample activity below. Most of the code comes from the MapView tutorial found here: http://developer.android.com/resources/tutorials/views/hello-mapview.html
The 'focus code' is in the onKeyDown() method. When TAB is pressed, focus is shifted to the next overlay. When ENTER is pressed, it shows a Toast, but that's where you can display your content.
The setFocus() method was found in the documentation for ItemizedOverlay found here: https://developers.google.com/maps/documentation/android/reference/
Hope this works.
public class OverlayFocusExampleActivity extends MapActivity {
private HelloItemizedOverlay itemizedoverlay;
private MapView mapView;
private MapController mapController;
private int currentOverlayIndex;
/*
* This entire method comes from the MapView tutorial.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
List<Overlay> mapOverlays = mapView.getOverlays();
// overlay_draw is a selector that specifies a different image for state_focused
Drawable drawable = this.getResources().getDrawable(R.drawable.overlay_draw);
itemizedoverlay = new HelloItemizedOverlay(drawable, this);
GeoPoint point = new GeoPoint(19240000, -99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
GeoPoint point2 = new GeoPoint(35410000, 139460000);
OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa!", "I'm in Japan!");
itemizedoverlay.addOverlay(overlayitem2);
mapOverlays.add(itemizedoverlay);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
OverlayItem overlay;
switch (keyCode) {
case KeyEvent.KEYCODE_TAB:
// Retrieve next overlay
currentOverlayIndex = (currentOverlayIndex + 1) % itemizedoverlay.size();
overlay = itemizedoverlay.getOverlayItem(currentOverlayIndex);
itemizedoverlay.setFocus(overlay);
// Since setFocus() doesn't center the map, we do it ourselves
mapController.animateTo(overlay.getPoint());
return true;
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
overlay = itemizedoverlay.getFocus();
if (overlay != null) {
// Perform associated action
// Stub
Toast.makeText(this, overlay.getSnippet(), Toast.LENGTH_SHORT).show();
return true;
}
default:
return false;
}
}
/*
* This entire class comes from the MapView tutorial except getOverlayItem().
*/
private class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
/*
* Not in MapView tutorial. Added for focusability.
*/
public OverlayItem getOverlayItem(int index) {
return mOverlays.get(index);
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
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;
}
}
}
Once again I seek the wisdom of my betters...
I'm working on an Android app that uses Google Maps and programmatically puts markers on the map based on status info from a file read from the web.
The problem I have is that each marker is drawn twice. Once in the right spot and once a little below (the top line of the second marker is in line with the bottom of the first marker).
private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items=new ArrayList<OverlayItem>();
private Drawable marker=null;
public SitesOverlay(Drawable marker) {
super(marker);
this.marker=marker;
try {
data = getData();
} catch (MalformedURLException e) {
//
}
if (!data.equals("")) {
String[] msg = data.split(NEWLINE);
for (Integer i = 0; i < msg.length; i++) {
items.add(new OverlayItem(
getPoint(lat.get(suburb), lng.get(suburb)),
msg[i], msg[i]));
}
}
populate();
map.postInvalidate();
}
#Override
protected OverlayItem createItem(int i) {
return(items.get(i));
}
#Override
public void draw(Canvas canvas, MapView mapView,
boolean shadow) {
super.draw(canvas, mapView, false);
//boundCenterBottom(marker);
}
#Override
protected boolean onTap(int i) {
Toast.makeText(getBaseContext(),
items.get(i).getSnippet(),
Toast.LENGTH_LONG).show();
map.getController().setZoom(16 + zoomModifier);
return(true);
}
#Override
public int size() {
return(items.size());
}
}
However, the second marker only becomes evident if I uncomment the boundCenterBottom(marker) in the draw function.
I've been trying to workout what is placing the second marker for days. Can anyone point me in the right direction?
Thanks
EDIT: I forgot to mention that if I touch the screen the marker that is not boundCenterBottom disappears.
Ok, finally worked out what was wrong.
Change
public SitesOverlay(Drawable marker) {
super(marker);
this.marker=marker;
to
public SitesOverlay(Drawable marker) {
super(marker);
boundCenterBottom(marker);
Is there any way to put a pushpin on an android map and when it's touched displays a popup with some extra info?
You need to extend this http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/ItemizedOverlay.html.
public class CustomOverlay extends ItemizedOverlay<OverlayItem> {
private Context context;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public CustomOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
this.context = context;
//after adding things to the overlay, call these:
setLastFocusedIndex(-1);
populate();
}
#Override
protected boolean onTap(int index) {
//called when an item is tapped
return true;
}
#Override
public boolean onTap (final GeoPoint p, final MapView mapV) {
boolean tapped = super.onTap(p, mapV);
if(!tapped){
//you can use this to check for other taps on the custom elements you are drawing
}
return true;
}
#Override
public void draw(Canvas canvas, MapView mapV, boolean shadow){
if(!shadow)
// if you have a custom image you may not want the shadow to be drawn
super.draw(canvas,mapV,shadow);
if(selected != null) {
// selected just means that something was clicked
// it isn't defined in this example
Projection projection = mapV.getProjection();
Point drawPoint = projection.toPixels(selected.getPoint(), null);
//get coordinates so you can do your drawing code afterward
}
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
}
This is a very rough sketch of what you need to do. Hope this helps.