Android, Google Maps won't show - android

I'm making an app that needs to show a location that I define with latitude and longitude. Everything goes fine, it opens Google maps in my app screen but the maps won't show up!
This is the code I use...
public class MapsActivity extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//vraag custom title bar
requestCustomTitle();
setContentView(R.layout.mapsactivity);
//Titel dynamisch invullen volgens taalvoorkeuren, roept methode setCustomTitle op
setCustomTitle(getResources().getString(R.string.MapsTitel));
showZoom();
locate(getIntent().getStringExtra("kantoorLat"), getIntent().getStringExtra("kantoorLng"));
addMarker();
}
/*
* De methode showZoom zorgt ervoor dat de zoombuttons worden getoond
*/
private void showZoom(){
mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
}
/*
* De methode locate zorgt ervoor dat de juiste locatie wordt getoond
*/
private void locate(String strlat, String strlng){
mc = mapView.getController();
String coordinates[] = {strlat,strlng};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
mapView.invalidate();
}
/*
* De methode addMarker zorgt ervoor dat er een indicator staat op de locatie
*/
private void addMarker(){
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
}
protected boolean isRouteDisplayed(){
return false;
}
class MapOverlay extends com.google.android.maps.Overlay{
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when){
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
mapView.getProjection().toPixels(p,screenPts);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.accentindicator);
canvas.drawBitmap(bmp, screenPts.x+7, screenPts.y-19, null);
return true;
}
}

MD5 key will not work on different machines.
You need to generate MD5 debug key for the current SDK that you are using.
Generate the debug key for your current machine and try again.

Get a API key here
http://code.google.com/apis/maps/signup.html
Put the following rule in your AndroidManifest
<uses-permission android:name="android.permission.INTERNET" />

Check the following blog entry that lists down the reasons that might cause this issue.
http://blog.doityourselfandroid.com/2011/01/18/using-google-maps-in-your-android-app/
Besides the activity, its also the manifest, the layout or the API key that can be invalid.
In short
The Android SDK needs to be setup to
support Google Maps.
A MapView
component needs to be added to your
layout, containing a valid Google
Maps API key
The Activity that will
be responsible for showing the map
needs to extend from MapActivity
The
application manifest needs to be
setup with the
android.permission.INTERNET
permission
The application manifest
needs to be setup with the
com.google.android.maps library
In your case, it's most likely internet connectivity (permisson in manifest and/or emulator internet access) or an invalid API key. Keep in mind that the API key is linked to the certificate used to sign your APK.

You need to get the key to access Google map in your application and after getting key you have to add library in ue manifest file which will be installed automatic when u got the key.

I think its your emulator version problem.Android sdk 2.2 emulators some times doesnot show the exact location.Try to run emulator 2.1 .

Even using the Google API Introduction I have had problems like this.
I followed this tutorial and it worked for me:
http://www.vogella.com/articles/AndroidGoogleMaps/article.html
Good luck,

Related

Use OSMDROID to display my server generated map tiles

I am trying to display map tiles using osmdroid. but it is displaying blank screen.
This is my code:
map.setZ(100);
map.setTileSource(new OnlineTileSourceBase("", 12, 18, 256, ".png",
new String[]{"https://storage.googleapis.com/brainpool/user-files/brainpoollicense/maptiles/PL53LY4K0EDYMX/"}) {
#Override
public String getTileURLString(long pMapTileIndex) {
return getBaseUrl()
+ MapTileIndex.getZoom(pMapTileIndex)
+ "/" + MapTileIndex.getX(pMapTileIndex)
+ "/" + MapTileIndex.getY(pMapTileIndex)
+ mImageFilenameEnding;
}
});
map.setMultiTouchControls(true);
IMapController mapController = map.getController();
GeoPoint startPoint;
startPoint = new GeoPoint(151.2927108765, -32.779342448);
mapController.setZoom(12F);
mapController.setCenter(startPoint);
map.invalidate();
createmarker();
This is output:
Also, note that i am getting this in output always:
W/OsmDroid: Problem downloading MapTile: /12/1674/2 HTTP response: Not Found
My question is when getTileURLString method is called? because it is called for the values which are taking wrong z/x/y which do not exist on the server.

Nutiteq Android- Offline map is not displaying

