android中的谷歌地图,以小尺寸地图视图显示
我想在地图视图中显示位置&地图大小应为200X200。 我尝试在mapview中加载地图,但在点击方向指针2到3次显示正确地图但未移动后,地图未正确显示 但是,当我使用完全尺寸的屏幕来显示地图时,却没有更改任何代码,它正在正确加载。我如何才能在小视图中显示相同的地图 地图代码-android中的谷歌地图,以小尺寸地图视图显示,android,google-maps,google-maps-api-3,android-mapview,Android,Google Maps,Google Maps Api 3,Android Mapview,我想在地图视图中显示位置&地图大小应为200X200。 我尝试在mapview中加载地图,但在点击方向指针2到3次显示正确地图但未移动后,地图未正确显示 但是,当我使用完全尺寸的屏幕来显示地图时,却没有更改任何代码,它正在正确加载。我如何才能在小视图中显示相同的地图 地图代码- static GoogleMap map; private MapView mapView; MapsInitializer.initialize(getApplicationContext());
static GoogleMap map;
private MapView mapView;
MapsInitializer.initialize(getApplicationContext());
switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()) )
{
case ConnectionResult.SUCCESS:
//Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show();
mapView = (MapView)findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
if(mapView!=null)
{
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(location, 10);
map.animateCamera(cameraUpdate);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
break;
case ConnectionResult.SERVICE_MISSING:
//Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
break;
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
//Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
break;
default: Toast.makeText(getApplicationContext(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()), Toast.LENGTH_SHORT).show();
}
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(location).zoom(10).bearing(0) // Sets the orientation of the camera to east
.tilt(70) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.setBuildingsEnabled(true);
map.setMyLocationEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
解决方案-
使用片段完成-
map_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/mapfragment"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
class="com.google.android.gms.maps.SupportMapFragment" />
<TextView
android:id="@+id/reg_office_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/mapfragment"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="@string/tata_auto"
android:textColor="@color/tatablue"
android:textSize="18sp" />
</RelativeLayout>
Maddress 1是您所在位置的纬度和经度。我测试了下面的代码,大小为200dp x 200dp的地图没有问题 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical">
<fragment
android:id="@+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="200dp"
android:layout_height="200dp" />
</LinearLayout>
更新-2020年8月11日
MapView.getMap()
已替换为MapView.getMapAsync()
我看到您使用的是MapView而不是MapFragment。在Google Maps V2中,旧的MapView类
com.Google.android.Maps.MapView
已经过时。因此,请确保在Java代码和XML布局中使用正确的MapView,这应该是
import com.google.android.gms.maps.MapView;
及
这应该足以在您的活动中加载自定义大小的地图视图。您还可以使用以下功能使其符合您的要求
mMapView.setClickable(false);
mMap.getUiSettings().setMyLocationButtonnabled(false);
mMap.getUiSettings().setZoomControlsEnabled(false);
mMap.getUiSettings().setCompassEnabled(false);
mMap.getUiSettings().setZoomGesturesEnabled(false);
mMap.getUiSettings().setScrollGesturesEnabled(false);
mMap.setMyLocationEnabled(true);
你有解决方案吗?@LazyCoder我已经添加了解决方案。请检查它,不要显示你的解决方案solution@LazyCoder请查看我的帖子,仅在下面列出解决方案。我可以回答我自己的问题。。。因为需要像这样的东西谢谢谢洛!请提供有关您的解决方案的帮助。。inside Fragment为什么会给出错误“验证android xml文件内的资源引用”class=“com.google.android.gms.maps.SupportMapFragment”“maps.SupportMapFragment”标记为红色?mapView.getMap()不再显示available@YashaswiNP是的,他们用异步版本替换了函数。谢谢答案是6岁:)
import com.google.android.gms.maps.MapView;
<com.google.android.gms.maps.MapView
android:id="@+id/map_view"
android:layout_width="your_width"
android:layout_height="your_height" />
public class YourMapActivity extends Activity {
MapView mMapView;
GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Your OnCreate with Map Initialization */
mMapView = (MapView) findViewById(R.id.map_view);
mMapView.onCreate(Bundle.EMPTY);
mMap = mMapView.getMap();
MapsInitializer.initialize(this);
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onDestroy() {
mMapView.onDestroy();
super.onDestroy();
}
@Override
public void onLowMemory() {
mMapView.onLowMemory();
super.onLowMemory();
}
}
mMapView.setClickable(false);
mMap.getUiSettings().setMyLocationButtonnabled(false);
mMap.getUiSettings().setZoomControlsEnabled(false);
mMap.getUiSettings().setCompassEnabled(false);
mMap.getUiSettings().setZoomGesturesEnabled(false);
mMap.getUiSettings().setScrollGesturesEnabled(false);
mMap.setMyLocationEnabled(true);