开发者

Android Google Maps

开发者 https://www.devze.com 2023-01-18 01:26 出处:网络
I use the google code from devloper.android.com I use that application but in the emulator it shows only the crossed lines not the map

I use the google code from devloper.android.com I use that application but in the emulator it shows only the crossed lines not the map so is there any settings in emulator for displaying maps HelloItemizedOverlay.java

package com.HelloGoogleMaps;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Draw开发者_如何学编程able;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class HelloItemizedOverlay extends ItemizedOverlay {
    ArrayList<OverlayItem> mOverlays;
    Context mContext;

  public HelloItemizedOverlay(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
    mOverlays = new ArrayList<OverlayItem>();
    // TODO Auto-generated constructor stub
  }

  public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
    super(defaultMarker);
    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();
  }

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

}

HelloGoogleMaps.java

package com.HelloGoogleMaps;
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.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

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);
        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
        HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
        GeoPoint point = new GeoPoint(19240000,-99120000);
        OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", 
                                    "I'm in Mexico   City!");

        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);
   }

   @Override
   protected boolean isRouteDisplayed() {
       return false;
   }
}

Android Manifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".HelloGoogleMaps"
              android:label="@string/app_name"
              android:theme="@android:style/Theme.NoTitleBar">>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

<uses-library android:name="com.google.android.maps" android:required="true"></uses-library>


Have you put an android:apiKey attribute in your MapView in your xml layout? If not, here is how to obtain an APIKey for you to use the MapView.


According to this tutorial on "Using Google Maps in Android":

If your application manages to load but you cannot see the map (all you see is a grid), then it is very likely you do not have a valid Map key, or that you did not specify the INTERNET permission [in your manifest]:

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


Are you inside a proxy network?
If yes, and if you are using your Android 2.1 Emulator, there can be issues like this?


Did u develop ur application using Goole API version 8? If so means use google api (version 8) to run ur application.

I too got the same problem earlier. Now i got map on my emulator.

