2016-04-29 2 views
0

setUpMapIfNeeded();というコマンドが見つからないため、以下のコードスニペットを投稿しました。必要なコマンドを入力すると、どこにでも赤い線が表示されます。誰もこのコードで私を助けることができますか?また、私のエミュレータにはマーカーがありません。MapsActivity.javaファイルにsetUpMapIfNeededライブラリがありません

私は提案がありますので、私を助けてください。私が使用しているmin sdkのバージョンは17、android studioのバージョンは2.1です。私はGoogleマップのアクティビティを使用しています。私はまだsetUpMapIfNeededコマンドを取得していない理由と、inbuiltコードスニペットの残りの部分を理解できません。

@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(R.id.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; 

     // Add a marker in Sydney and move the camera 
     LatLng sydney = new LatLng(-34, 151); 
     mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    } 
} 

Code screenshot

も不足しているコードを試した後、私は、任意のマーカーを取得できませんよ。 一方、私がオンラインで検索しているところでは、私はこれを得ています tutorial

他のコードを知っている場合は、教えてください。

+1

:自分のマーカーを追加する場合

また、このような次のコード

public void setMarkerOnMap(String name, LatLng l) { // Creating a marker MarkerOptions markerOptions = new MarkerOptions(); // Setting the position for the marker markerOptions.position(l); // Setting the title for the marker. // This will be displayed on taping the marker markerOptions.title(name); // Placing a marker on the touched position mMap.addMarker(markerOptions); } 

コールこの機能を使用します。 –

答えて

0

あなたはonCreate()方法でsetUpMapIfNeeded()を呼び出す必要があります:マーカーを追加する方法について

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    setUpMapIfNeeded(); 
} 

チェックこのtutorialSO question

0

MapsActivtyを使用する場合、余分なコードは必要ありません。すべての必要なコードは、Android Studio自体によって生成されます。チュートリアルを見ると、setUpMapIfNeeded()のコードはonCreate()のコードと同じです。 Google MapsActivtyを追加すると、地図を表示するにはGoogle Maps APIキーが必要です。 APiキーを "google_maps_api.xml"(値のフォルダ内)に貼り付けると、完了です。アプリケーションを実行すると、アクティビティにマップが表示されます。

GoogleマップAPIキーを取得する手順は、のgoogle_maps_api.xmlに記載されています。あなたは、彼らがあなたのリンクに行ったように、setUpMapIfNeeded機能をコーディングする必要が

LatLng loc=new LatLng(latitude,longitude); 
String str_placeName="Place Name"; 
setMarkerOnMap(str_placeName,loc); 
関連する問題