在地图片段上添加按钮以在地图上执行操作
Posted
技术标签:
【中文标题】在地图片段上添加按钮以在地图上执行操作【英文标题】:add button on map fragment to perform action on the map 【发布时间】:2018-05-15 10:38:41 【问题描述】:我正在开发一个简单的地图应用程序,我使用navigationDrawer
将我的地图放在drawer
的第一个选项中。现在问题是当我将任何按钮放在同一个fragment
中时地图或相同的布局它不执行我想要的!
例如:当我单击button
时,我需要放大我的位置。
我尝试了一些在网站上找到的解决方案,但没有运气..
xml 代码:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_>
<fragment android:layout_
android:layout_ android:id="@+id/mapfragment"
class="com.google.android.gms.maps.MapFragment"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="8dp"
/>
<RelativeLayout android:layout_
android:layout_>
<Button
android:id="@+id/bt1"
android:layout_
android:layout_
android:layout_marginBottom="46dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:onClick="onClick"
/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
java代码:
public class maptest extends Fragment implements OnMapReadyCallback,View.OnClickListener
GoogleMap mp;
@Nullable
View v1;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
Button bt2=(Button)v1.findViewById(R.id.bt1);
bt2.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Toast.makeText(getActivity(), "ggg", Toast.LENGTH_SHORT).show();
);
return inflater.inflate(R.layout.newmapfragment, container,false);
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
MapFragment fragment= (MapFragment) getChildFragmentManager().findFragmentById(R.id.mapfragment);
fragment.getMapAsync(this);
@Override
public void onMapReady(GoogleMap mp)
// Add a marker in Sydney, Australia,
// and move the map's camera to the same location.
LatLng sydney = new LatLng(31.163937, 35.710373);
mp.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
mp.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mp.addMarker(new MarkerOptions().position(new
LatLng(31.163937,35.710373)).title("mymap")).showInfoWindow();
mp.animateCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(31.163937,35.710373),16));
@Override
public void onClick(View view)
Button b = (Button) view.findViewById(R.id.bt1);
b.setOnClickListener(this);
LatLng sydney = new LatLng(31.163937, 35.710373);
mp.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
mp.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mp.addMarker(new MarkerOptions().position(new
LatLng(31.163937,35.710373)).title("mymap")).showInfoWindow();
mp.animateCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(31.163937,35.710373),16));
任何帮助将不胜感激..
【问题讨论】:
为什么你的按钮有两个 onClick 方法? 【参考方案1】:您可能在片段的 onCreateView 中被遗忘了:
vi=inflater.inflate(your_fragment_layout_xml,container,false);
【讨论】:
【参考方案2】:用下面的代码替换你的 maptest 片段
public class maptest extends Fragment implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener
GoogleMap mMap;
double latitude;
double longitude;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker;
LocationRequest mLocationRequest;
View v1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
v1 = inflater.inflate(R.layout.fragment_map, container, false);
((SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map)).getMapAsync(this);
//Check if Google Play Services Available or not
if (!CheckGooglePlayServices())
Log.d("onCreate", "Finishing test case since Google Play Services are not available");
else
Log.d("onCreate", "Google Play Services available.");
Button bt2=(Button)v1.findViewById(R.id.bt1);
bt2.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
if(mLastLocation!=null && mMap!=null)
/////to change map type
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//////to zoom on current location
LatLng sydney = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(sydney)
.title("current location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
);
return v1;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
MapFragment fragment= (MapFragment) getChildFragmentManager().findFragmentById(R.id.mapfragment);
fragment.getMapAsync(this);
private boolean CheckGooglePlayServices()
GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
int result = googleAPI.isGooglePlayServicesAvailable(context);
if (result != ConnectionResult.SUCCESS)
if (googleAPI.isUserResolvableError(result))
googleAPI.getErrorDialog((Activity) context, result,
0).show();
return false;
return true;
protected synchronized void buildGoogleApiClient()
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
@Override
public void onConnected(Bundle bundle)
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
@Override
public void onConnectionSuspended(int i)
@Override
public void onLocationChanged(Location location)
Log.d("onLocationChanged", "entered");
mLastLocation = location;
if (mGoogleApiClient != null)
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
Log.d("onLocationChanged", "Removing Location Updates");
Log.d("onLocationChanged", "Exit");
@Override
public void onConnectionFailed(ConnectionResult connectionResult)
@Override
public void onMapReady(GoogleMap googleMap)
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//Initialize Google Play Services
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (ContextCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED)
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
else
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
@Override
public void onResume()
super.onResume();
【讨论】:
是的先生..我需要从我的按钮得到任何响应我的意思是我放大我的位置或更改地图类型(卫星、地形)我似乎没有从我的按钮得到任何东西它没有连接任何功能...【参考方案3】:试试这个你忘了在onCreateView
中的v1
中充气
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
v1=inflater.inflate(your_fragment_layout_xml,container,false);
Button bt2=(Button)v1.findViewById(R.id.bt1);
bt2.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Toast.makeText(getActivity(), "ggg", Toast.LENGTH_SHORT).show();
);
return v1;
EDIT-1
将此移至Fragment
Button b = (Button) v1.findViewById(R.id.bt1);
b.setOnClickListener(this);
EDIT-2
从Button
中的XML
中删除android:onClick="onClick"
【讨论】:
我用这个替换了我的代码,但一切都一样,没有任何改变【参考方案4】:首先删除任何一个 onclick 方法,因为现在您正在使用两个 onclick 方法。你应该像这样添加按钮:-
<RelativeLayout android:layout_
android:layout_>
<Button
android:id="@+id/bt1"
android:layout_
android:layout_
android:layout_marginBottom="46dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
</RelativeLayout>
View v1;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
v1 = inflater.inflate(R.layout.newmapfragment, container, false);
Button bt2=(Button)v1.findViewById(R.id.bt1);
bt2.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Toast.makeText(getActivity(), "ggg", Toast.LENGTH_SHORT).show();
);
return v1;
点击here!用于获取当前位置。
【讨论】:
以上是关于在地图片段上添加按钮以在地图上执行操作的主要内容,如果未能解决你的问题,请参考以下文章