Android 如何在谷歌地图中显示标记列表
我尝试了这段代码,但它显示了用户的当前位置Android 如何在谷歌地图中显示标记列表,android,google-maps,google-maps-markers,Android,Google Maps,Google Maps Markers,我尝试了这段代码,但它显示了用户的当前位置 公共类developerAddress在MapreadyCallback、GoogleAppClient.ConnectionCallbacks、GoogleAppClient.OnConnectionFailedListener、LocationListener上扩展了片段实现{ 私有静态最终字符串var=Config.PORT\u CONX; 私有静态最终字符串标记=developerAddress.class.getSimpleName();
公共类developerAddress在MapreadyCallback、GoogleAppClient.ConnectionCallbacks、GoogleAppClient.OnConnectionFailedListener、LocationListener上扩展了片段实现{
私有静态最终字符串var=Config.PORT\u CONX;
私有静态最终字符串标记=developerAddress.class.getSimpleName();
私有静态最终字符串url=var+“ListAddress”;
私人对话;
私有谷歌地图;
GoogleapClient MGoogleapClient;
位置mLastLocation;
位置请求mLocationRequest;
标记器mCurrLocationMarker;
列出地址;
公共静态final int MY_PERMISSIONS_REQUEST_LOCATION=99;
公共developerAddress(){
}
公共静态developerAddress newInstance(){
developerAddress片段=新developerAddress();
Bundle args=新Bundle();
fragment.setArguments(args);
返回片段;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
地址=GetAllAddresss();
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图=充气机。充气(R.layout.developer\u address,container,false);
SupportMapFragment mapFragment=(SupportMapFragment)getChildFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
收件人(地址:addresss){
//mMap.addMarker(新的MarkerOptions().position(新的LatLng(address.getLatitude(),address.getLongitude()));
图标(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
Marker markert=mMap.addMarker(markerOptions);
}
返回视图;
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
mMap.setMapType(GoogleMap.MAP\u TYPE\u地形);
mMap.getUiSettings().setZoomControlsEnabled(true);
if(android.os.Build.VERSION.SDK\u INT>=Build.VERSION\u CODES.M){
if(ContextCompat.checkSelfPermission(getContext(),Manifest.permission.ACCESS\u FINE\u LOCATION)==PackageManager.permission\u已授予){
buildGoogleAppClient();
mMap.setMyLocationEnabled(真);
}
}
否则{
buildGoogleAppClient();
mMap.setMyLocationEnabled(真);
}
}
受保护的同步无效BuildGoogleAppClient(){
mGoogleApiClient=new GoogleApiClient.Builder(getContext()).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
mGoogleApiClient.connect();
}
@凌驾
未连接的公共空间(捆绑包){
mlLocationRequest=新位置请求();
mlLocationRequest.setInterval(1000);
mlLocationRequest.setFastTestInterval(1000);
mLocationRequest.setPriority(位置请求、优先级、平衡、功率、精度);
if(ContextCompat.checkSelfPermission(getContext(),Manifest.permission.ACCESS\u FINE\u LOCATION)==PackageManager.permission\u已授予){
LocationServices.FusedLocationApi.RequestLocationUpdate(mgoogleapClient、mlLocationRequest、this);
}
}
@凌驾
公共空间连接暂停(int i){
}
@凌驾
已更改位置上的公共无效(位置){
mLastLocation=位置;
if(mCurrLocationMarker!=null){
mCurrLocationMarker.remove();
}
//放置当前位置标记
LatLng LatLng=新LatLng(location.getLatitude(),location.getLongitude());
MarkerOptions MarkerOptions=新MarkerOptions();
标记选项位置(板条);
标记选项。标题(“当前位置”);
图标(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
mCurrLocationMarker=mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
//停止位置更新
if(mGoogleApiClient!=null){
LocationServices.FusedLocationApi.RemovelocationUpdate(mgoogleapClient,this);
}
}
@凌驾
公共无效onConnectionFailed(ConnectionResult ConnectionResult){
}
@凌驾
公共无效onRequestPermissionsResult(int requestCode,
字符串权限[],int[]grantResults){
开关(请求代码){
案例我的权限请求位置:{
//如果取消请求,则结果数组为空。
如果(grantResults.length>0
&&grantResults[0]==PackageManager.PERMISSION\u已授予){
//获得了许可。
if(ContextCompat.checkSelfPermission(getContext()),
清单.权限.访问(位置)
==PackageManager.权限(已授予){
if(mGoogleApiClient==null){
buildGoogleAppClient();
}
mMap.setMyLocationEnabled(真);
}
}否则{
//权限被拒绝,请禁用依赖于此权限的功能。
Toast.makeText(getContext(),“权限被拒绝”,Toast.LENGTH_LONG.show();
}
回来
}
}
}
公共布尔值checkLocationPermission(){
if(ContextCompat.checkSelfPermission(getContext()),
清单.权限.访问(位置)
!=PackageManager.权限(已授予){
//询问用户是否需要解释
如果(ActivityCompat.shouldShowRequestPermissionRegulation)(getActivity(),
清单.权限.访问(位置){
ActivityCompat.requestPermissions(getActivity(),
新字符串[]{Manifest.permission.ACCESS\u FINE\u LOCATION},
我的\权限\请求\位置);
}否则{
ActivityCompat.requestPermissions(getActivity(),
新字符串
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();
}
}
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));
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);