Google Maps Android API V2 problems with android 4.3

后端 未结 5 1959
栀梦
栀梦 2021-01-28 00:54

I am novice in Google maps. Just followed this link http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/ when I am launching my app, follwing error is coming

相关标签:
5条回答
  • 2021-01-28 01:06
    package com.synergyifs.libapp;
    
    import android.Manifest;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.content.Context;
    import android.content.ContextWrapper;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.location.Address;
    import android.location.Criteria;
    import android.location.Geocoder;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.provider.Settings;
    import android.support.v4.content.ContextCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AutoCompleteTextView;
    import android.widget.Toast;
    
    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.GoogleApiAvailability;
    import com.google.android.gms.common.GooglePlayServicesUtil;
    import com.google.android.gms.common.api.GoogleApiClient;
    import com.google.android.gms.location.LocationServices;
    import com.google.android.gms.location.places.Place;
    import com.google.android.gms.location.places.ui.PlacePicker;
    import com.google.android.gms.maps.CameraUpdate;
    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.Marker;
    import com.google.android.gms.maps.model.MarkerOptions;
    import com.synergyifs.libapp.adapter.PlacesAutoCompleteAdapter;
    
    import java.util.List;
    import java.util.Locale;
    
    /**
     * Created by Admin34 on 23-03-2016.
     */
    public class MapDemoActivity extends AppCompatActivity implements OnMapReadyCallback,
            GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener,
            GoogleMap.OnMarkerDragListener, GoogleMap.OnMapLongClickListener {
    
        int PLACE_PICKER_REQUEST = 1;
        private GoogleMap map;
        private LatLng synergyIFS = new LatLng(18.511478, 73.801217);
        private GoogleApiClient googleApiClient;
        private double longitude;
        private double latitude;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.map_activity);
    
            SupportMapFragment fragment = (SupportMapFragment) this.getSupportFragmentManager().findFragmentById(R.id.main_map);
            fragment.getMapAsync(this);
            googleApiClient = new GoogleApiClient.Builder(MapDemoActivity.this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API).build();
        }
    
        private void initMap(GoogleMap googleMap) {
            map = googleMap;
            CameraUpdate center = CameraUpdateFactory.newLatLng(synergyIFS);
            CameraUpdate zoom = CameraUpdateFactory.zoomTo(10f);
            map.moveCamera(center);
            map.animateCamera(zoom);
            map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            getCurrentLocation();
            map.setMyLocationEnabled(true);
            map.setOnMapLongClickListener(this);
            map.setOnMarkerDragListener(this);
    
        }
    
        private void getCurrentLocation() {
            Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
            if (location != null) {
                //Getting longitude and latitude
                longitude = location.getLongitude();
                latitude = location.getLatitude();
    
                //moving the map to location
                moveMap();
            }
        }
    
        private void moveMap() {
            String msg = latitude + ", " + longitude;
    
            //Creating a LatLng Object to store Coordinates
            LatLng latLng = new LatLng(latitude, longitude);
    
            //Adding marker to map
            map.addMarker(new MarkerOptions()
                    .position(latLng) //setting position
                    .draggable(true) //Making the marker draggable
                    .title("Current Location")); //Adding a title
    
            //Moving the camera
            map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    
            //Animating the camera
            map.animateCamera(CameraUpdateFactory.zoomTo(15));
    
            //Displaying current coordinates in toast
            Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
        }
    
    
        protected void onStart() {
            googleApiClient.connect();
            super.onStart();
        }
    
        @Override
        protected void onStop() {
            googleApiClient.disconnect();
            super.onStop();
        }
    
        @Override
        public void onMapReady(GoogleMap googleMap) {
            initMap(googleMap);
        }
    
        @Override
        public void onConnected(Bundle bundle) {
            getCurrentLocation();
        }
    
        @Override
        public void onConnectionSuspended(int i) {
    
        }
    
        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
    
        }
    
        @Override
        public void onMarkerDragStart(Marker marker) {
    
        }
    
        @Override
        public void onMarkerDrag(Marker marker) {
    
        }
    
        @Override
        public void onMarkerDragEnd(Marker marker) {
            latitude = marker.getPosition().latitude;
            longitude = marker.getPosition().longitude;
    
            //Moving the map
            moveMap();
            new ReverserGeoCoding(MapDemoActivity.this).execute(marker.getPosition());
        }
    
        @Override
        public void onMapLongClick(LatLng latLng) {
            map.clear();
    
            //Adding a new marker to the current pressed position
            map.addMarker(new MarkerOptions()
                    .position(latLng)
                    .draggable(true));
        }
    
        class ReverserGeoCoding extends AsyncTask<LatLng,Void,String>
        {
            Context mContext;
    
            public ReverserGeoCoding(Context context){
                super();
                mContext = context;
            }
            @Override
            protected String doInBackground(LatLng... params) {
    
                Geocoder geocoder=new Geocoder(mContext);
                double Lat=params[0].latitude;
                double  Long=params[0].longitude;
    
                List<Address> addresses = null;
                String addressText="";
    
               try
               {
                   addresses = geocoder.getFromLocation(Lat, Long,2);
               }catch (Exception e)
               {
                   e.printStackTrace();
               }
                if(addresses != null && addresses.size() > 0 ){
                    Address address = addresses.get(0);
    
                    addressText = String.format("%s, %s, %s",
                            address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                            address.getLocality(),
                            address.getCountryName());
                }
                return addressText;
            }
    
            @Override
            protected void onPostExecute(String s) {
    
                   if(s!=null)
                   {
                       Toast.makeText(mContext,s,Toast.LENGTH_SHORT).show();
                   }
                else
                   {
                       Toast.makeText(mContext,"no address found",Toast.LENGTH_SHORT).show();
                   }
            }
        }
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <manifest
        package="com.synergyifs.libapp"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
        <uses-feature
            android:glEsVersion="0x00020000"
            android:required="true"/>
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:largeHeap="true"
            android:name=".app.MyApplication"
            android:theme="@style/AppTheme">
    
            <activity android:name=".SplashScreen">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
    
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
            <activity
                android:name=".HomeActivity"
                android:windowSoftInputMode="adjustPan"
                android:screenOrientation="portrait">
            </activity>
            <activity
                android:name=".Login"
                android:windowSoftInputMode="adjustPan"
                android:screenOrientation="portrait">
            </activity>
            <activity
                android:name=".RegisterUser"
                android:windowSoftInputMode="adjustPan"
                android:screenOrientation="portrait">
            </activity>
            <activity
                android:name=".MapDemoActivity"
                android:windowSoftInputMode="adjustPan"
                android:screenOrientation="portrait">
            </activity>
            <activity
                android:name=".PlacesActivity"
                android:windowSoftInputMode="adjustPan"
                android:screenOrientation="portrait">
            </activity>
            <!-- ATTENTION: This was auto-generated to add Google Play services to your project for
                 App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
            <meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version"/>
            <meta-data
                android:name="com.google.android.geo.API_KEY"
                android:value="@string/google_api_key"/>
        </application>
    
    </manifest>
    
    0 讨论(0)
  • 2021-01-28 01:07

    I think you need to download google play services r10 or even r12. Then you don't need

    <meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
    

    For better understanding follow this link

    0 讨论(0)
  • 2021-01-28 01:10

    After adding these two lines in my manifest file It worked .

    For eclipse add this :

        <meta-data android:name="com.google.android.gms.version" 
         android:value="@integer/google_play_services_version" />
    
        <meta-data android:name="com.google.android.maps.v2.API_KEY"
                  android:value="Your KEY" />
    

    If you are using Android Studio with gradle0.7+ then add this line:

    <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="Your Key" />
    
    0 讨论(0)
  • 2021-01-28 01:12

    As you commented on @gokhan answer, you should do

    <meta-data 
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    

    However, you still need to do

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/app_key" />
    

    where app_key is your Key.

    I suggest you to create an ids.xml file inside your values folder and have something like:

    <resources>
        <string name="app_id" translatable="false">1234567890</string>
        <string name="app_key" translatable="false">QWERTTYruyfgREYT</string>
        <string name="facebookAppId" translatable="false"></string>
    

    0 讨论(0)
  • 2021-01-28 01:21

    Probably You forgot to add your google_api_key you must add your key for use this service

    You should add your key too AndroidManifest.xml

    <application>
    
    <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="xxxxxxxxxx" />
    
    </application>
    

    like this.

    I hope this is help you

    0 讨论(0)
提交回复
热议问题