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.
Related
I am doing the MapView tutorial found here: https://developers.google.com/maps/documentation/android/v1/hello-mapview
The problem is that it keeps crashing. Here's the code:
package com.example.googlemapstest;
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.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.Menu;
public class MainActivity extends MapActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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);
GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
drawable.setBounds(0,0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
overlayitem.setMarker(drawable);
itemizedoverlay.addOverlay(overlayitem, drawable);
mapOverlays.add(itemizedoverlay);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And the HelloItemizedOverlay class:
package com.example.googlemapstest;
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;
public class HelloItemizedOverlay extends ItemizedOverlay
{
private ArrayList<OverlayItem> mOverlays;
Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
mOverlays = new ArrayList<OverlayItem>();
populate();
}
public void addOverlay(OverlayItem overlay, Drawable drawable) {
overlay.setMarker(drawable);
mOverlays.add(overlay);
populate();
}
#Override
public int size() {
return mOverlays.size();
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
mOverlays = new ArrayList<OverlayItem>();
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;
}
#Override
protected OverlayItem createItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
}
The exception I keep getting is
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlemapstest/com.example.googlemapstest.MainActivity} : java.lang.NullPointerException
The crash happens at the populate() line in itemizedoverlay.addOverlay(overlayitem, drawable);
Now, initially I found that the mMarker variable of the OverlayItem I was adding was null, and added two lines to manually set it. However, the program still kept crashing!
I then found this question: Problem with crash with ItemizedOverlay which seemed to be dealing with the same error.
I did as recommended in the best answer there, but still no luck.
I believe I've vigourously combed this, and can't imagine where the null error is coming from. Any help is much appreciated.
I'm pretty sure you shouldn't be returning null in createItem(int index), meaning you should change the following method in your HelloItemizedOverlay class:
#Override
protected OverlayItem createItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
The most basic implementation should return the OverlayItem object appropriate for the given index. Quite probably, you'll just want to return whatever object is located in the list of OverlayItems at the same index. For example:
#Override
protected OverlayItem createItem(int index) {
return mOverlays.get(index);
}
That will only make sense if you also populate that list somewhere. Also, to avoid confusion, you should probably rename the variable to mOverlayItems, since that is what it contains (as opposed to 'overlays').
More importantly, as #ianhanniballake already mentioned: the Android Maps SDK v1 has been deprecated in favour of v2. This means that from March 3rd, 2013 you will no longer be able to request an API key for v1. Better hurry up and generate a key now, if you haven't done so yet, or just migrate to v2 now, which probably wouldn't be a bad move.
Version 1 of the Google Maps Android API as been officially deprecated as of December 3rd, 2012
Go with Google Map API V2 and make your life easy
https://developers.google.com/maps/documentation/android/
Happy coding
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();
}
}
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"
/>
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)
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);