Google maps android api v1 mapview tutorial crashes - android

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

Related

Plot latitudes and longitudes from database on Google maps v1

I am developing an application where I want to plot latitudes and longitudes from my database on Maps. In my database schema I have three columns:
Name | Latitude | Longitude
I can retrieve the data from SQLite in an ArrayList, but the problem I am facing is how should I go about visualizing these values on a Google Map along with the name of the location as stored in the database? Also, how can I draw a path from the start geopoint to the end geopoint.
I got my code working by creating an itemized overlay class
package com.rahul.besttracker;
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 = new ArrayList<OverlayItem>();
Context mContext;
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) {
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;
}
}
I created an instance of this in my main class and every-time i need to add an item to overlay i called the add overlay function. finally i plotted the overlay on map

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();
}
}

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.

Google Map View tutorial - Overlays

I followed Google's Map View tutorial from this link http://developer.android.com/resources/tutorials/views/hello-mapview.html
I've done the first about Creating Map activity, but have problems with Adding Overlays. Here is the source code:
package rs.iz.stevy.wifi;
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 Overlay extends ItemizedOverlay {
public Overlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
Context mContext;
mContext = context;
}
private ArrayList<OverlayItem>mOverlays= new ArrayList<OverlayItem>();
public Overlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
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;
}
}
Error appear at this line:
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
Eclipse shows mContext cannot be resolved to variable.
Here is the Activity class:
rs.iz.stevy.wifi;
import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
public class WiFiKupacicaActivity extends MapActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapa= (MapView) findViewById(R.id.Mapa1);
mapa.setBuiltInZoomControls(true);
mapa.setSatellite(true);
GeoPoint initGeoPoint = mapa.getMapCenter();
List<Overlay> mapOverlays = mapa.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.kupacica);
Overlay itemizedoverlay = new Overlay (drawable);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
Eclipse show error Cannot instantiate Overlay at this line:
Overlay itemizedoverlay = new Overlay (drawable);
If you see here any error that can be easily fixed please answer cause this are my first attempts to write an android app.
The first error is because you defined mContext as a local variable in the constructor instead of as an instance variable. To remedy, move the Context mContext; line out of the constructor (put it right before this line public Overlay(Drawable defaultMarker, Context context) {). You want to declare instance variables outside of any method, but inside the class itself.
Your second error occurs because Overlay is an Abstract class. You cannot instantiate an abstract class with "new". What you want to do here is instantiate your Overlay subclass. In your import statements, you're importing com.google.android.maps.Overlay so it thinks you're instantiating that rather than your subclass. To fix, replace
Overlay itemizedoverlay = new Overlay (drawable);
with
rs.iz.stevy.wifi.Overlay itemizedoverlay = new rs.iz.stevy.wifi.Overlay (drawable);
In practice, you should avoid giving a class the name of an abstract class already in its own hierarchy (to avoid import errors like this).

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