Google Maps Android API V2 problems with android 4.3

后端 未结 5 1966
栀梦
栀梦 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
        {
            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
    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(); } } } }

提交回复
热议问题