在mapview中提供用户定义的注释

时间:2011-03-10 12:19:31

标签: android

我想在带有多个注释的mapview上探索我的城市,但我无法做到这一点。是否有人建议我如何在用户定义的经度和纬度的android中做到这一点。我创建了一个带有一个注释的地图视图,但是在系统定义的情况下,我必须从模拟器控件传递经度和纬度。我们如何使用带有多个注释的代码发送它。

这是我的代码:

package com.ex.maps;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Toast;
import android.widget.ZoomControls;

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.Overlay;

public class HelloGoogleMaps extends MapActivity implements LocationListener{
    MapController mc;
    GeoPoint p;
    double lat;
    double lng;
    @Override


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MapView gMapView = (MapView) findViewById(R.id.mapview);
        gMapView.setBuiltInZoomControls(true);
        GeoPoint p = new GeoPoint((int) (lat * 1000000), (int) (lng * 1000000));
        gMapView.setSatellite(true);
        mc = gMapView.getController();
        mc.setCenter(p);
        mc.setZoom(14);
        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new HelloGoogleMaps();
        //lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,mlocListener );
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
        ZoomControls zoomControls = (ZoomControls) gMapView.getZoomControls();

        zoomControls.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,

        LayoutParams.WRAP_CONTENT));

        gMapView.addView(zoomControls);

        gMapView.displayZoomControls(true);
        MyLocationOverlay myLocationOverlay = new MyLocationOverlay();

        List<Overlay> list = gMapView.getOverlays();

        list.add(myLocationOverlay);
    /*List<Overlay> mapOverlays = gMapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.arrow_icon);
    HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
    //GeoPoint p = new GeoPoint((int) (lat * 1000000), (int) (lng * 1000000));
    OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
    itemizedoverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedoverlay);*/
    }

    public void onLocationChanged(Location location) {
        if (location != null) {
            lat = location.getLatitude();
            lng = location.getLongitude();
            p = new GeoPoint((int) (lat * 1000000), (int) (lng * 1000000));
            mc.animateTo(p);
            //Log.d("LOCATION CHANGED", location.getLatitude() + "");
            //Log.d("LOCATION CHANGED", location.getLongitude() + "");*/
            Toast.makeText(HelloGoogleMaps.this,
                location.getLatitude() + "" + location.getLongitude(),
                Toast.LENGTH_LONG).show();
            }
        }
         //makeUseOfNewLocation(location);



    public void onProviderDisabled(String provider) {
    }
    public void onProviderEnabled(String provider) {
    }
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
    protected boolean isRouteDisplayed() {
    return true;
    }
    class MyLocationOverlay extends com.google.android.maps.Overlay {
        @Override

        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {

        super.draw(canvas, mapView, shadow);
        Paint paint = new Paint();
        // Converts lat/lng-Point to OUR coordinates on the screen.
        Point myScreenCoords = new Point();
        mapView.getProjection().toPixels(p, myScreenCoords);
        paint.setStrokeWidth(1);
        paint.setARGB(255, 255, 255, 255);
        paint.setStyle(Paint.Style.STROKE);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
        canvas.drawText("Here I am...", myScreenCoords.x, myScreenCoords.y, paint);
        return true;

        } } 
}

1 个答案:

答案 0 :(得分:2)

public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.favmapview);
              listbtn = (Button)findViewById(R.id.button1);
              mapbtn = (Button)findViewById(R.id.button2);
              TextView tv = (TextView)findViewById(R.id.tvmap);
              bundle = getIntent().getExtras();
              str1= bundle.getString("key");
              lat = bundle.getStringArrayList("lat");
              log=bundle.getStringArrayList("log");
              location = bundle.getStringArrayList("location");
              tv.setText(str1);


              listbtn.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                    // TODO Auto-generated method stub
                    OpenListView();        // Function called to display list view


                        }
                    });
                initialiseMapView();
          } 
    // Navigates to listView class and displays listView     
          protected void OpenListView() {

                final ProgressDialog dialog = ProgressDialog.show(FavMapView.this, "HIV ATLAS", 

                          "Please wait...", true);
                final Handler handler = new Handler() {

                    public void handleMessage(Message msg) {

                                dialog.dismiss();
                                }
                             };

                 Thread checkUpdate = new Thread() {  

                     public void run() {
                         Intent intent = new Intent(FavMapView.this,Favorites.class);
                         intent.putExtra("key", bundle.getString("key"));
                         startActivity(intent);
                         dialog.dismiss();
                         handler.sendEmptyMessage(0);

                     }
                 };
                 checkUpdate.start();   
          }

    //============End    

          @Override
          public boolean isRouteDisplayed() {
              return true;
          } 
          //Generates Map View 
          private void initialiseMapView() {

              mapView = (MapView) findViewById(R.id.mapview);
              mapController = mapView.getController();
              mapView.setBuiltInZoomControls(true);
              mapView.setSatellite(false);

              GeoPoint startPoint = new GeoPoint((int)(40.7575 * 1E6), (int)(-73.9785 * 1E6));
              mapController.setCenter(startPoint);

          }
          //===============End
          @Override
          public void onStart() {
              super.onStart();
              initialiseOverlays();
          }
        // Sets Geo points on Map View 
          private void initialiseOverlays() {
              try {
                // Create an ItemizedOverlay to display a list of markers
                  Drawable defaultMarker = getResources().getDrawable(R.drawable.marker);
                  placesItemizedOverlay = new HelloItemizedOverlay (FavMapView.this, defaultMarker);
                  for(int i=0;i<log.size();i++)
                  {
                      double lat1 = Double.parseDouble(lat.get(i));
                      double log1 =Double.parseDouble(log.get(i));
                      placesItemizedOverlay.addOverlayItem(new OverlayItem(new GeoPoint((int)(lat1 * 1E6),
                              (int) (log1 * 1E6)),location.get(i),null));
                  }

//==============================End 
                // Add the overlays to the map
                  mapView.getOverlays().add(placesItemizedOverlay);
            } catch (NumberFormatException e) {
                Toast.makeText(getApplicationContext(), "No Jobs available",
                        Toast.LENGTH_SHORT).show();
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NotFoundException e) {
                Toast.makeText(getApplicationContext(), "No Jobs available",
                        Toast.LENGTH_SHORT).show();
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          }

    }