2016-06-28 50 views
0

我在onPlaceSelected(Place place)方法中的主要活動我想在地圖片段中使用此設置標記位置在選定的位置。地圖片段是主活動中的一個片段(在導航抽屜中) ,我怎樣才能做到這一點 這裏是我的代碼..設置標記在自動完成搜索位置

@Override 
public void onPlaceSelected(Place place) { 
    Log.i(TAG, "Place Selected: " + place.getName()); 

    // Format the returned place's details and display them in the TextView. 
    mPlaceDetailsText.setText(formatPlaceDetails(getResources(), place.getName(), place.getId(), 
      place.getAddress(), place.getPhoneNumber(), place.getWebsiteUri(),place.getLatLng())); 

    final CharSequence name = place.getName(); 
    final CharSequence address = place.getAddress(); 
    final LatLng location = place.getLatLng(); 
    AppConstants.SHOW_SEARCH_LOCATION=address; 
    AppConstants.SHOW_SEARCH_LATLNG=location; 
    Log.d("placedetails",name+","+address+" "+location); 

    CharSequence attributions = place.getAttributions(); 
    if (!TextUtils.isEmpty(attributions)) { 
     mPlaceAttribution.setText(Html.fromHtml(attributions.toString())); 
    } else { 
     mPlaceAttribution.setText(""); 
    } 
} 

回答

1

你只需要

mMap.addMarker(new MarkerOptions().position(place.getLatLng())); 

添加到您的onPlaceSelected方法。

mMap是地圖的實例,你可以得到這樣的:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 
    private GoogleMap mMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_maps); 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 

    @Override 
    public void onMapReady(GoogleMap map) { 
     this.mMap = map; 
    } 
} 
+0

我有getLagLng()在主activity.how我得到它MapActivity擴展類片段 –