2017-02-12 22 views
-2

У меня есть кнопка на моем фрагменте карты в активности вкладок, как показано на рисунке, но я понятия не имею, где реализовать onClickListener этой кнопки и другие функции карты onLocationChanged, onConnectionSuspened и т. Д., Пожалуйста, помогите !!Как бороться с кнопкой фрагмента карты?

EditedДобавить код

MapsActivity.Java

public class MapsActivity extends ActionBarActivity implements 
    AsyncResponse, View.OnClickListener, 
    OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, 
    LocationListener { 
     protected void onCreate(Bundle savedInstanceState) { 
    Log.i("okkk", " ho jamaaloooo "); 
    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); 
    Intent intent = getIntent(); 
    newString = intent.getLongExtra("user_id", newString); 
    btn_rest = (Button) findViewById(R.id.btn_rest); 
    btn_rest.setOnClickListener(this); 
} 
@Override 
public void onClick(View v) { 
    // getPars 
    Log.i("okk", "Click "); 

    HashMap postData = new HashMap(); 
    PostResponseAsyncTask task = new PostResponseAsyncTask(this, postData); 
    task.execute("http://ashna.netau.net/A_location.php"); 
} 

TabACtivity.java вызова карта фрагмент здесь

 public Fragment getItem(int position) { 
     Log.i("okkk", "Position: " + position); 
     try { 
      switch (position) { 
       case 0: { 
        return PlaceholderFragment.newInstance(position + 1); 
       } 
       case 1: { 
        Deals deals = new Deals(); 
        return deals; 
       } 
      } 
     } 
     catch (Exception e) 
     { 
      Log.i("okkk", "Exception 1 : " + e); 
     } 
     return PlaceholderFragment.newInstance(position + 1); 
    } 

MapActivity.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/map" 
tools:context=".MapsActivity" 
android:name="com.google.android.gms.maps.SupportMapFragment" 
tools:layout="@layout/abc_action_menu_layout"/> 

<Button 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Nearby Resturants" 
    android:id="@+id/btn_rest" 
    android:layout_gravity="bottom" /> 

enter image description here

+0

Какая кнопка? вы говорите о плавающей кнопке Actin в правом углу с иконкой сообщения? – tahsinRupam

+0

Нет кнопки наверху (рядом с рестораном) @tahsinRupam – SFAH

+0

вы можете реализовать в oncreate. – AAA

ответ

0

Вы можете передать этот код, приведенный ниже, надеюсь, что это поможет, но это лучше, если вы можете предоставить свой код.

public class MapFragment extends BaseFragment implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 

private Location mCurrentLocation; 
private MapView mMapView; 
private GoogleMap mGoogleMap; 
private LocationManager locationManager; 
private String provider; 
private GoogleApiClient mGoogleApiClient; 
private LocationRequest mLocationRequest; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_map, null); 

    mMapView = (MapView) view.findViewById(R.id.map); 
    mMapView.onCreate(savedInstanceState); 

    Button nearBy = (Button)view.findViewById(R.id.nearby_button); 
    nearBy.setOnClickListener(new View.OnClickListener(){ 
     // DO SOMETHING FOR NEARBY BUTTOM 
    } 

    return view; 

} 

@Override 
public void onResume() { 
    mMapView.onResume(); 
    super.onResume(); 

    initMap(); 

} 

@Override 
public void onPause() { 
    super.onPause(); 
    mMapView.onPause(); 

} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    mMapView.onDestroy(); 
} 

private void initMap() { 
    Log.d("MapFragment", "init map"); 

    mMapView.getMapAsync(new OnMapReadyCallback() { 
     @Override 
     public void onMapReady(GoogleMap googleMap) { 
      mGoogleMap = googleMap; 
      mGoogleMap.setInfoWindowAdapter(new PopupAdapter(mContext, getLayoutInflater(null))); 
      mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { 
       @Override 
       public boolean onMarkerClick(Marker marker) { 
        marker.showInfoWindow(); 
        return true; 
       } 
      }); 
      MapsInitializer.initialize(mContext); 
     } 
    }); 

} 

private void locateMyLocation(Location currentLocation) { 
    mGoogleMap.addMarker(new MarkerOptions() 
      .title(mContext.getResources().getString(R.string.map_user_location_title)) 
      .anchor(0.0f, 1.0f) 
      .position(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()))); 
    mGoogleMap.getUiSettings().setMyLocationButtonEnabled(false); 
    mGoogleMap.getUiSettings().setZoomControlsEnabled(false); 
    mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()), 15.0f)); 
} 

@Override 
public void onLocationChanged(Location location) { 
    mCurrentLocation = location; 

    locateMyLocation(mCurrentLocation); 

    //stop location updates 
    if (mGoogleApiClient != null) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
    } 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(1000); 
    mLocationRequest.setFastestInterval(1000); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

protected synchronized void buildGoogleApiClient() { 
    Log.d("MapFragment", "buildGoogleApiClient"); 

    mGoogleApiClient = new GoogleApiClient.Builder(mContext) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
    mGoogleApiClient.connect(); 
} 

}

+0

Я назвал фрагмент карты из активности вкладок, поэтому он отображает только кнопку, не выполняющую никаких действий onClickListener – SFAH

 Смежные вопросы

  • Нет связанных вопросов^_^