Map shows marker but doesn't show real map in background
I have map in m android app but it doesn't work well. I spent last two hours looking in code line by line but I don't know what is the problem. I done all like in tutorial http://mobiforge.com/developing/story/using-google-maps-android. I set in manifest uses google maps. Map shows marker, doesn't show map ( instead in background is grid lines), zoom controls disappear after few seconds and not come back. What can be a problem ?
This is activity class.
package com.roomate.www;
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.MapView.LayoutParams;
import com.google.android.maps.Overlay;
import android.view.View;
import android.widget.LinearLayout;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
public class Location extends MapActivity{
MapView mapView;
MapController mc;
GeoPoint p;
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);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts)开发者_如何学运维;
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.marker);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_result);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setStreetView(true);
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);
mc = mapView.getController();
String coordinates[] = {"1.352566007", "103.78921587"};
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);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
This is layout for map.
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_gravity="left"
android:gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/imbReturnMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:src="@drawable/close"
android:layout_gravity="left">
</ImageButton>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_gravity="right">
<ImageButton
android:id="@+id/imbCloseM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:src="@drawable/close"
android:layout_gravity="right">
</ImageButton>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="@+id/mainlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0WR3Cl0evv0U6G2xH9ZnvEFYbDiSDUu_GRovTUQ"
/>
<LinearLayout android:id="@+id/zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</LinearLayout>
As indicated by your screenshot, your device or emulator does not have any connectivity. The zero-bars icon tells you this.
If this is an emulator, it probably failed to detect the Internet on startup, or perhaps you pressed [F8] to toggle off connectivity. Restart the emulator and, with luck, you will get the normal two bars of emulated signal strength.
While you are waiting for the emulator to restart, also confirm that you hold the INTERNET
permission in your manifest. That has no impact on the emulator's overall access to the Internet -- however, lacking that permission will also cause the behavior you are seeing.
Its a problem with api key. Create a new api key using MD5 key.that helped me
https://developers.google.com/maps/documentation/android/v1/maps-api-signup?hl=sv-SE
First off you have to install Google API package in your emulator.
Secondly Google has changed the procedure of getting map api key.
this tutorial can solve your problem
Also See this for displaying markers on the map
精彩评论