2017-01-10 28 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。您可以随时更改此值。