How do I set streetView in my mapview - android

I an working on an android project whereby i need to set my map to zoom in and show a more detailed view of my map, like the streets of where i want my coordinates to triangulate. The problem is in Mapview class, setStreetView is deprecated, wat is the alternative?
This is how setStreetView looks when i use it.
package com.HelloMapView;
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.Overlay;
import com.google.android.maps.OverlayItem;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.LinearLayout;
public class HelloMapView extends MapActivity {
MapView mapview;
LinearLayout linearlayout;
List<Overlay> mapOverlay;
Drawable drawable;
HelloItemizedOverlay itemizedOverlay;
#SuppressWarnings("deprecation")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapview=(MapView)findViewById(R.id.mapview);
mapview.setBuiltInZoomControls(true);
mapview.setStreetView(true);
mapOverlay=mapview.getOverlays();
drawable=this.getResources().getDrawable(R.drawable.androidmarker);
itemizedOverlay=new HelloItemizedOverlay(drawable);
GeoPoint geoPoint=new GeoPoint(19240000,-99120000);
OverlayItem overlayitem=new OverlayItem(geoPoint,"","");
itemizedOverlay.addoverlay(overlayitem);
mapOverlay.add(itemizedOverlay);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
this does not work it only shows square boxes with no map at all

Use setStreetView(boolean) of MapView.

myMapView.setStreetView(true);

public class ABC extends MapActivity {
private static MapController myMapController = null;
private static GeoPoint geoPoint = new GeoPoint
(
(int) (25.24243399999999 * 1E6), (int) (55.30611937301637 * 1E6));
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_us_layout);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.displayZoomControls(false);
mapView.setBuiltInZoomControls(true);
mapView.setFocusable(true);
myMapController = mapView.getController();
myMapController.animateTo(geoPoint);
myMapController.setZoom(15);
// mapView.invalidate();
// mapView.setFocusable(true);
myMapController.setCenter(geoPoint);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(
R.drawable.mapmarker);
HelloItemizedOverlay itemizedOverlay = new HelloItemizedOverlay(
drawable, this);
OverlayItem overlayItem = new OverlayItem(geoPoint,
"abc");
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);
//mapView.setSatellite(true);
**mapView.setStreetView(true);**
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList mOverlays = new ArrayList();
private Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
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
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;
}
}
}
Hello,,,
I have posted the working code for what u desire...
If your map isn't displaying anything. Your api key is not signed properly..Please sign it again and provide it in mapView.
If you want streetView of map use:
mapView.setStreetView(true)
If you want satelliteView of map use:
mapView.setSatellite(true)

Related

placing a pinpoint marker to my Google maps

Ok I've asked this before. Got one answer. I'm going to go more in depth on what I'm doing. I'm creating a app for a business that has multiple stores. I have a layout that shows button to each store. click on one of the buttons it takes you to another view with two buttons on it. In this view theres a button that when u click it it calls the store for u, got that working just fine. the other button in the same view when u click it it takes you to Google maps and shows u where the location of the store is. This all works fine. But what i want is a marker to show up on the map as well. I don't need it to do anything but show the customer this is where the store is located. This is my code I'm using--->
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 android.os.Bundle;
public class Main extends MapActivity {
MapController mControl;
GeoPoint GeoP;
MapView navView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
navView = (MapView) findViewById(R.id.navView);
navView.displayZoomControls(true);
navView.setBuiltInZoomControls(true);
navView.setSatellite(true);
navView.getOverlays().add(new MyLocationOverlay(this,navView));
double lat = 40.325874;
double longi = -76.002211;
GeoP = new GeoPoint((int) ( lat *1E6), (int) (longi * 1E6));
mControl = navView.getController();
mControl.animateTo(GeoP);
mControl.setZoom(20);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
#Erik Graul
Check this tutorial , it will help you
http://mobiforge.com/developing/story/using-google-maps-android
check out this hhttp://eagle.phys.utk.edu/guidry/android/mapOverlayDemo.htm
GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY());
Drawable srcdrawable = getApplicationContext().getResources().getDrawable(R.drawable.pin_blue);
CustomItemizedOverlay srcitemizedOverlay = new CustomItemizedOverlay(srcdrawable, getApplicationContext());
OverlayItem srcoverlayitem = new OverlayItem(p, "Hello!", "This is your Location.");
srcitemizedOverlay.addOverlay(srcoverlayitem);
mapView.getOverlays().clear();
mapView.getOverlays().add(srcitemizedOverlay);
mapController.animateTo(srcpoint);
mapController.setZoom(16);
Use the above code in ontouch method also use the below CustomItemizedOverlay.java class
public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private final ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public CustomItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
#Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
#Override
public int size() {
return mapOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
this.populate();
}
}

