2017-01-10 89 views
-3

我嘗試這樣的代碼,但它讓我看到用戶的當前位置如何顯示標記列表中的谷歌地圖


public class DeveloperAdress extends Fragment implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener { 

private static final String var= Config.PORT_CONX; 
private static final String TAG = DeveloperAdress.class.getSimpleName(); 
private static final String url =var+ "listeAddress"; 
private ProgressDialog pDialog; 
private GoogleMap mMap; 
GoogleApiClient mGoogleApiClient; 
Location mLastLocation; 
LocationRequest mLocationRequest; 
Marker mCurrLocationMarker; 
List<Address> adresss; 
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99; 

public DeveloperAdress() { 

} 


public static DeveloperAdress newInstance() { 
    DeveloperAdress fragment = new DeveloperAdress(); 
    Bundle args = new Bundle(); 

    fragment.setArguments(args); 
    return fragment; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    adresss=getAllAdresss(); 




} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view= inflater.inflate(R.layout.developer_adress, container, false); 
    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 


    for(Address address:addresss) { 
     // mMap.addMarker(new MarkerOptions().position(new LatLng(address.getLatitude(), address.getLongitude()))); 

     markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)); 
     Marker markert = mMap.addMarker(markerOptions); 
    } 
    return view; 
} 

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 
    mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); 

    mMap.getUiSettings().setZoomControlsEnabled(true); 




    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
      buildGoogleApiClient(); 
      mMap.setMyLocationEnabled(true); 
     } 
    } 
    else { 
     buildGoogleApiClient(); 
     mMap.setMyLocationEnabled(true); 
    } 
} 

protected synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(getContext()).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build(); 
    mGoogleApiClient.connect(); 
} 
@Override 
public void onConnected(Bundle bundle) { 
    mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(1000); 
    mLocationRequest.setFastestInterval(1000); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
    if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } 
} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onLocationChanged(Location location) { 
    mLastLocation = location; 
    if (mCurrLocationMarker != null) { 
     mCurrLocationMarker.remove(); 
    } 

    //Place current location marker 
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
    MarkerOptions markerOptions = new MarkerOptions(); 
    markerOptions.position(latLng); 
    markerOptions.title("Current Position"); 
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)); 
    mCurrLocationMarker = mMap.addMarker(markerOptions); 

    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    mMap.animateCamera(CameraUpdateFactory.zoomTo(11)); 

    //stop location updates 
    if (mGoogleApiClient != null) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
    } 


} 


@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 

} 



@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_LOCATION: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // Permission was granted. 
       if (ContextCompat.checkSelfPermission(getContext(), 
         Manifest.permission.ACCESS_FINE_LOCATION) 
         == PackageManager.PERMISSION_GRANTED) { 

        if (mGoogleApiClient == null) { 
         buildGoogleApiClient(); 
        } 
        mMap.setMyLocationEnabled(true); 
       } 

      } else { 

       // Permission denied, Disable the functionality that depends on this permission. 
       Toast.makeText(getContext(), "permission denied", Toast.LENGTH_LONG).show(); 
      } 
      return; 
     } 


    } 
} 
public boolean checkLocationPermission(){ 
    if (ContextCompat.checkSelfPermission(getContext(), 
      Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 

     // Asking user if explanation is needed 
     if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), 
       Manifest.permission.ACCESS_FINE_LOCATION)) { 


      ActivityCompat.requestPermissions(getActivity(), 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_LOCATION); 


     } else { 
      ActivityCompat.requestPermissions(getActivity(), 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_LOCATION); 
     } 
     return false; 
    } else { 
     return true; 
    } 
} 



public List<Address> getAllAddresss(){ 

    final List<Address> addresss = new ArrayList<Address>(); 

    JsonArrayRequest prodReq = new JsonArrayRequest(url, 
      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray response) { 
        Log.d(TAG, response.toString()); 
        hidePDialog(); 

        // Parsing json 
        for (int i = 0; i < response.length(); i++) { 
         try { 

          JSONObject obj = response.getJSONObject(i); 
          Address address = new Address(); 
          address.setId(obj.getInt("id")); 
             address.setLongitude(obj.getDouble("longitude")); 
          address.setLatitude(obj.getDouble("latitude")); 

          addresss.add(address); 

         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 

        } 



       } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      VolleyLog.d(TAG, "connexion impossible !!"); 
      hidePDialog(); 

     } 
    }); 

    AppController.getInstance().addToRequestQueue(prodReq); 

    return addresss; 

} 
private void hidePDialog() { 
    if (pDialog != null) { 
     pDialog.dismiss(); 
     pDialog = null; 
    } 

} 




@Override 
public void onResume() { 
    super.onResume(); 

} 
} 
+1

你想要什麼?請詳細說明問題。 –

+0

謝謝你們,我找到了解決辦法:只要檢索前一個片段中的地址列表,並在該片段的構造函數中傳遞列表 –

回答

0

試試這個:

googleMap.addMarker(new MarkerOptions() 
      .position(new LatLng(28.7750, 77.4183)) 
      .title("Marker in India") 
      .icon(BitmapDescriptorFactory 
      .defaultMarker(BitmapDescriptorFactory.HUE_GREEN)) 
      .snippet("kochi").draggable(true)); 

您的onMapReady(GoogleMap googleMap)未被調用。 你需要調用getMapAsync在地圖上的片段設置OnMapReadyCallback

the documentation

公共無效getMapAsync(OnMapReadyCallback回調)

設置將被觸發時的GoogleMap的回調對象 實例已準備就緒可以使用。

onCreate函數的末尾添加以下代碼:

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); 
mapFragment.getMapAsync(this); 

使用標記

放置在地圖的表面上的特定點的圖標。標記圖標是針對設備的屏幕而非地圖表面繪製的;即由於地圖旋轉,傾斜或縮放而不一定會改變方位。

的標記具有以下屬性:

將放置在標記的經緯度位置的圖像上的點。默認爲圖像左側和圖像底部的50%。

位置

的標記在地圖上的位置的經緯度值。如果您想移動標記,您可以隨時更改此值。

標題

當用戶點擊該標記中顯示的有關信息窗口的文本字符串。您可以隨時更改此值。

片段時顯示的標題下方

附加文本。您可以隨時更改此值。

圖標

在隨即出現的標誌的位圖。如果圖標未設置,則顯示默認圖標。您可以使用defaultMarker(float)指定默認圖標的替代顏色。創建標記後,您無法更改圖標。

將狀態

如果你想允許用戶拖動標記,此屬性設置爲true。您可以隨時更改此值。默認值是true。

能見度

默認情況下,標記是可見的。要使標記不可見,請將此屬性設置爲false。您可以隨時更改此值。