如何处理地图碎片按钮?

Posted

技术标签:

【中文标题】如何处理地图碎片按钮?【英文标题】:How to deal with map fragment button? 【发布时间】:2017-06-30 07:45:24 【问题描述】:

我在选项卡活动中的地图片段上有按钮,如图所示,但我不知道在哪里实现此按钮的 onClickListener 和其他地图功能 onLocationChanged ,onConnectionSuspened 等请帮忙!

已编辑 添加代码

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_
android:layout_
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_
    android:layout_
    android:text="Nearby Resturants"
    android:id="@+id/btn_rest"
    android:layout_gravity="bottom" />

【问题讨论】:

哪个按钮?你是说右上角带有消息图标的浮动 Actin 按钮吗? 顶部没有按钮(附近的餐厅)@tahsinRupam 你可以在oncreate中实现。 它不工作:| @AAA 它只显示按钮 @android 它应该可以工作。如果没有,请显示代码 【参考方案1】:

你可以参考下面这段代码,希望对你有帮助,如果你能提供你的代码就更好了。

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();

【讨论】:

我从标签活动中调用了地图片段,所以它只显示按钮,在 onClickListener 上不做任何事情

以上是关于如何处理地图碎片按钮?的主要内容,如果未能解决你的问题,请参考以下文章

如何处理隐藏 div 内的谷歌地图(更新图片)

如何处理 android.view.InflateException?当我尝试打开地图时出现此错误

如何处理 QSslSocket:无法解决 TLSv1_1_client_method 错误

在ios中如果按钮的位置超出父视图无法响应.如何处理?

echarts绘制geo地图

如何处理自定义结果视图的 NSFetchedResultsControllerDelegate 更新