sara.nikzadeh
دوشنبه 09 شهریور 1394, 16:23 عصر
با سلام دوباره خدمت دوستان خوبم
من دوتا polygon ساختم. موقعیت کاربر هر در هر بار که اکتیویتی اجرا میشه میگیرم.
حالا با این توصیفات، میخوام اگه کاربرم، تو موقعیت polygon که تعریف کردم قرار گرفت،داخل مارکر مربوط به اون polygon توضیحاتی درج بشه
امیدوارم منظورم رو خوب رسونده باشم.
اینم کدهای اکتیویتی :
public class MainActivity extends ActionBarActivity implements
GooglePlayServicesClient.OnConnectionFailedListene r,
GooglePlayServicesClient.ConnectionCallbacks
{
private static final int GPS_ERRORDIALOG_REQUEST = 9001;
GoogleMap googleMap;
Marker marker;
private static final float Default_Zoom = 15;
LocationClient locationClient;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (serviceOk())
{
ArrayList<LatLng> points = new ArrayList<LatLng>();
points.add(new LatLng(32.610253, 51.626202));
points.add(new LatLng(32.610271, 51.617437));
points.add(new LatLng(32.606972, 51.617469));
points.add(new LatLng(32.606692, 51.617791));
points.add(new LatLng(32.606629, 51.626181));
// draw polygon:
PolygonOptions polygonOptions = new PolygonOptions();
polygonOptions.addAll(points);
polygonOptions.fillColor(Color.YELLOW);
polygonOptions.strokeWidth(10);
polygonOptions.strokeColor(Color.DKGRAY);
ArrayList<LatLng> point = new ArrayList<LatLng>();
point.add(new LatLng(32.612202, 51.607433));
point.add(new LatLng(32.609919, 51.607377));
point.add(new LatLng(32.610128, 51.601886));
point.add(new LatLng(32.612369, 51.602079));
// draw polygon:
PolygonOptions polygonOption = new PolygonOptions();
polygonOption.addAll(point);
polygonOption.fillColor(Color.GREEN);
polygonOption.strokeColor(Color.DKGRAY);
setContentView(R.layout.activity_map);
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id. map)).getMap();
googleMap.addPolygon(polygonOptions);
googleMap.addPolygon(polygonOption);
if (initMap())
{
Toast.makeText(this, "Ready to use", Toast.LENGTH_LONG).show();
locationClient = new LocationClient(this, this, this);
locationClient.connect();
}
}
else
{
Toast.makeText(this, "Ready not available", Toast.LENGTH_LONG).show();
}
}
public boolean serviceOk()
{
try
{
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailab le(this);
if (isAvailable == ConnectionResult.SUCCESS)
{
return true;
}
else if (GooglePlayServicesUtil.isUserRecoverableError(isA vailable))
{
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST);
dialog.show();
}
else
{
Toast.makeText(this, "Cannot connect to Map", Toast.LENGTH_LONG).show();
}
}
catch (Exception ex)
{
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
}
return false;
}
private boolean initMap()
{
if (googleMap == null )
{
SupportMapFragment supportMapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id. map);
googleMap = supportMapFragment.getMap();
if (googleMap != null)
{
googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter()
{
@Override
public View getInfoWindow(Marker arg0)
{
return null;
}
@Override
public View getInfoContents(Marker marker)
{
View v = getLayoutInflater().inflate(R.layout.info_window, null);
TextView tvLocality = (TextView) v.findViewById(R.id.tv_locality);
TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);
TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);
TextView tvSnippet = (TextView) v.findViewById(R.id.tv_snippet);
LatLng ll = marker.getPosition();
tvLocality.setText(marker.getTitle());
tvLat.setText("Latitude: " + ll.latitude);
tvLng.setText("Longitude: " + ll.longitude);
tvSnippet.setText(marker.getSnippet());
return v;
}
});
}
}
return (googleMap != null);
}
private void setMarker(String locality, String country, double lat, double lng)
{
if (marker != null)
{
marker.remove();
}
MarkerOptions markerOption = new MarkerOptions()
.title(locality)
.position(new LatLng(lat, lng))
.icon(BitmapDescriptorFactory.fromResource(R.drawa ble.ic_mapmarker));
if (country.length() > 0)
{
markerOption.snippet(country);
}
marker = googleMap.addMarker(markerOption);
}
private void gotoCurrentLocation()
{
Location currentLocation = locationClient.getLastLocation();
if (currentLocation == null)
{
Toast.makeText(this, "Current location is not available", Toast.LENGTH_LONG).show();
}
else
{
LatLng ll = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(ll, Default_Zoom);
googleMap.animateCamera(cameraUpdate);
setMarker("Current Location", "",
currentLocation.getLatitude(),
currentLocation.getLongitude());
}
}
@Override
public void onConnected(Bundle bundle)
{
gotoCurrentLocation();
}
@Override
public void onDisconnected()
{
}
@Override
public void onConnectionFailed(ConnectionResult arg0)
{
}
@Override
protected void onStop() {
super.onStop();
MapStateManager mgr = new MapStateManager(this);
mgr.saveMapState(googleMap);
}
@Override
protected void onResume() {
super.onResume();
MapStateManager mgr = new MapStateManager(this);
CameraPosition position = mgr.getSavedCameraPosition();
if (position != null) {
CameraUpdate update = CameraUpdateFactory.newCameraPosition(position);
googleMap.moveCamera(update);
// This is part of the answer to the code challenge
googleMap.setMapType(mgr.getSavedMapType());
}
}
}
من دوتا polygon ساختم. موقعیت کاربر هر در هر بار که اکتیویتی اجرا میشه میگیرم.
حالا با این توصیفات، میخوام اگه کاربرم، تو موقعیت polygon که تعریف کردم قرار گرفت،داخل مارکر مربوط به اون polygon توضیحاتی درج بشه
امیدوارم منظورم رو خوب رسونده باشم.
اینم کدهای اکتیویتی :
public class MainActivity extends ActionBarActivity implements
GooglePlayServicesClient.OnConnectionFailedListene r,
GooglePlayServicesClient.ConnectionCallbacks
{
private static final int GPS_ERRORDIALOG_REQUEST = 9001;
GoogleMap googleMap;
Marker marker;
private static final float Default_Zoom = 15;
LocationClient locationClient;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (serviceOk())
{
ArrayList<LatLng> points = new ArrayList<LatLng>();
points.add(new LatLng(32.610253, 51.626202));
points.add(new LatLng(32.610271, 51.617437));
points.add(new LatLng(32.606972, 51.617469));
points.add(new LatLng(32.606692, 51.617791));
points.add(new LatLng(32.606629, 51.626181));
// draw polygon:
PolygonOptions polygonOptions = new PolygonOptions();
polygonOptions.addAll(points);
polygonOptions.fillColor(Color.YELLOW);
polygonOptions.strokeWidth(10);
polygonOptions.strokeColor(Color.DKGRAY);
ArrayList<LatLng> point = new ArrayList<LatLng>();
point.add(new LatLng(32.612202, 51.607433));
point.add(new LatLng(32.609919, 51.607377));
point.add(new LatLng(32.610128, 51.601886));
point.add(new LatLng(32.612369, 51.602079));
// draw polygon:
PolygonOptions polygonOption = new PolygonOptions();
polygonOption.addAll(point);
polygonOption.fillColor(Color.GREEN);
polygonOption.strokeColor(Color.DKGRAY);
setContentView(R.layout.activity_map);
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id. map)).getMap();
googleMap.addPolygon(polygonOptions);
googleMap.addPolygon(polygonOption);
if (initMap())
{
Toast.makeText(this, "Ready to use", Toast.LENGTH_LONG).show();
locationClient = new LocationClient(this, this, this);
locationClient.connect();
}
}
else
{
Toast.makeText(this, "Ready not available", Toast.LENGTH_LONG).show();
}
}
public boolean serviceOk()
{
try
{
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailab le(this);
if (isAvailable == ConnectionResult.SUCCESS)
{
return true;
}
else if (GooglePlayServicesUtil.isUserRecoverableError(isA vailable))
{
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST);
dialog.show();
}
else
{
Toast.makeText(this, "Cannot connect to Map", Toast.LENGTH_LONG).show();
}
}
catch (Exception ex)
{
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
}
return false;
}
private boolean initMap()
{
if (googleMap == null )
{
SupportMapFragment supportMapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id. map);
googleMap = supportMapFragment.getMap();
if (googleMap != null)
{
googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter()
{
@Override
public View getInfoWindow(Marker arg0)
{
return null;
}
@Override
public View getInfoContents(Marker marker)
{
View v = getLayoutInflater().inflate(R.layout.info_window, null);
TextView tvLocality = (TextView) v.findViewById(R.id.tv_locality);
TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);
TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);
TextView tvSnippet = (TextView) v.findViewById(R.id.tv_snippet);
LatLng ll = marker.getPosition();
tvLocality.setText(marker.getTitle());
tvLat.setText("Latitude: " + ll.latitude);
tvLng.setText("Longitude: " + ll.longitude);
tvSnippet.setText(marker.getSnippet());
return v;
}
});
}
}
return (googleMap != null);
}
private void setMarker(String locality, String country, double lat, double lng)
{
if (marker != null)
{
marker.remove();
}
MarkerOptions markerOption = new MarkerOptions()
.title(locality)
.position(new LatLng(lat, lng))
.icon(BitmapDescriptorFactory.fromResource(R.drawa ble.ic_mapmarker));
if (country.length() > 0)
{
markerOption.snippet(country);
}
marker = googleMap.addMarker(markerOption);
}
private void gotoCurrentLocation()
{
Location currentLocation = locationClient.getLastLocation();
if (currentLocation == null)
{
Toast.makeText(this, "Current location is not available", Toast.LENGTH_LONG).show();
}
else
{
LatLng ll = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(ll, Default_Zoom);
googleMap.animateCamera(cameraUpdate);
setMarker("Current Location", "",
currentLocation.getLatitude(),
currentLocation.getLongitude());
}
}
@Override
public void onConnected(Bundle bundle)
{
gotoCurrentLocation();
}
@Override
public void onDisconnected()
{
}
@Override
public void onConnectionFailed(ConnectionResult arg0)
{
}
@Override
protected void onStop() {
super.onStop();
MapStateManager mgr = new MapStateManager(this);
mgr.saveMapState(googleMap);
}
@Override
protected void onResume() {
super.onResume();
MapStateManager mgr = new MapStateManager(this);
CameraPosition position = mgr.getSavedCameraPosition();
if (position != null) {
CameraUpdate update = CameraUpdateFactory.newCameraPosition(position);
googleMap.moveCamera(update);
// This is part of the answer to the code challenge
googleMap.setMapType(mgr.getSavedMapType());
}
}
}