代码之家  ›  专栏  ›  技术社区  ›  Ben FM

在android中向Mapview投射地图片段

  •  1
  • Ben FM  · 技术社区  · 8 年前

    我正在开发一个应用程序,它允许用户在地图上选择一个特定的位置并保存它的坐标。

    package com.example.getfamiliarwiththemap;
    
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.view.Gravity;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.FrameLayout;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.MapView;
    import com.google.android.gms.maps.OnMapReadyCallback;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.Marker;
    import com.google.android.gms.maps.model.MarkerOptions;
    
    import static com.example.getfamiliarwiththemap.R.id.map;
    
    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    
        private GoogleMap mMap;
        private Button ok;
    
        static final LatLng HAMBURG = new LatLng(53.558, 9.927);
        static final LatLng KIEL = new LatLng(53.551, 9.993);
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);
            // Obtain the SupportMapFragment and get notified when the map is ready to be used.
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(map);
            mapFragment.getMapAsync(this);
    
    
    
        }
    
    
        /**
         * Manipulates the map once available.
         * This callback is triggered when the map is ready to be used.
         * This is where we can add markers or lines, add listeners or move the camera. In this case,
         * we just add a marker near Sydney, Australia.
         * If Google Play services is not installed on the device, the user will be prompted to install
         * it inside the SupportMapFragment. This method will only be triggered once the user has
         * installed Google Play services and returned to the app.
         */
        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;
    
            // Create pin
            ImageView pin = new ImageView(MapsActivity.this);
            pin.setImageResource(R.drawable.common_google_signin_btn_icon_light);
            // Position pin
            FrameLayout.LayoutParams parameters = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
            float density = getResources().getDisplayMetrics().density;
    
            parameters.bottomMargin = (int) (12 * density);
            pin.setLayoutParams( parameters);
    
            MapView mapView = (MapView) findViewById(R.id.map) ;
    
            mapView.addView( pin);
    
    
    
    
    
            ok = (Button) findViewById(R.id.ok);
            ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
    
    
    
    
                }
            });
    
    
    
            if (mMap != null){
                Marker hamburg = mMap.addMarker(new MarkerOptions().position(HAMBURG)
                        .title("Hamburg"));
                Marker kiel = mMap.addMarker(new MarkerOptions()
                        .position(KIEL)
                        .title("Kiel")
                        .snippet("Kiel is cool"));
                mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                    @Override
                    public boolean onMarkerClick(Marker marker) {
                        Toast.makeText(MapsActivity.this,"You clicked a marker named " + marker.getTitle().toString(),Toast.LENGTH_SHORT).show();
                        return false;
                    }
                });
    
                // Move the camera instantly to hamburg with a zoom of 15.
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
                // Zoom in, animating the camera.
                mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
            }
        }
    } 
    

    MapView mapView = (MapView) findViewById(R.id.map) ;
    

    我应该如何将mapFragment转换为Mapview以添加视图?我对安卓很陌生,所以任何简单的答案都会很感激。

    P、 S:我的XML布局代码:

    <com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.getfamiliarwiththemap.MapsActivity" >
    
    
    
        <LinearLayout
            android:id="@+id/mLlayoutBottomButtons"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:orientation="vertical" >
    
        <Button
    
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select"
        android:textColor="#ffec00"
        android:background="#b600ff"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="500dp"
            android:layout_marginLeft="150dp"
            android:layout_marginRight="150dp"
    
        />
    
        </LinearLayout>
    
    </fragment>
    
        </com.google.android.gms.maps.MapView>
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Daniel RL    8 年前

    在xml布局中添加如下地图视图:

                <com.google.android.gms.maps.MapView
                    android:id="@+id/map"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
    
        2
  •  0
  •   Daniel RL    8 年前

    为什么不直接在XML布局中声明地图视图?

    推荐文章