创建菜单以在不同类型的地图之间切换(Android上的Google Map API)

时间:2017-03-10 19:19:37

标签: android google-maps-api-2

我正在 Android Studio 2.3 上开发一个带有 Google Map V2 的地图应用,我想创建一个允许在不同类型的地图之间切换的菜单。下面是我的MapActivity.java代码。请帮忙。

package net.dada.thm;

import android.database.sqlite.SQLiteOutOfMemoryException;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.firebase.appindexing.Action;
import com.google.firebase.appindexing.FirebaseUserActions;
import com.google.firebase.appindexing.builders.Actions;


import static net.dada.thm.R.id.map;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    public GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        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(map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in LOME 
        LatLng LOME = new LatLng(6.131944, 1.222778);
        mMap.addMarker(new MarkerOptions().position(LOME).title("LOME"));

    }

2 个答案:

答案 0 :(得分:0)

您可以尝试这种方法:

  • 使用以下内容创建一项活动:
  • MAPTYPE_ONE标签
  • MAPTYPE_TWO标签

示例

mMap.setBuildingsEnabled(true);
final float mCameraZoom = 10.00f;
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); //this part;
mMap.setTrafficEnabled(true);
mMap.setIndoorEnabled(true);

然后,当您选择第一个选项卡时,您可以看到第一个类型的地图,第二个类型,您可以相应地设置地图类型;

你真的不需要菜单;只有一个小ViewPager,上面有两个项目!

我希望它有所帮助!

答案 1 :(得分:0)

我能够使用onClick属性在LinearLayout中创建按钮来更改地图类型。但是,我在布局中丢失了应用初始状态。我不知道在哪里插入以下参数:

map:cameraTargetLat="8.6253268"
    map:cameraTargetLng="-1.4193078"
    map:cameraTilt="00"
    map:cameraZoom="6.5"
    map:cameraBearing="00"
    map:mapType="normal"
    map:uiCompass="true"
    map:uiRotateGestures="false"
    map:uiScrollGestures="true"
    map:uiTiltGestures="false"
    map:uiZoomControls="true"
    map:uiZoomGestures="true"

或者我在MapsActiviti.java文件中是否有一些与我想要添加到Activity_maps.xml文件的脚本不兼容的脚本?请帮忙。

Activity_maps.xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:hint="Search Location" />

        <Button
            android:id="@+id/search_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:onClick="onMapSearch"
            android:text="Search" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Type: " />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onNormalMap"
            android:text="Normal" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onSatelliteMap"
            android:text="Satellite" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onTerrainMap"
            android:text="Terrain" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onHybridMap"
            android:text="Hybrid" />

    </LinearLayout>

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="net.dada.thm.MapsActivity"/>
    </LinearLayout>

MapsActivity.java文件:

package net.dada.thm;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.EditText;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.io.IOException;
import java.util.List;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        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);
    }

    public void onMapSearch(View view) {
        EditText locationSearch = (EditText) findViewById(R.id.editText);
        String location = locationSearch.getText().toString();
        List<Address> addressList = null;

        Geocoder geocoder = new Geocoder(this);
        try {
            addressList = geocoder.getFromLocationName(location, 1);

        } catch (IOException e) {
            e.printStackTrace();
        }
        assert addressList != null;
        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 onNormalMap(View view) {
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    }

    public void onSatelliteMap(View view) {
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    }

    public void onTerrainMap(View view) {
        mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
    }


    public void onHybridMap(View view) {
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in TSEVIE
        LatLng TSEVIE = new LatLng(6.433333, 1.21666);
        mMap.addMarker(new MarkerOptions().position(TSEVIE).title("TSEVIE"));
        // mMap.moveCamera(CameraUpdateFactory.newLatLng(TSEVIE ));

        // Add a marker in ATAKPAME
        LatLng ATAKPAME = new LatLng(7.526944, 1.126669);
        mMap.addMarker(new MarkerOptions().position(ATAKPAME).title("ATAKPAME"));

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;

        }
        mMap.setMyLocationEnabled(true);
    }
}