我尝试了此代码,但它显示了用户的当前位置
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();
}
}
答案 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
public void getMapAsync(OnMapReadyCallback callback)
设置一个回调对象,该对象将在GoogleMap时触发 实例已准备好使用。
在onCreate
功能的末尾添加此代码:
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
使用标记类
放置在地图表面特定点的图标。标记图标是针对设备的屏幕而不是地图的表面绘制的;即,由于地图旋转,倾斜或缩放,它不一定会改变方向。
标记具有以下属性:
<强>锚强>
图像上将放置在标记的LatLng位置的点。默认情况下,图像左侧和图像底部为50%。
<强>位置强>
地图上标记位置的LatLng值。如果要移动标记,可以随时更改此值。
<强>标题强>
当用户点按标记时,在信息窗口中显示的文本字符串。您可以随时更改此值。
<强>段强>
标题下方显示的其他文字。您可以随时更改此值。
图标强>
为标记显示的位图。如果未设置图标,则会显示默认图标。您可以使用defaultMarker(float)指定默认图标的替代着色。一旦您创建了标记,就无法更改图标。
拖动状态
如果要允许用户拖动标记,请将此属性设置为true。您可以随时更改此值。默认值为true。
<强>可见性强>
默认情况下,标记可见。要使标记不可见,请将此属性设置为false。您可以随时更改此值。
答案 1 :(得分:0)
https://github.com/anandvardhan1991/GoogleMaps-Web-API-Demo
查看此Google展示位置API演示。