开发者

google map in android application

I am new at Android. I have 开发者_开发技巧been trying from many days to make very basic google map application but unable to complete it yet... :(

There are no errors in code, emulator running fine from terminal, Map key also fine but still I am unable to see the map. When I run my app only grid appears and map is not displayed. Here is the code, can any body please help me.

public class HelloGoogleMaps extends MapActivity { 
    /** 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); 
    } 
    protected boolean isRouteDisplayed(){ 
        return false; 
    } 
} 

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:layout_weight="1" 
            android:clickable="true" 
            android:apiKey="0fyF-qSuCtdQinoUGoFbLxZoTx10Tm-YV6m6A8g" 
/> 

manifest file

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.android.GoogleMaps" 
    android:versionCode="1" 
    android:versionName="1.0"> 

    <uses-permission android:name="android.permission.INTERNET"/ 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
        <uses-library android:name="com.google.android.maps" /> 
        <uses-permission android:name="android.permission.INTERNET"/> 
        <activity android:name=".HelloGoogleMaps" 
              android:label="@string/app_name" 
              android:theme="@android:style/Theme.NoTitleBar"> 
        <!--<activity android:name=".HelloGoogleMaps" 
                  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> 
    <uses-sdk android:minSdkVersion="4" /> 
</manifest> 

Don't know where is anything wrong. I am using eclipse and android 1.6


try setting internet permission outside the application tag


    **// Activty**
     public class MapsActivity extends MapActivity {

            private MapController mapController;
            private MapView mapView;
            private LocationManager locationManager;
            private MyOverlays itemizedoverlay;
            private MyLocationOverlay myLocationOverlay;

            public void onCreate(Bundle bundle) {
                super.onCreate(bundle);
                setContentView(R.layout.main); // bind the layout to the activity

                // Configure the Map
                mapView = (MapView) findViewById(R.id.map_container);
                mapView.setBuiltInZoomControls(true);
            //  mapView.setSatellite(true);
                mapView.setStreetView(true);

                mapController = mapView.getController();
                mapController.setZoom(14); // Zoon 1 is world view
                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                        0, new GeoUpdateHandler());

                myLocationOverlay = new MyLocationOverlay(this, mapView);
                mapView.getOverlays().add(myLocationOverlay);

                myLocationOverlay.runOnFirstFix(new Runnable() {
                    public void run() {
                        mapView.getController().animateTo(myLocationOverlay.getMyLocation());
                    }
                });

                Drawable drawable = this.getResources().getDrawable(R.drawable.map);
                itemizedoverlay = new MyOverlays(this, drawable);
                createMarker();
            }


            protected boolean isRouteDisplayed() {
                return false;
            }

            public class GeoUpdateHandler implements LocationListener {


                public void onLocationChanged(Location location) {
                    int lat = (int) (location.getLatitude() * 1E6);
                    int lng = (int) (location.getLongitude() * 1E6);
                    GeoPoint point = new GeoPoint(lat, lng);
                    createMarker();
                    mapController.animateTo(point); // mapController.setCenter(point);

                }


                public void onProviderDisabled(String provider) {
                }


                public void onProviderEnabled(String provider) {
                }


                public void onStatusChanged(String provider, int status, Bundle extras) {
                }
            }

            private void createMarker() {
                GeoPoint p = mapView.getMapCenter();
                OverlayItem overlayitem = new OverlayItem(p, "", "");
                itemizedoverlay.addOverlay(overlayitem);
                if (itemizedoverlay.size() > 0) {
                    mapView.getOverlays().add(itemizedoverlay);
                }
            }


            protected void onResume() {
                super.onResume();
                myLocationOverlay.enableMyLocation();
                myLocationOverlay.enableCompass();
            }


            protected void onPause() {
                super.onPause();
                myLocationOverlay.disableMyLocation();
                myLocationOverlay.disableCompass();
            }
        } 

    **//Class MyOvelays**

        public class MyOverlays extends ItemizedOverlay<OverlayItem> {

            private static int maxNum = 5;
            private OverlayItem overlays[] = new OverlayItem[maxNum];
            private int index = 0;
            private boolean full = false;
            private Context context;
            private OverlayItem previousoverlay;

            public MyOverlays(Context context, Drawable defaultMarker) {
                super(boundCenterBottom(defaultMarker));
                this.context = context;
            }

            @Override
            protected OverlayItem createItem(int i) {
                return overlays[i];
            }

            @Override
            public int size() {
                if (full) {
                    return overlays.length;
                } else {
                    return index;
                }

            }

            public void addOverlay(OverlayItem overlay) {
                if (previousoverlay != null) {
                    if (index < maxNum) {
                        overlays[index] = previousoverlay;
                    } else {
                        index = 0;
                        full = true;
                        overlays[index] = previousoverlay;
                    }
                    index++;
                    populate();
                }
                this.previousoverlay = overlay;
            }

            protected boolean onTap(int index) {
                OverlayItem overlayItem = overlays[index];
                Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("This will end the activity");
                builder.setCancelable(true);
                builder.setPositiveButton("I agree", new OkOnClickListener());
                builder.setNegativeButton("No, no", new CancelOnClickListener());
                AlertDialog dialog = builder.create();
                dialog.show();
                return true;
            };

            private final class CancelOnClickListener implements
                    DialogInterface.OnClickListener {
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(context, "You clicked yes", Toast.LENGTH_LONG)
                            .show();
                }
            }

            private final class OkOnClickListener implements
                    DialogInterface.OnClickListener {
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(context, "You clicked no", Toast.LENGTH_LONG).show();
                }
            }
        } 


    //Main.xml

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent">



         <com.google.android.maps.MapView

                        android:id="@+id/map_container"
                         android:layout_width="fill_parent"
                         android:layout_height="fill_parent"
                         android:apiKey="0PYAmXmindXBuvCvIFhCUC3y0GNjJKuFJHclkVw"
                         android:clickable="true"
                         android:focusable="true"
                         android:keepScreenOn="true"


                         />


        </RelativeLayout>

    //Android.mainifest



 <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.map"
            android:versionCode="1"
            android:versionName="1.0" >

            <uses-sdk android:minSdkVersion="10" />
            <uses-permission android:name="android.permission.INTERNET"/>
            <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
            <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


            <application
                android:icon="@drawable/ic_launcher"
                android:label="@string/app_name" >
                 <uses-library android:name="com.google.android.maps" />
                <activity
                    android:name=".MapsActivity"
                    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>


Are you using the correct Google Maps key - if you use a debug key, the maps seem not to work.

You might have to regenerate the key without the --debug flag.

maybe this will help: Android MapView - tiles not loading with Debug API key


Common issues are that you're using the wrong emulator (ie not the Google APIs one), have the wrong imports, have the wrong API key, don't have permissions in manifest, don't have uses library etc

I wrote up a newbies guide on this, have a look http://www.jameselsey.co.uk/blogs/techblog/android-how-to-display-a-map-the-easy-way/


I have recently been through all of these. I as well tried everything mentioned but all that worked is -

Make sure you are not a behind a proxy and remove any proxy settings you made on the emulator. There's a bug that Google Maps doesn't work on Android Emulators behind a proxy.


I think the problem is that you are using a wrong apk. You must use the apk generated by the emulator (eclipse in my case) and not the apk generated using the option "export". I think the problem is related to the key used to export the apk is not the same as the google maps api key. Try it.

PS: sorry for my bad English.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