2017-06-30 48 views
0

我正在研究一個應用程序,該應用程序允許用戶選擇地圖上的特定位置並保存其座標。 我使用這樣的MapsActivity:在android中將地圖片段投射到Mapview

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能夠補充意見?我很新的android,所以任何簡單的答案將不勝感激。

**** ****解決最後的XML:

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> 

回答

0

添加在XML佈局MapView的是這樣的:

  <com.google.android.gms.maps.MapView 
       android:id="@+id/map" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 
+0

This works.Thanks! –

0

你爲什麼不聲明直接在XML佈局一個MapView?

+0

,因爲我不知道how.In事實上,我甚至不知道這是正確的,直到知道一個選項!你能特意輸入代碼嗎? –