谷歌地图V2活动

时间:2014-03-07 18:08:13

标签: android

我正在编写应用程序并在其中一项活动中,我希望用户能够通过应用程序拍摄照片,并且使用标记在地图上标记拍摄照片的位置。单击标记时,将显示照片。到目前为止,我正在尝试将标记设置为图片本身,但似乎不起作用。这是我目前的代码  MainActivity类:

// Google Map
    private GoogleMap googleMap;
    Button setMarker;
    ImageView image;
    ImageButton bt;
    Intent i;
    final static int cameraData = 0;
    Bitmap bmp;
    Location location;

    // latitude and longitude
    // double latitude = 53.264315;
    // double longitude = -6.164774;

    // create marker
    // MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude,
    // longitude)).title("Hiya")

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
        bmp = BitmapFactory.decodeStream(is);

        try {
            // Loading map
            initilizeMap();
            initilizeVars();
            // googleMap.addMarker(marker);
            googleMap.setMyLocationEnabled(true);
            googleMap.getUiSettings().setMyLocationButtonEnabled(true);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    private void initilizeVars() {
        setMarker = (Button) findViewById(R.id.btMarker);
        image = (ImageView) findViewById(R.id.ivPic);
        bt = (ImageButton) findViewById(R.id.ibCamera);
        setMarker.setOnClickListener(this);
        bt.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btMarker:
            try {
                // Creating a LatLng object for the current location
                LatLng latLng = new LatLng(location.getLatitude(),
                        location.getLongitude());
                googleMap.addMarker(new MarkerOptions().position(latLng)
                        .icon(BitmapDescriptorFactory.fromBitmap(bmp)));
            /*  Marker marker = googleMap.addMarker(new MarkerOptions().position(latLng)
                        .title("Picture Location")
                        .icon(BitmapDescriptorFactory.fromBitmap(bmp)));


                //googleMap.addMarker(marker);*/


            } catch (Exception e) {
                e.printStackTrace();
            }

            break;
        case R.id.ibCamera:
            i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, cameraData);
            break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            bmp = (Bitmap) extras.get("data");
            image.setImageBitmap(bmp);
        }
    }

}
activity 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" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <ImageView
        android:id="@+id/ivPic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/btMarker"
        android:layout_toLeftOf="@+id/btMarker"
        android:src="@drawable/common_signin_btn_icon_dark" />

    <ImageButton
        android:id="@+id/ibCamera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btMarker"
        android:layout_toLeftOf="@+id/btMarker"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/btMarker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="58dp"
        android:text="Set Picture marker" />

</RelativeLayout>

0 个答案:

没有答案