0

Google mapにマーカーを作成して追加しました。ユーザーの位置を入力して、ユーザーの位置に基づいて別のマーカーを追加し、その間に線を描画します。Googleマップにマーカーを動的に追加するには

これは私が行ったことです。

 //OnCreate() 
    mClient= new GoogleApiClient.Builder(this). 
       addConnectionCallbacks(this). 
       addOnConnectionFailedListener(this). 
       addApi(Places.GEO_DATA_API).enableAutoManage(this, this).build(); 




     mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 

    /** 
     * 
     * @PLACES API 
     */ 


     PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) 
       getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment); 

     autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() { 
      @Override 
      public void onPlaceSelected(Place place) { 
       // TODO: Get info about the selected place. 
       Log.i("Place Name", "Place: " + place.getName()); 
       LatLng latLng = place.getLatLng(); 
       destinationLat= latLng.latitude; 
       destinationLng= latLng.longitude; 


       destinationLatLng= new LatLng(destinationLat, destinationLng); 
       /* mapFragment.addMarker(new MarkerOptions().position(destinationLatLng) 
         .title("PAF-KIET"));*/ 
      } 

      @Override 
      public void onError(Status status) { 
       // TODO: Handle the error. 
       Log.i("Error", "An error occurred: " + status); 
      } 
     }); 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     LatLng pafKietLocation = new LatLng(24.865498, 67.073302); 
     googleMap.addMarker(new MarkerOptions().position(pafKietLocation) 
       .title("University")); 
     googleMap.setMinZoomPreference(14.0f); 
     googleMap.setMaxZoomPreference(74.0f); 
     googleMap.moveCamera(CameraUpdateFactory.newLatLng(pafKietLocation)); 

    } 

問題は、私が地図にユーザーLatLngを渡すのいずれかの方法を見つけることができませんということです。

+0

「ユーザーLatLng」とはどういう意味ですか? –

+0

@GurgenHakobyan: 'onMapReady'では、あらかじめ定義された' Lat Lng'値を持つマーカーを追加しています。これで 'onPlaceSelected()'にもメーカーを追加します。 – Kirmani88

答えて

1

あなたの活動にGoogleMap mapと宣言し、onMapReady(GoogleMap googleMap)this.map = googleMapを追加してください。 this.mapを使用する場合は、常にnullを確認してください。 Gurgenが言ったようonPlaceSelected

if(this.map != null) 
{ 
    this.map.addMarker(new MarkerOptions().position(destinationLatLng) 
        .title("PAF-KIET")); 
} 
1

では、あなたがそれを必要なときにそれを使用するGoogleマップのグローバルインスタンスを使用しますが、最初にあなたのonCreate()メソッドにマップのインスタンスを取得する必要があります。

関連する問題