0

Google map에 마커를 만들고 추가했습니다. 사용자 위치를 기준으로 사용자 위치를 기준으로 다른 마커를 추가하고 그 사이에 선을 그립니다.구글 맵에 마커를 동적으로 추가하는 법

이것은 내가 한 일입니다.

 //OnCreate() 
    mClient= new GoogleApiClient.Builder(this). 
       addConnectionCallbacks(this). 
       addOnConnectionFailedListener(this). 
       addApi(Places.GEO_DATA_API).enableAutoManage(this, this).build(); 




     mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 

    /** 
     * 
     * @PLACES API 
     */ 


     PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) 
       getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment); 

     autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() { 
      @Override 
      public void onPlaceSelected(Place place) { 
       // TODO: Get info about the selected place. 
       Log.i("Place Name", "Place: " + place.getName()); 
       LatLng latLng = place.getLatLng(); 
       destinationLat= latLng.latitude; 
       destinationLng= latLng.longitude; 


       destinationLatLng= new LatLng(destinationLat, destinationLng); 
       /* mapFragment.addMarker(new MarkerOptions().position(destinationLatLng) 
         .title("PAF-KIET"));*/ 
      } 

      @Override 
      public void onError(Status status) { 
       // TODO: Handle the error. 
       Log.i("Error", "An error occurred: " + status); 
      } 
     }); 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     LatLng pafKietLocation = new LatLng(24.865498, 67.073302); 
     googleMap.addMarker(new MarkerOptions().position(pafKietLocation) 
       .title("University")); 
     googleMap.setMinZoomPreference(14.0f); 
     googleMap.setMaxZoomPreference(74.0f); 
     googleMap.moveCamera(CameraUpdateFactory.newLatLng(pafKietLocation)); 

    } 

문제

내가지도에 사용자 LatLng 전달의 방법을 찾을 수없는 생각이다.

+0

"사용자 LatLng", 당신은 무엇을 의미합니까? –

+0

@GurgenHakobyan :'onMapReady'에 미리 정의 된'Lat Lng' 값을 가진 마커를 추가하고 있습니다. 이제'onPlaceSelected()'에도 메이커를 추가하고 싶습니다. – Kirmani88

답변

1

귀하의 활동에 GoogleMap map을 신고하십시오. onMapReady(GoogleMap googleMap)this.map = googleMap을 추가하십시오. this.map을 사용할 때 항상 null을 확인하십시오. Gurgen 말한 것처럼 onPlaceSelected

if(this.map != null) 
{ 
    this.map.addMarker(new MarkerOptions().position(destinationLatLng) 
        .title("PAF-KIET")); 
} 
1

에서, 당신이 그것을 필요로 할 때 언제든지 그것을 사용 GoogleMap으로의 전역 인스턴스를 사용하지만 먼저 당신은 당신의에서 onCreate() 메소드에서 맵의 인스턴스를 얻어야한다.

관련 문제