android中的Google地图

android中的Google地图

本文介绍了android中的Google地图,以小尺寸的mapview显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在地图视图中显示位置&地图大小应该是200X200。
我试图在mapview中加载地图,但是在点击方向指针2到3次后,它不能正确显示它显示正确的地图但不移动。

=https://i.stack.imgur.com/70z4C.pngalt =map>



但是当我使用完整尺寸的屏幕显示地图而不会改变任何代码加载正确。我如何显示相同的iin小视图?



地图代码 -

 静态GoogleMap地图; 
私人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);
//从MapView获取到GoogleMap并执行初始化操作
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();
休息;
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
//Toast.makeText(getActivity(),UPDATE REQUIRED,Toast.LENGTH_SHORT).show();
休息;
default:Toast.makeText(getApplicationContext(),GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()),Toast.LENGTH_SHORT).show();


CameraPosition cameraPosition = new CameraPosition.Builder()
.target(location).zoom(10).bearing(0)//设置相机的方向为east
.tilt(70)//将相机的倾斜角度设置为30度
.build(); //从构建器创建一个CameraPosition
map.setBuildingsEnabled(true);
map.setMyLocationEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

解决方案 -
使用片段完成 -



map_screen.xml

 <?xml version =1.0encoding = 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_horizo​​ntal
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_horizo​​ntal
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>

java代码 -

  public class MapFragment extends Fragment {
$ b $ private view View view;

公开Goog​​leMap地图;
私人位置位置;
私人LatLng mAddress1;
public static boolean inMap = false;

@Override
public查看onCreateView(LayoutInflater inflater,ViewGroup容器,Bundle savedInstanceState){
// TODO自动生成的方法存根
view = inflater.inflate( R.layout.contact_map,container,false);
setMapView();
返回视图;


private void setMapView(){
CurrentLocation mCurrentLoc = new CurrentLocation(getActivity());
location = CurrentLocation.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location!= null){
mCurrentLoc.onLocationChanged(location);
} else {
mCurrentLoc.onLocationChanged(location);
}

//class=\"com.google.android.gms.maps.SupportMapFragment

map =((SupportMapFragment)getActivity()。getSupportFragmentManager( ).findFragmentById(R.id.mapfragment))的GetMap();


CameraPosition cameraPosition = new CameraPosition.Builder()
.target(mAddress1).zoom(8).bearing(0)//设置摄像机的方向为东
.tilt(30)//将相机的倾斜角度设置为30度
.build(); //从构建器创建一个CameraPosition
map.setBuildingsEnabled(true);
map.setMyLocationEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));


}
}

mAddress1是纬度和经度您的位置。

解决方案

我测试了下面的代码,并且没有问题,地图尺寸为200dp x 200dp



布局:

 <?xml version =1.0编码= 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>

碎片:

  public class LocationFragment extends Fragment 
{
GoogleMap googleMap;

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_location,container ,假);
$ b googleMap =((SupportMapFragment)getFragmentManager()。findFragmentById(R.id.map))。getMap();

MapsInitializer.initialize(getActivity()。getApplicationContext()); (新的LatLng(38.7222524,-9.139336599999979))
.title(我的位置)
.icon($)
$ b googleMap.addMarker(新的MarkerOptions() BitmapDescriptorFactory
.fromResource(R.drawable.ic_mobileedge_navpoint)));

//以15倍的放大比例立即移动相机。
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(38.7222524,-9.139336599999979),15));

//放大,使相机动画。
googleMap.animateCamera(CameraUpdateFactory.zoomTo(12),1000,null);

return v;
}
}


I want to display location in map view & map size should be 200X200.I tried loading map in mapview but its not displaying properly after tapping on direction pointer 2 to 3 times its displaying correct map but not moving.

But when I use complete size of screen for displaying map without changing any code it's loading correctly.How can i display same iin small view?

map code-

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

Solution -Done using fragment -

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>

java code -

public class MapFragment extends Fragment {

    private View view;

    public  GoogleMap map;
    private Location location;
    private LatLng mAddress1;
    public static boolean inMap=false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        view= inflater.inflate(R.layout.contact_map, container, false);
        setMapView();
        return view;
    }

    private void setMapView(){
        CurrentLocation mCurrentLoc=new CurrentLocation(getActivity());
        location = CurrentLocation.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            mCurrentLoc.onLocationChanged(location);
        } else {
            mCurrentLoc.onLocationChanged(location);
        }

        //class="com.google.android.gms.maps.SupportMapFragment"

        map = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.mapfragment)).getMap();


        CameraPosition cameraPosition = new CameraPosition.Builder()
        .target(mAddress1).zoom(8).bearing(0) // Sets the orientation of the camera to east
        .tilt(30)    // 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));


    }
}

mAddress1 is latitude and longitude of your location.

解决方案

I tested the code below and there is no problems with the map to the size 200dp x 200dp

Layout:

<?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>

Fragment:

public class LocationFragment extends Fragment
{
    GoogleMap googleMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View v = inflater.inflate(R.layout.fragment_location, container, false);

        googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        MapsInitializer.initialize(getActivity().getApplicationContext());

        googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(38.7222524, -9.139336599999979))
                .title("MyLocation")
                .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.ic_mobileedge_navpoint)));

        // Move the camera instantly  with a zoom of 15.
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom( new LatLng( 38.7222524, -9.139336599999979), 15));

        // Zoom in, animating the camera.
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(12), 1000, null);

        return v;
    }
}

这篇关于android中的Google地图,以小尺寸的mapview显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 06:15