错误:找不到符号变量映射

时间:2015-04-17 02:47:24

标签: google-maps spinner

我正在使用android studio编写googlemap活动

mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.**map**))
                    .getMap();

" map"在我运行程序之后变为红色(之前没有错误)

这是我的代码

主要活动:

package com.example.albert.lab_assignment2;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

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

import java.util.ArrayList;
import java.util.List;

public class MapsActivity extends FragmentActivity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

    Marker markerParis, markerLondon, markerNewYork, markerTokyo, markerHongKong;
    LatLng HongKong, Paris, London, NewYork, Tokyo;
    private Spinner spinner2;
    Button btnParis, btnLondon, btnNewYork;




    private void addItemOnSpinner2() {




        spinner2 = (Spinner) findViewById(R.id.spinner2);
        List list = new ArrayList();

        list.add("Hong Kong");
        list.add("London");
        list.add("New York");
        list.add("Paris");
        list.add("Tokyo");

        ArrayAdapter dataAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(
                android.R.layout.simple_spinner_dropdown_item);

        spinner2.setAdapter(dataAdapter);
        String.valueOf(spinner2.getSelectedItem());

        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            String message = "Selected place:" + parent.getItemAtPosition(position).toString();
                   Toast.makeText(parent.getContext(),message,Toast.LENGTH_LONG).show();

                String place = parent.getItemAtPosition(position).toString();

                if (place.equals("Hong Kong")){

                    goHongKong();

                }

                else if (place.equals("Paris")){

                    goParis();

                }

                else if (place.equals("New York")){

                    goNewYork();

                }

                else if (place.equals("London")){

                    goLondon();

                }

                else if (place.equals("Tokyo")){

                    goTokyo();

                }


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    private void goTokyo() {

        mMap.clear();

        double distance = computeDistance(Tokyo);
        String distMsg = "Distance from HK:" + Math.round(distance) + "km";

        Toast.makeText(getApplicationContext(), distMsg, Toast.LENGTH_LONG).show();

        markerTokyo = mMap.addMarker(new MarkerOptions()
                .position(Tokyo)
                .title("Tokyo")
                .snippet("Population"));

        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(NewYork, 10));

    }


    private void goHongKong() {

        mMap.clear();

        double distance = computeDistance(HongKong);
        String distMsg = "Distance from HK:" + Math.round(distance) + "km";

        Toast.makeText(getApplicationContext(), distMsg, Toast.LENGTH_LONG).show();

        markerHongKong = mMap.addMarker(new MarkerOptions()
                .position(HongKong)
                .title("Hong Kong")
                .snippet("Population"));

        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(HongKong, 10));

    }


    private void goLondon() {

        mMap.clear();

        double distance = computeDistance(London);
        String distMsg = "Distance from HK:" + Math.round(distance) + "km";

        Toast.makeText(getApplicationContext(), distMsg, Toast.LENGTH_LONG).show();

        markerLondon = mMap.addMarker(new MarkerOptions()
                .position(London)
                .title("London")
                .snippet("Population"));

        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(London, 10));

    }

    private void goNewYork() {

        mMap.clear();

        double distance = computeDistance(NewYork);
        String distMsg = "Distance from HK:" + Math.round(distance) + "km";

        Toast.makeText(getApplicationContext(), distMsg, Toast.LENGTH_LONG).show();

        markerNewYork = mMap.addMarker(new MarkerOptions()
                .position(NewYork)
                .title("New York")
                .snippet("Population"));

        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(NewYork, 10));


    }

    private void goParis() {
        mMap.clear();

        double distance = computeDistance(Paris);
        String distMsg = "Distance from HK:" + Math.round(distance) + "km";

        Toast.makeText(getApplicationContext(), distMsg, Toast.LENGTH_LONG).show();

        markerParis = mMap.addMarker(new MarkerOptions()
                .position(Paris)
                .title("Paris")
                .snippet("Population"));

        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(Paris, 10));

    }

    private double computeDistance(LatLng dest) {
        int R = 6371; //Earth's radius in km

        double dLat = Math.toRadians(HongKong.latitude - dest.latitude) ;
        double dLon = Math.toRadians(HongKong.longitude - dest.longitude);

        double lat1 = Math.toRadians(dest.latitude);
        double lat2 = Math.toRadians(HongKong.latitude);


        double a =  Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) *
                Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);

        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        return R * c;

    }

    private void HomeLocation() {
        mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
            @Override
            public boolean onMyLocationButtonClick() {

                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(HongKong, 14));

                return false;
            }
        });
    }

    private void initializingCity() {

        HongKong = new LatLng(22.2783, 114.1747);
        Paris = new LatLng(48.8567, 2.3508);
        London = new LatLng(51.518815, -0.1275);
        NewYork = new LatLng(40.713723, -74.002640);
        Tokyo = new LatLng(35.6833, 139.6833);

    }




    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    /**
     * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
     * installed) and the map has not already been instantiated.. This will ensure that we only ever
     * call {@link #setUpMap()} once when {@link #mMap} is not null.
     * <p/>
     * If it isn't installed {@link SupportMapFragment} (and
     * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
     * install/update the Google Play services APK on their device.
     * <p/>
     * A user can return to this FragmentActivity after following the prompt and correctly
     * installing/updating/enabling the Google Play services. Since the FragmentActivity may not
     * have been completely destroyed during this process (it is likely that it would only be
     * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
     * method in {@link #onResume()} to guarantee that it will be called.
     */
    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();
            }
        }
    }

    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case, we
     * just add a marker near Africa.
     * <p/>
     * This should only be called once and when we are sure that {@link #mMap} is not null.
     */

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(HongKong).title("Marker"));
        mMap.animateCamera(CameraUpdateFactory.newLatLng(HongKong));
        mMap.getUiSettings().setZoomControlsEnabled(true);
        mMap.getUiSettings().setScrollGesturesEnabled(true);
        mMap.getUiSettings().setTiltGesturesEnabled(true);
        mMap.getUiSettings().setRotateGesturesEnabled(true);
        mMap.setMyLocationEnabled(true);
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    }
}

activity_maps.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/select2" />

</LinearLayout>

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <string name="app_name">Lab_Assignment2</string>
    <string name="title_activity_maps">Map</string>
    <string name="select2">Choose an item</string>   //spinner

</resources>

请帮忙

2 个答案:

答案 0 :(得分:1)

您的fragment中没有任何xml地图标记。

在相关的xml中添加以下内容:

<fragment
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment" />

此外,请尝试逐步关注this,最后您将在手机上获得地图。对于整个地图示例,您可以参考我的github here

答案 1 :(得分:0)

尝试将其放在gradle.properties中

android.enableJetifier=true