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

「用戶經緯度」,你是什麼意思? –

+0

@GurgenHakobyan:上'onMapReady',我加入了預定義的'緯度Lng'值的標記,現在我想在'onPlaceSelected()'以及添加製造商。 – Kirmani88

回答

1

在您的活動中聲明GoogleMap map並在onMapReady(GoogleMap googleMap)內添加this.map = googleMap。當使用this.map時,請務必檢查爲空。在onPlaceSelected

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

像Gurgen說,使用的GoogleMap的全局實例,然後只要你需要它使用它,但首先你應該得到地圖實例在onCreate()方法。