0

기본보기가 Google지도 인 활동이 있습니다. 지도를 처음로드 할 때 표식을 설정하지만 클릭하면 표식이나 아무것도 얻을 수 없습니다. 지도는 나타나지만 마커를 클릭하거나 화면을 탭하거나 탭하여 길게 눌러 새 마커를 만들 수 없습니다. 기본적으로 아무것도 할 수 없다 ... 그리고 나는 이유를 알 수 없다! 너희들은 내가 보지 못하는 것을 볼 수 있기를 바란다.안드로이드에서 google maps marker를 클릭하십시오?

여기가 내 주요 활동입니다.

public class MapsActivity extends FragmentActivity { 

    //Maps 
    private GoogleMap mMap; 
    //Marker 
    private Marker marker; 
    //Location 
    private LocationListener locationListener = null; 
    private LocationManager locationManager = null; 
    private static final float DEFAULTZOOM = 15; 
    private double longitude_mapsActivity; 
    private double latitude_from_mapsActivity; 
    private String cityName_mapsActivity; 
    private String countryName_mapsActivity; 
    //ProgressBar 
    private ProgressBar myPB_MAPS; 
    //Buttons 
    private ImageButton fab_doneButton; 

    //SearchEditText 
    private EditText editText_Search; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 
     mapFragment.getMapAsync(new OnMapReadyCallback() { 
      @Override 
      public void onMapReady(GoogleMap googleMap) { 
       mMap = googleMap; 
       LatLng sydney = new LatLng(-34, 151); 
       mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
       mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
       mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null); 
      } 
     }); 
     //Get user current location. 
     //myPB_MAPS = (ProgressBar) findViewById(R.id.myPB_MAPS); 

     //initialize your map 
     initMap(); 

     //FAB button 
     fab_doneButton = (ImageButton) findViewById(R.id.activity_maps_FAB_done); 
     fab_doneButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (countryName_mapsActivity == null) { 
        Toast.makeText(MapsActivity.this, "Location is null", Toast.LENGTH_SHORT).show(); 
       } else { 
        Global_Class.getInstance().getValue().countryName_GLOBAL = countryName_mapsActivity; 
        Global_Class.getInstance().getValue().cityName_GLOBAL = cityName_mapsActivity; 
        Global_Class.getInstance().getValue().longitude_user_GLOBAL = longitude_mapsActivity; 
        Global_Class.getInstance().getValue().latitude_user_GLOBAL = latitude_from_mapsActivity; 
        //Go to make sure we're sending all the GPS info, so we set geoLocationFromMapsIsPresent to true. 
        FinishCard.geoLocationFromMapsIsPresent(); 
        FinishCard.setComingBackFromMaps(); 
        Intent FinishCardIntent = new Intent(MapsActivity.this, FinishCard.class); 
        startActivity(FinishCardIntent); 
       } 

      } 
     }); 

     //EditText 
     editText_Search = (EditText) findViewById(R.id.maps_EditText); 
     editText_Search.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
      @Override 
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
       if (actionId == EditorInfo.IME_ACTION_SEARCH) { 
        performSearch(); 
        return true; 
       } 
       return false; 
      } 

     }); 




     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    } 

    private void performSearch() 
    { 
     String location = editText_Search.getText().toString(); 
     if(location.length() == 0) 
     { 
      Toast.makeText(this,"Please enter a location",Toast.LENGTH_SHORT).show(); 
      return; 
     } 
     //1-first step 
     Geocoder gc = new Geocoder(this); 
     List<Address> list = null;//For this function I only want a single address. 
     try 
     { 
      //3-Third step 
      list = gc.getFromLocationName(location,10); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     //4-Fourth step 
     Address add = list.get(0);//Give me the first and only item of the list. 

     //5-fifth step 
     String locality = add.getLocality();//So if you enter Taj mahal you get Agra, the place where its at, thats what Address locality does. 
     double lat = add.getLatitude(); 
     double lng = add.getLongitude(); 

     //GoToLocation() method 
     gotoLocation(lat, lng, DEFAULTZOOM); 

     //For Removing existing markers. 
     if(marker != null) 
     { 
      marker.remove(); 
     } 

     MarkerOptions options = new MarkerOptions() 
       .title(locality) 
       .position(new LatLng(lat, lng)) 
       .draggable(true); 
     marker = mMap.addMarker(options); 
    } 

    private void gotoLocation(double lat, double lng, float zoom) 
    { 
     LatLng ll = new LatLng(lat,lng); 
     CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom); 
     mMap.moveCamera(update); 

    } 

    private void setMarker(String locality, String country, double lat, double lng) 
    { 
     if(marker != null) 
     { 
      marker.remove(); 
     } 
     MarkerOptions options = new MarkerOptions() 
       .title(locality) 
       .position(new LatLng(lat, lng)) 
       .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)) 
       .draggable(true); 
     if(country.length() > 0) 
     { 
      options.snippet(country);//Background highlight TEXT SUPER IMPORTANT 
     } 
     //.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)); 
     marker = mMap.addMarker(options);//So here we connect our marker to our map, which is used in initMap. 

    } 

    private void initMap() 
    { 
     if(mMap == null) 
     { 

      if(mMap != null) 
      { 
       mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() 
       { 
        @Override 
        public void onMapLongClick(LatLng ll) { 
         Geocoder gc = new Geocoder(MapsActivity.this); 
         List<Address> list = null; 
         try { 
          list = gc.getFromLocation(ll.latitude, ll.longitude, 1); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 

         Address add = list.get(0); 
         MapsActivity.this.setMarker(add.getLocality(), add.getCountryName(), ll.latitude, ll.longitude);//this is where we set the orange marker. 
         latitude_from_mapsActivity= ll.latitude; 
         longitude_mapsActivity= ll.longitude; 
         countryName_mapsActivity = add.getCountryName(); 
         cityName_mapsActivity = add.getLocality(); 
        } 
       }); 

       mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { 
        @Override 
        public boolean onMarkerClick(Marker marker) 
        { 
         LatLng ll = marker.getPosition(); 
         latitude_from_mapsActivity= ll.latitude; 
         longitude_mapsActivity = ll.longitude; 
         Geocoder gc = new Geocoder(MapsActivity.this); 
         //Global_Class.getInstance().getValue().cardLocality = "Paris"; 
         List<Address> list = null; 
         try 
         { 
          list = gc.getFromLocation(ll.latitude, ll.longitude,1); 

         } 
         catch (IOException e) 
         { 
          e.printStackTrace(); 
         } 
         try 
         { 
          Address add = list.get(0); 
          countryName_mapsActivity = add.getCountryName(); 
          cityName_mapsActivity = add.getLocality(); 
          return false; 

         } 
         catch (IndexOutOfBoundsException e) 
         { 
          return false; 
         } 


        } 
       }); 

       mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() //If you want to drag the original google maps marker you use this method, if you comment this out it will use the orange one. 
       { 
        @Override 
        public void onMarkerDragStart(Marker marker) { 

        } 

        @Override 
        public void onMarkerDrag(Marker marker) { 

        } 

        @Override 
        public void onMarkerDragEnd(Marker marker) { 
         Geocoder gc = new Geocoder(MapsActivity.this); 
         List<Address> list = null; 
         LatLng ll = marker.getPosition(); 
         try { 
          list = gc.getFromLocation(ll.latitude, ll.longitude, 1); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 

         Address add = list.get(0); 
         marker.setTitle(add.getLocality()); 
         marker.setSnippet(add.getCountryName()); 
         //marker.showInfoWindow(); 
        } 
       }); 
      } 
     } 
    } 
} 