Here is my code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setComponents(new Components());
RasterDataSource datasource = new PackagedRasterDataSource(new EPSG3857(), 11, 12, "t{zoom}_{x}_{y}", getApplicationContext());
RasterLayer mapLayer = new RasterLayer(datasource, 16);
mapView.getLayers().setBaseLayer(mapLayer);
mapView.setFocusPoint(mapView.getLayers().getBaseLayer().getProjection().fromWgs84(217884.21f, 1928068.13f));
//mapView.setZoom(15);
}
I have added .map file from http://www.mapcacher.com/ and converted it to PNG using http://dev.nutiteq.ee/jarmaps/ . I have mentioned the correct zoom level, checked that t11 and t12 files exist under res/raw. Also I have converted lat/lon to the required format using http://www.latlong.net/lat-long-utm.html . What is it that I am doing wrong.I don't get any errors in log cat but a blank page with Nutiteq logo is diplayed.
This line is wrong, instead of UTM coordinates
mapView.setFocusPoint(mapView.getLayers().getBaseLayer().getProjection().fromWgs84(217884.21f, 1928068.13f));
you should use WGS84, lat-long coordinates (first parameter long as x, then lat as y), like method name fromWgs84 suggests. No need to convert from/to UTM externally.

Google Maps GroundOverlay Display

I am trying to use groundoverlay on Google Maps just to see if I can get an image on Google Maps. I have no error and looking through the documentation and examples. The image does not display.
Here is part of my code where I am implementing groundoverlay:
private GroundOverlay mGroundOverlay;
private static final LatLng NEWARK = new LatLng(40.714086, -74.228697);
.....
Under
protected void onCreate(Bundle savedInstanceState) {
.....
public void setImage(){
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(NEWARK, 11));
mGroundOverlay = googleMap.addGroundOverlay(new GroundOverlayOptions()
.image(BitmapDescriptorFactory.fromResource(R.drawable.battery)).anchor(0, 1)
.position(NEWARK , 85000f,6500f));
}
I have the image "battery.jpeg" in my resource folder, drawable-hpdi
Can some look at my code and tell me what I am missing? Since I am a beginner in Android I would like an explanation as well.
Maybe I'm reading your intention incorrectly, but if you are trying to use the Android Maps API, you'll have to get a key from the API dashboard; the irreplaceable vogella has a tutorial:
http://www.vogella.com/tutorials/AndroidGoogleMaps/article.html
if you are a new droidgrammer, I can think of no better programmer to learn from.
also, could you post your logcat? FYI: you can use Logcat Extreme on a rooted device to get live logging overlaid on your device that you can also send to file... it's the bee's knees and pouring over logcat readouts will accelerate your education greatly. =]
gl hf
Check this one
LatLng c1 = new LatLng( -33.67032801,151.30209310);
LatLng c2 = new LatLng( -33.66687426,151.30269820);
LatLng c3 = new LatLng( -33.66657936,151.30026840);
LatLng c4 = new LatLng( -33.67003309,151.29966320);
LatLngBounds latLngBounds = new LatLngBounds(c1, c3).including(c2).including(c4);
GroundOverlayOptions groundOverlayOptions = new
GroundOverlayOptions();
BitmapDescriptor bitmapDescriptor =
BitmapDescriptorFactory.fromAsset("h1.png");
groundOverlayOptions.image(bitmapDescriptor);
groundOverlayOptions.positionFromBounds(latLngBounds);
map.addGroundOverlay(groundOverlayOptions);

The source attachment does not contain the source for the file Layoutinflater.class