ItemizedOverlays Android maps not working

I'm trying to add multiple markers on the mapview and i can't make it work, it doesn't
seems to load even the mapview when i comment the method that calls LoadMarkers().
can you tell me what is wrong with my code please??
public class MyATMLocatorActivity extends MapActivity {
/** Called when the activity is first created. */
private MapView mapView;
private MapController myMapController;
private GeoPoint myGeoPoint;
private LocationManager myLocationManager;
private LocationListener myLocationListener;
public static Context context;
private MyItemizedOverlay myItemizedOverlay = null;
private MyLocationOverlay myLocationOverlay = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext();
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
myMapController = mapView.getController();
this.LoadMarkers();
}
this is the method that loads the markers..
private void LoadMArkers() {
mapView.getOverlays().clear();
mapView.postInvalidate();
OverlayItem overlayItem = null;
Drawable myATMPic = getResources().getDrawable(R.drawable.atmicon);
myATMPic.setBounds(0, 0, myATMPic.getIntrinsicWidth(), myATMPic.getIntrinsicHeight());
// Super España
myItemizedOverlay = new MyItemizedOverlay(myATMPic);
myGeoPoint = new GeoPoint((int) (-25.353043), (int) (-57.444495));
overlayItem = new OverlayItem(myGeoPoint, "Supermercado Espana","Capiatá");
myItemizedOverlay.addOverlay(overlayItem);
mapView.getOverlays().add(myItemizedOverlay);
// Martín Ledesma
myItemizedOverlay = new MyItemizedOverlay(myATMPic);
myGeoPoint = new GeoPoint((int) (-25.353974), (int) (-57.445214));
overlayItem = new OverlayItem(myGeoPoint, "Martín Ledesma", "Capiatá");
myItemizedOverlay.addOverlay(overlayItem);
mapView.getOverlays().add(myItemizedOverlay);
mapView.postInvalidate();
}
and this is my ItemizedOverlay() class
public class MyItemizedOverlay extends com.google.android.maps.ItemizedOverlay<OverlayItem> {
private Context mContext;
private ArrayList<OverlayItem> myOverlaysArray = new ArrayList<OverlayItem>();;
private GeoPoint geoPoint = null;
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public MyItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
myOverlaysArray.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return myOverlaysArray.get(i);
}
// Removes overlay item i
public void removeItem(int i) {
myOverlaysArray.remove(i);
populate();
}
// Returns present number of items in list
#Override
public int size() {
return myOverlaysArray.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
myOverlaysArray.add(overlayItem);
populate();
}
public void addOverlayItem(int lat, int lon, String title) {
try {
geoPoint = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(geoPoint, title, null);
addOverlayItem(overlayItem);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
protected boolean onTap(int index) {
OverlayItem item = myOverlaysArray.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
Are you adding the right code to the android manifest.xml?
Add the com.google.android.maps as a child to the application element using a MapView.
<uses-library android:name="com.google.android.maps" />
Add INTERNET as a child element to the element.
<uses-permission android:name="android.permission.INTERNET" />
You don't need to create 2 itemized overlays. Also, you need to multiply all GPS coordinates by 1e6 before casting them to ints. In your previous code, both GPS coordinates will be casted to the same integer pair values and the markers will be placed on the same location on the map(-25,-57) and only the last 1 will be touchable. Also, don't forget to pass the application's context to the itemized overlay.
MyATMLocatorActivity.java
package maps.test;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.LocationListener;
import android.location.LocationManager;
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.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class MyATMLocatorActivity extends MapActivity {
/** Called when the activity is first created. */
private MapView mapView;
private MapController myMapController;
private GeoPoint myGeoPoint;
private GeoPoint myGeoPoint2;
private LocationManager myLocationManager;
private LocationListener myLocationListener;
public static Context context;
private MyItemizedOverlay myItemizedOverlay = null;
private final MyLocationOverlay myLocationOverlay = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext();
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
myMapController = mapView.getController();
this.LoadMarkers();
}
private void LoadMarkers() {
List<Overlay> overlays = mapView.getOverlays();
mapView.postInvalidate();
OverlayItem overlayItem = null;
Drawable myATMPic = getResources().getDrawable(R.drawable.ic_launcher);
myATMPic.setBounds(0, 0, myATMPic.getIntrinsicWidth(), myATMPic.getIntrinsicHeight());
myItemizedOverlay = new MyItemizedOverlay(myATMPic, this);
// Super España
myGeoPoint = new GeoPoint((int) (-25.353043*1e6), (int) (-57.444495*1e6));
overlayItem = new OverlayItem(myGeoPoint, "Supermercado Espana","Capiatá");
myItemizedOverlay.addOverlay(overlayItem);
// Martín Ledesma
myGeoPoint = new GeoPoint((int) (25.353974*1e6), (int) (-57.445214*1e6));
overlayItem = new OverlayItem(myGeoPoint2, "Martín Ledesma", "Capiatá");
myItemizedOverlay.addOverlay(overlayItem);
overlays.add(myItemizedOverlay);
mapView.postInvalidate();
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
MyItemizedOverlay.java
package maps.test;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.OverlayItem;
public class MyItemizedOverlay extends com.google.android.maps.ItemizedOverlay<OverlayItem> {
private Context mContext;
private final ArrayList<OverlayItem> myOverlaysArray = new ArrayList<OverlayItem>();
private GeoPoint geoPoint = null;
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public MyItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
myOverlaysArray.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return myOverlaysArray.get(i);
}
// Removes overlay item i
public void removeItem(int i) {
myOverlaysArray.remove(i);
populate();
}
// Returns present number of items in list
#Override
public int size() {
return myOverlaysArray.size();
}
public void addOverlayItem(OverlayItem overlayItem) {
myOverlaysArray.add(overlayItem);
populate();
}
public void addOverlayItem(int lat, int lon, String title) {
try {
geoPoint = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(geoPoint, title, null);
addOverlayItem(overlayItem);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
protected boolean onTap(int index) {
OverlayItem item = myOverlaysArray.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
I've tested the code in a sample application and here is my android manifest, compare it to your own.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="maps.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".MyATMLocatorActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is my Main.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="Your Maps API Key goes here"
/>

itemized overlay tutorial

I am trying to incorporate a map into an app I am developing, so I am learning how to use them with the ItemizedOverlay functionality. I went through the tutorial on dev-android, and everything went fine except this one line.
public class HelloMapViewActivity extends MapActivity {
#Override
protected boolean isRouteDisplayed()
{
return true;
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable); //this is the error
GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
}
The problem says:
The constructor HelloItemizedOverlay(drawable) is undefined,
would anyone be able to tell me what im doing wrong? As well when I follow what Eclipse tells me to do and put null next to it in the parameters, it clears the problem but does not show up with the drawable over the map.
I think this is a good basic source to learn http://developer.android.com/resources/tutorials/views/hello-mapview.html
Here is an example how my HelloItemizedOverlay was when I was working with Google Maps :
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;
#SuppressWarnings("rawtypes")
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
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;
}
}
HelloitemizedOverlay konstructor also needs the context.

How to add a marker when I touch the map in android ?

I have a map activity which displays the map, I want to add a marker when I touch on the screen ..
This is my Activity ...
package com.adhamenaya.android;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;
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.Overlay;
import com.google.android.maps.OverlayItem;
public class MapApp extends MapActivity {
private MapView mapView;
private MapController mapController;
private LocationManager locationManager;
private Context context;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context=this;
initLayout();
initMap();
}
private void initLayout(){
mapView = (MapView) findViewById(R.id.mapview);
//blueIcon = (Drawable)this.getResources().getDrawable(R.drawable.blueicon);
}
private void initMap(){
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
mapController=mapView.getController();
mapController.setZoom(10);// 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, new GeoUpdateHandler());
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
class GeoUpdateHandler implements LocationListener {
#Override
public void onLocationChanged(Location location) {
int lat=(int)(location.getLatitude()*1E6);
int lng=(int)(location.getLongitude()*1E6);
GeoPoint point=new GeoPoint(lat,lng);
//GeoPoint point = new GeoPoint(19240000,-99120000);
mapController.animateTo(point);
mapController.setCenter(point);
Drawable redIcon = context.getResources().getDrawable(R.drawable.redicon);
List<Overlay> mapOverlays = mapView.getOverlays();
MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(redIcon);
OverlayItem overlayitem = new OverlayItem(point, "Hello !", "I'm here");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
#Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
public boolean onTouchEvent(MotionEvent event){
int x=(int)event.getX();
int y=(int)event.getY();
Toast.makeText(context, x, Toast.LENGTH_LONG).show();
GeoPoint point = mapView.getProjection().fromPixels(x, y);
Drawable redIcon = context.getResources().getDrawable(R.drawable.blueicon);
List<Overlay> mapOverlays = mapView.getOverlays();
MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(redIcon);
OverlayItem overlayitem = new OverlayItem(point, "New Place", "I clicked here !");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
return false;
}
}
}
Edit : This is the class for ItemizedOverlay, where I have implemented onTap() method , but nothing happens ...
package com.adhamenaya.android;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.widget.Toast;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class MyItemizedOverlay extends ItemizedOverlay{
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public MyItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
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
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;
}
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
int x=(int)event.getX();
int y=(int)event.getY();
Toast.makeText(mContext, x, Toast.LENGTH_LONG).show();
return super.onTouchEvent(event, mapView);
}
}
Hello, MapView! tutorial covers showing markers on MapView. In the tutorial markers are added as soon as the activity starts, but you can also add markers at later time, for example, when user touches screen. If you try this approach, you'll likely want to override onTap in your subclass of ItemizedOverlay.
Update:
If you follow the tutorial, you'll have your own subclass of ItemizedOverlay. onTap is one of its methods. Now that I look at the documentation, unfortunately onTap(GeoPoint p, MapView mapView) is not public or protected so you cannot override it.
What you can do, is have another overlay, solely for detecting taps. Instead of subclassing ItemizedOverlay, subclass Overlay.
private class TapOverlay extends Overlay {
public boolean onTap(GeoPoint p, MapView mapView) {
// code that adds item in your ItemizedOverlay
// goes here
return true;
}
}

Android Google Map view returns error when onTap() is used - Alertdialog is supposed to be displayed

I currently have modified 'Google Map View' code (found below) but when the map loads and you click on the item on the map it returns an error.
I believe it has something to do with the null value for mContext but I'm not sure, I would really appreciate it if anyone could help me out with this:
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
Context mContext = null;
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
My Map class:
package testing.map;
import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
public class Mapview extends MapActivity {
/** Called when the activity is first created. */
protected boolean isRouteDisplayed(){
return false;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.jd_sports_logo);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
//Declared longitude/latitude as doubles as GeoPoint only uses 'int' - converted to microdegrees
double latitude = 51.545538;
double longitude = -0.477247;
GeoPoint point = new GeoPoint((int)(latitude * 1e6), (int)(longitude * 1e6));
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
}
My HelloItemizedOverlay class:
package testing.map;
import java.util.ArrayList;
import android.graphics.drawable.Drawable;
import android.app.AlertDialog;
import android.content.Context;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
Context 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);
Context mContext = null;
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
You are missing the Context in the main class.
Add this :
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, mContext);
where mContext = this;
instead of this:
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
Change two places:
1.
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
2.
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
I also found that the HelloGoogleMaps tutorial code failed with a npe when you tap the marker.
If you pass this (from an instance that extends MapActivity) the dialog shows.
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
Constructor required:
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
you can pass context like this, here is the pseudo code:
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(this,drawable);

Categories

Resources