A question about MapViews

Hey,

        I was just wondering how MapViews work in Android...  I was looking through the Android Cookbook by Addison-Wesley and I copied one of their sample programs that was supposed to display a MapView along with the location:

package cookbook.recipes;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class MapsTest extends MapActivity {
LocationManager mLocationManager;
Location mLocation;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.zip);
tv = (TextView) findViewById(R.id.zip_disp);
mLocationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String locationprovider =
mLocationManager.getBestProvider(criteria,true);
mLocation = mLocationManager.getLastKnownLocation(locationprovider);
tv.setText("Last location lat:" + mLocation.getLatitude()+ " long:" + mLocation.getLongitude());
}
@Override
protected boolean isRouteDisplayed() {
// this method is required
return false;
}
}

Open in new window


But, for some reason its not working... I'm sure I copied it verbatim, and to be absolutely sure, I also copy pasted this program as well as the relevant files...

Could anyone help out with this?
errangAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dejan PažinHead of SW DevelopmentCommented:

What is the output of the logcat? There should be some error displayed, that will tell us more.

Did you fix the two basic things for MapViews:

1. Obtain the key: http://code.google.com/intl/sl/android/add-ons/google-apis/mapkey.html

2. Run the emulator with Google Apis.
errangAuthor Commented:
Yes, I did get a MD 5 key with the Keytool thing, and I did make sure to use an emulator that had the google API...

I could try putting print statements at random points in this program...

Thought that the programs in these books always worked tho...
Dejan PažinHead of SW DevelopmentCommented:

Ok, then there must be some kind of error in logcat. Take a look and post it here.
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

errangAuthor Commented:
Hm... this is how I modified it...

package cookbook.recipes;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class MapsTest extends MapActivity {
	LocationManager mLocationManager;
	Location mLocation;
	TextView tv;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		System.out.println("I get here 1");
		
		MapView mapView = (MapView) findViewById(R.id.zip);
		tv = (TextView) findViewById(R.id.zip_disp);
		
		System.out.println(mapView);
		
		mLocationManager = (LocationManager)
		getSystemService(Context.LOCATION_SERVICE);
		
		System.out.println(mLocationManager);
		
		System.out.println("I get in here 2");
		
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		
		System.out.println(criteria);
		
		String locationprovider = mLocationManager.getBestProvider(criteria,true);
		mLocation = mLocationManager.getLastKnownLocation(locationprovider);
		
		System.out.println(locationprovider);
		
		tv.setText("Last location lat:" + mLocation.getLatitude()+ " long:" + mLocation.getLongitude());
	}
	@Override
	protected boolean isRouteDisplayed() {
		// this method is required
		return false;
	}
}

Open in new window


Printed out every relevant thing... but doesn't print out anything... so I'm not even sure its running, its crashing before it runs...

But I'm sure I added the file to the manifest file, and main.xml exists.

The console prints this out...

[2011-03-29 02:31:40 - cookbook] ------------------------------
[2011-03-29 02:31:40 - cookbook] Android Launch!
[2011-03-29 02:31:40 - cookbook] adb is running normally.
[2011-03-29 02:31:40 - cookbook] Performing cookbook.recipes.MapsTest activity launch
[2011-03-29 02:31:40 - cookbook] Automatic Target Mode: using existing emulator 'emulator-5556' running compatible AVD 'Maps'
[2011-03-29 02:31:40 - cookbook] WARNING: Application does not specify an API level requirement!
[2011-03-29 02:31:40 - cookbook] Device API version is 10 (Android 2.3.3)
[2011-03-29 02:31:42 - cookbook] Application already deployed. No need to reinstall.
[2011-03-29 02:31:42 - cookbook] Starting activity cookbook.recipes.MapsTest on device emulator-5556
[2011-03-29 02:31:43 - cookbook] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=cookbook.recipes/.MapsTest }
Dejan PažinHead of SW DevelopmentCommented:

It would be helpful if you can create a minimum application which should work, but doesnt, and post all the relevant files. That way I can run the project and see where the problem is.

From the above it looks like the activity never starts.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
errangAuthor Commented:
Ok... this application kinda runs...

It displays a grid and a pin... I declared a MapView object... I'm not sure if it just doesn't know where I am, or if it just takes a really long time to load...

Sorry...I couldn't attach the entire file... it didn't like a lot of the file extensions...
HelloItemizedOverlay.java
HelloMap.java
R.java
strings.xml
main.xml
geocode.png
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Android

From novice to tech pro — start learning today.