I'm trying to write an android application that uses google maps api version 2.
In Debug the application crashes when it executes the instruction:
setContentView(R.layout.activity_poi_map);
This is the error:
the source attachment does not contain the source for the file
Layoutinflater.class
I don't understand the reason. In headers I imported the whole class
android.view.*
Thank you in advance for any answer.
This is the code:
public class PoiMap extends FragmentActivity {
private GoogleMap pMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent callerIntent = getIntent();
final LatLng userCoord = callerIntent.getParcelableExtra("userCoord");
final Poi poi = (Poi) callerIntent.getSerializableExtra("poi");
setContentView(R.layout.activity_poi_map);
setUpMapIfNeeded(userCoord);
// Sets a callback that's invoked when the camera changes.
pMap.setOnCameraChangeListener(new OnCameraChangeListener() {
#Override
/* Only use the simpler method newLatLngBounds(boundary, padding) to generate
* a CameraUpdate if it is going to be used to move the camera *after* the map
* has undergone layout. During layout, the API calculates the display boundaries
* of the map which are needed to correctly project the bounding box.
* In comparison, you can use the CameraUpdate returned by the more complex method
* newLatLngBounds(boundary, width, height, padding) at any time, even before the
* map has undergone layout, because the API calculates the display boundaries
* from the arguments that you pass.
* #see com.google.android.gms.maps.GoogleMap.OnCameraChangeListener#onCameraChange(com.google.android.gms.maps.model.CameraPosition)
*/
public void onCameraChange(CameraPosition arg0) {
// Move camera.
if (poi != null){
LatLng poiCoord = poi.getLatLng();
pMap.addMarker(new MarkerOptions()
.position(poiCoord)
.title(poi.getName())
.snippet(poi.getCategory())
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_star)));
Log.d("userCoord", userCoord.toString());
Log.d("poiCoord", poiCoord.toString());
double minY = Math.min(userCoord.latitude, poiCoord.latitude);
double minX = Math.min(userCoord.longitude, poiCoord.longitude);
double maxY = Math.max(userCoord.latitude, poiCoord.latitude);
double maxX = Math.max(userCoord.longitude, poiCoord.longitude);
Log.d("minY", " " + minY);
Log.d("minX", " " + minX);
Log.d("maxY", " " + maxY);
Log.d("maxX", " " + maxX);
LatLng northEast = new LatLng(maxY, maxX);
LatLng southWest = new LatLng(minY, minX);
LatLngBounds bounds = new LatLngBounds(southWest, northEast);
// move camera
pMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 40));
// Remove listener to prevent position reset on camera move.
pMap.setOnCameraChangeListener(null);
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_poi_map, menu);
return true;
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded(new LatLng(0.0, 0.0));
}
private void setUpMapIfNeeded(LatLng coord) {
// Do a null check to confirm that we have not already instantiated the map.
if (pMap == null) {
// Try to obtain the map from the SupportMapFragment.
pMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (pMap != null) {
setUpMap(coord);
}
}
}
private void setUpMap(LatLng userCoord) {
pMap.addMarker(new MarkerOptions()
.position(userCoord)
.title("Your location")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_user)));
pMap.animateCamera(CameraUpdateFactory.newLatLng(userCoord));
/* public static CameraUpdate newLatLngZoom (LatLng latLng, float zoom)
* Returns a CameraUpdate that moves the center of the screen to a latitude
* and longitude specified by a LatLng object, and moves to the given zoom
* level.
* Parameters
* latLng a LatLng object containing the desired latitude and longitude.
* zoom the desired zoom level, in the range of 2.0 to 21.0.
* Values below this range are set to 2.0, and values above it are set to 21.0.
* Increase the value to zoom in. Not all areas have tiles at the largest zoom levels.
* Returns
* a CameraUpdate containing the transformation.
*/
pMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userCoord, 15));
}
}
This is a common question and I can never find a good answer for anyone so I'll do my best to help.
This sounds like you are missing the source code for Android. You need to download the Sources for Android SDK in Eclipse with the SDK Manager.
Open the SDK Manager in Eclipse
Find the version of android you want the source for. In this example, Android 4.2 (API17)
Check the box Sources for Android SDK
Click Install # packages
For example
After that, run your program and get to the point that you get that error. Now you will need to attach the documentation you've downloaded
Short
Click the button Edit Source Lookup Path
Click the Default folder
Click Add
Select File System Directory and click OK
Locate your downloaded source code. In this example, look for android-17 for API17. It will be in a directory such as adt-bundle\sdk\sources\android-17
Click OK a few times and your source is now linked.
Long
Click the button Edit Source Lookup Path
Click the Default folder and click Add
Select File System Directory and click OK
Locate your downloaded source code. In this example, look for android-17 for API17. It will be in a directory such as adt-bundle\sdk\sources\android-17
Click OK twice and it should look something like this
Click OK and enjoy your source code
This usually means that you are unable to access the source code whose bytecode is being called. In order to list out the exception you would need the original source which unfortunately is usually unavailable in jar formats.
Your option is to update the .jar file by updating your Android SDK with correct Rev’s and let it compile again to fix the issue.

Android Maps KML Handler

Ok so at the moment I'm getting a null pointer exception when adding to the list of overlays. I've declared this at the top of the map activity class:
private static List<Overlay> mOverlays;
And then further down I've got this for my source/ from point:
Drawable fromDrawable = getResources().getDrawable(R.drawable.pin_blue);
OverlayItem fromOverlayItem = new OverlayItem(fromGeoPoint, "Hello!", "This is your current Location.");
MapWhatsNearOverlay fromItemizedOverlay = new MapWhatsNearOverlay(fromDrawable);
mOverlays.add(fromItemizedOverlay);
connectAsyncTask _connectAsyncTask = new connectAsyncTask();
_connectAsyncTask.execute();
mMapView.getOverlays().add((Overlay) mOverlays);
And the same for the destination as well, but the null pointer exception pops up when processing the bottom line of code.
Refer this link to draw driving path in your application. You just need to add the four classes present in the link in your project and call the below lines when you need to display the route.
SharedData data = SharedData.getInstance();
data.setAPIKEY("0RUTLH7cqd6yrZ0FdS0NfQMO3lioiCbnH-BpNQQ");//set your map key here
data.setSrc_lat(17);//set your src lat
data.setSrc_lng(78);//set your src lng
data.setDest_lat(18);//set your dest lat
data.setDest_lng(77);//set your dest lng
startActivity(new Intent(YourActivity.this,RoutePath.class));//add RoutePath in your manifeast file
//Also add the permission to your manifeast file

Categories

Resources