Hope this ll help...


    MAin.xml


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

                   <com.google.android.maps.MapView
                    android:id="@+id/mapview"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:apiKey="0xkjqDikoo8j6Bks9CX7-P1FBT7fNUWC6uQkyyQ"
                    android:clickable="true" />
            </LinearLayout>

        `    `-------------------------------------------------------------------------

            MainActivity  Java Class



        package com.example.mapsample;

        import com.google.android.maps.GeoPoint;
        import com.google.android.maps.ItemizedOverlay;
        import com.google.android.maps.MapActivity;
        import com.google.android.maps.MapController;
        import com.google.android.maps.MapView;
        import com.google.android.maps.OverlayItem;

        import android.content.Context;
        import android.content.Intent;
        import android.graphics.drawable.Drawable;
        import android.location.Location;
        import android.location.LocationListener;
        import android.location.LocationManager;
        import android.os.Bundle;
        import android.util.Log;
        import android.widget.Toast;

        public class MainActivity extends MapActivity implements LocationListener {

            private LocationManager locManager;
            private MyItemizedOverlay itemizedOverlay;
            private MapView mapView;
            private MapController controller;
            private double lat;
            private double lon;
            private double getlat;
            private double getlon;
            private double[] getlatarr;
            private double[] getlonarr;
            private GeoPoint point;
            private Intent getintent;
            OverlayItem overlayItem;

            @Override
            public void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                Bundle extras = getIntent().getExtras();
                // getlat = getintent.getDoubleExtra("latval", 1);
                // getlon = getintent.getDoubleExtra("lonval", 1);

                getlatarr = extras.getDoubleArray("latval");
                getlonarr = extras.getDoubleArray("lonval");

                Log.i("getlatarr", "getlatarr " + getlatarr[0]);

                // fetch the map view from the layout
                mapView = (MapView) findViewById(R.id.mapview);

                // make available zoom controls
                mapView.setBuiltInZoomControls(true);

                // SetGeoPoints(getlat,getlon);
                SetGeoPoints(getlatarr, getlonarr);

            }

            // public void SetGeoPoints(double nlat,double nlon) {
            public void SetGeoPoints(double[] nlat, double[] nlon) {

                // TODO Auto-generated method stub

                Log.i("nlat", "nlat " + nlat.length);
                Log.i("nlon", "nlon " + nlon.length);

                // latitude and longitude of Rome ***************for single
                // pin*************
                // lat = nlat;//41.889882;
                // lon = nlon;//12.479267;

                // ***************for multiple pin*************
                Drawable drawable = this.getResources().getDrawable(R.drawable.map_pin);
                itemizedOverlay = new MyItemizedOverlay(drawable, this);
                for (int i = 0; i < nlat.length; i++) {

                    lat = nlat[i];// 41.889882;
                    lon = nlon[i];// 12.479267;

                    // create geo point
                    point = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));

                    // get the MapController object
                    controller = mapView.getController();

                    // animate to the desired point
                    // controller.animateTo(point);

                    // set the map zoom to 13
                    // zoom 1 is top world view
                    controller.setZoom(13);

                    // fetch the drawable - the pin that will be displayed on the map

                    // create and add an OverlayItem to the MyItemizedOverlay list
                    overlayItem = new OverlayItem(point, "", "");

                    itemizedOverlay.addOverlay(overlayItem);

                    // add the overlays to the map
                    mapView.getOverlays().add(itemizedOverlay);

                }
                mapView.invalidate();

                /*
                 * // Use the location manager through GPS locManager =
                 * (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                 * locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                 * this);
                 * 
                 * //get the current location (last known location) from the location
                 * manager Location location = locManager
                 * .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                 * 
                 * 
                 * //if location found display as a toast the current latitude and
                 * longitude if (location != null) {
                 * 
                 * Toast.makeText( this, "Current location:\nLatitude: " +
                 * location.getLatitude() + "\n" + "Longitude: " +
                 * location.getLongitude(), Toast.LENGTH_LONG).show();
                 * 
                 * point = new
                 * GeoPoint((int)(location.getLatitude()*1E6),(int)(location.
                 * getLongitude() *1E6));
                 * 
                 * Log.i("point", "point "+point); controller.animateTo(point); } else {
                 * 
                 * Toast.makeText(this, "Cannot fetch current location!",
                 * Toast.LENGTH_LONG).show(); }
                 * 
                 * 
                 * 
                 * //when the current location is found – stop listening for updates
                 * (preserves battery) locManager.removeUpdates(this);
                 */
            }

            @Override
            protected boolean isRouteDisplayed() {
                return false;

            }

            /* When the activity starts up, request updates */
            @Override
            protected void onResume() {
                super.onResume();
                // locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0,
                // this);
            }

            @Override
            protected void onPause() {
                super.onPause();
                // locManager.removeUpdates(this); //activity pauses => stop listening
                // for updates
            }

            @Override
            public void onLocationChanged(Location location) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

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

            }

        }



        ------------------------------------------------

        MapPoints   class




        package com.example.mapsample;

    import java.util.ArrayList;
    import java.util.HashMap;

    import com.google.android.maps.MapActivity;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class MapPoints extends Activity implements OnClickListener{

        private Button showmapbtn;
    //  ArrayList<Double> latlist = new ArrayList<Double>();//{41.889882,30.889882,39.889882,42.889882,43.889882};
    //  ArrayList<Double> longlist = new ArrayList<Double>();//{12.479267,13.479267,14.479267,11.479267,10.479267};;

        double[] latlist = {41.889882,41.889862,41.689882,41.889882,41.589882};
        double[]  longlist = {12.479267,12.479267,12.379267,12.579267,12.979267};;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

    //      latlist.add(41.889882);
    //      latlist.add(39.889882);
    //      latlist.add(48.889882);
    //      latlist.add(30.889882);
    //      
    //      latlist.add(12.479267);
    //      latlist.add(11.889882);
    //      latlist.add(10.889882);
    //      latlist.add(15.479267);

            showmapbtn = (Button)findViewById(R.id.btnshowmap);
            showmapbtn.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.btnshowmap:

                Intent mapintent = new Intent(getApplicationContext(),MainActivity.class);
    //          mapintent.putExtra("latval", 41.889882);
    //          mapintent.putExtra("lonval", 12.479267);

                mapintent.putExtra("latval", latlist);
                mapintent.putExtra("lonval", longlist);

    //          mapintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(mapintent);

                break;

            default:
                break;
            }
        }

    }


    ----------------------------------------------------------

    MyItemizedOverlay java class



    package com.example.mapsample;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

    public MyItemizedOverlay(Drawable defaultMarker, Context ctx) {
        super(boundCenterBottom(defaultMarker));

    }

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    public void clear() {

        mOverlays.clear();
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

    @Override
    public int size() {
        return mOverlays.size();
    }

    @Override
    protected boolean onTap(int index) {
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) {

        return false;
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        if (!shadow) {
            super.draw(canvas, mapView, false);
        }
    }

}
0

精彩评论

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