Android谷歌地图

时间:2010-09-25 05:27:50

标签: android

我使用devloper.android.com中的google代码 我使用该应用程序,但在模拟器中它只显示交叉线而不是地图 那么在模拟器中是否有用于显示地图的设置 HelloItemizedOverlay.java

package com.HelloGoogleMaps;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
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>

    

 

5 个答案:

答案 0 :(得分:1)

您是否在{x} android:apiKey的{​​{1}}中添加了MapView个属性?如果没有,layout是如何获取APIKey以供您使用MapView。

答案 1 :(得分:0)

根据this tutorial on "Using Google Maps in Android"

  

如果您的应用程序设法加载但您看不到地图(您看到的只是一个网格),那么您很可能没有有效的Map键,或者您没有指定INTERNET权限[在您的清单]:

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

答案 2 :(得分:0)

您是否在代理网络内? 如果是,如果您使用的是Android 2.1仿真器,可能会出现这样的问题吗?

答案 3 :(得分:0)

您是否使用Goole API版本8开发了您的应用程序? 如果是这样意味着使用google api(版本8)来运行你的应用程序。

我之前也遇到过同样的问题。现在我在我的模拟器上得到了地图。

希望这会有所帮助...

答案 4 :(得分:0)

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

}
相关问题