如何在片段中使用Google地图
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在片段中使用Google地图相关的知识,希望对你有一定的参考价值。
我正在尝试将此类放在一个片段中,如何将其放入一个片段类中?
public class MapsActivity extends FragmentActivity
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
@Override
protected void onResume()
super.onResume();
setUpMapIfNeeded();
我需要在我的片段中使用这些方法。但是我无法通过按钮开车]
public void onSearch(View view)
EditText location_tf = (EditText) findViewById(R.id.TFaddress);
String location = location_tf.getText().toString();
List<Address> addressList = null;
if (location != null || !location.equals(""))
Geocoder geocoder = new Geocoder(this);
try
addressList = geocoder.getFromLocationName(location, 1);
catch (IOException e)
e.printStackTrace();
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
public void onZoom(View view)
if (view.getId() == R.id.Bzoomin)
mMap.animateCamera(CameraUpdateFactory.zoomIn());
if (view.getId() == R.id.Bzoomout)
mMap.animateCamera(CameraUpdateFactory.zoomOut());
public void changeType(View view)
if (mMap.getMapType() == GoogleMap.MAP_TYPE_NORMAL)
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
else
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
private void setUpMapIfNeeded()
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null)
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
setUpMap();
所有这些操作都通过“ onClick”连接到布局。
private void setUpMap()
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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.
return;
mMap.setMyLocationEnabled(true);
我正在尝试将此类放在一个片段类中,如何将其放入一个片段类中?公共类MapsActivity扩展FragmentActivity 私有GoogleMap mMap; //如果Google Play ...可能为null ...
答案
片段类应该这样...
另一答案
在我的项目中,地图是通过以下方式实现的:
另一答案
这对我有用的是
以上是关于如何在片段中使用Google地图的主要内容,如果未能解决你的问题,请参考以下文章
使用 TabLayout 在 ViewPager 中的 Google 地图片段中膨胀异常
如何在 Fragment 中获取 Google Maps 对象
片段中的 Xamarin Android Google 地图错误