여기에 당신은 당신의 initMap()에 자신을 모순되는 내 XML

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:map="http://schemas.android.com/apk/res-auto" 
    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="com.daprlabs.swipedeck.GeoLocation.MapsActivity"> 


    <RelativeLayout 
     android:layout_width="340dp" 
     android:layout_height="50dp" 
     android:background="#FFFFFF" 
     android:elevation="10sp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="10dp"> 

     <EditText 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/maps_EditText" 
      android:imeOptions="actionSearch" 
      android:inputType="text"/> 
    </RelativeLayout> 

    <ProgressBar 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:id="@+id/myPB_MAPS" 
     android:layout_marginLeft="150dp" 
     android:layout_marginTop="55dp"/> 

    <ImageButton 
     android:layout_width="70dp" 
     android:layout_height="70dp" 
     android:background="@drawable/circle_fab" 
     android:id="@+id/activity_maps_FAB_done" 
     android:layout_gravity="right|bottom" 
     android:src="@drawable/white_plus" /> 


</fragment> 

답변

0

입니다.

는 if 문 다음 제거
if (mMap == null) 

은 또한 단지 initMap() mapFragment.getMapAsync 후 수익률을 호출합니다. 이 시점에서지도를 사용할 준비가되었음을 알 수 있습니다.

mapFragment.getMapAsync(new OnMapReadyCallback() { 
     @Override 
     public void onMapReady(GoogleMap googleMap) { 
      mMap = googleMap; 
      LatLng sydney = new LatLng(-34, 151); 
      mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
      mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
      mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null); 
      initMap(); 
     } 
    }); 
+0

그래서 onMapReadyCall을 구현하지 않아도됩니까? –

+0

비동기 버전을 사용하면 콜백이 필요 없다고 생각합니다. 이 말을 인용하지 말고 문서를 읽으십시오. – StuStirling

+1

감사 봉오리가 효과가있었습니다. 네, 안드로이드 스튜디오와 구글 플레이 서비스를 업그레이드해야만했는데,지도 기능 중 일부가 제대로 작동하지 않았고 처리하기가 짜증나고 오래된 코드가 스스로 똥을 치기 시작했습니다. –

0

님의

OnMapReadyCallback

차례로 재정에 구현 해야하는

onMapReady

이제 onMapReady 내에서 Map을 조작 할 수 있습니다. 그 전에지도가 실제로 올바르게 설정되었는지는 확실하지 않습니다.

마커를로드하고 마커 클릭 수신기를 설정하는 것과 같이 맵을 조작하는 모든 것이 onMapReady에서 발생해야합니다.

적절한 시간에지도를 조작 한 예로지도의 카메라가 올바르게 설정된 경우에만 다음 코드에서 힌트를 얻을 수 있습니다.

public class YourMapFragment extends Fragment implements OnMapReadyCallback { 
... 
@Override 
public void onMapReady(GoogleMap googleMap) { 
mMap = googleMap; 
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentPosition,16)); 

    mMap.addMarker(new MarkerOptions() 
      .position(currentPosition) 
      .snippet("Lat:" + lat + "Lng:" + log)); 
} 

    ... 
    }