Android 要在每个标记上显示设备id,从服务器接收的设备id
要在每个标记上显示Uniqueid,请从服务器接收Uniqueid。 我想为每个marker uniqueid秀设置marker的标签,但我尝试了几次,但都没有实现。。 请帮助我如何在每个标记上设置每个标记的唯一IDAndroid 要在每个标记上显示设备id,从服务器接收的设备id,android,json,performance,android-layout,android-fragments,Android,Json,Performance,Android Layout,Android Fragments,要在每个标记上显示Uniqueid,请从服务器接收Uniqueid。 我想为每个marker uniqueid秀设置marker的标签,但我尝试了几次,但都没有实现。。 请帮助我如何在每个标记上设置每个标记的唯一ID public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFail
public class MainActivity extends AppCompatActivity
implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static MainActivity instance;
private ArrayList<LatLng> latLngList;
private static final int ERROR_DIALOG_REQUEST = 9001;
GoogleMap mMap;
int FirstTimeMapIniciate = 0;
double latitude = 0;
double longitude = 0;
private GoogleApiClient mLocationClient;
private com.google.android.gms.location.LocationListener mListener;
private Marker marker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
latLngList = new ArrayList<>();
if (servicesOK()) {
setContentView(R.layout.activity_map);
if (initMap()) {
mLocationClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mLocationClient.connect();
mMap.setMyLocationEnabled(true);
} else {
Toast.makeText(this, "Map Connected!", Toast.LENGTH_SHORT).show();
}
} else {
setContentView(R.layout.activity_main);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.mapTypeNone:
mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
break;
case R.id.mapTypeNormal:
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
break;
case R.id.mapTypeSatellite:
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case R.id.mapTypeTerrain:
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
case R.id.mapTypeHybrid:
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
}
return super.onOptionsItemSelected(item);
}
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, ERROR_DIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(this, "Can't connect to mapping service", Toast.LENGTH_SHORT).show();
}
return false;
}
private boolean initMap() {
if (mMap == null && FirstTimeMapIniciate == 0) {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mapFragment.getMap();
mMap.setMyLocationEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
return (mMap != null);
}
private void gotoLocation(double lat, double lng, float zoom) {
LatLng latLng = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, zoom);
mMap.moveCamera(update);
}
public void showCurrentLocation(MenuItem item) {
Location currentLocation = LocationServices.FusedLocationApi
.getLastLocation(mLocationClient);
if (currentLocation == null) {
Toast.makeText(this, "Couldn't connect!", Toast.LENGTH_SHORT).show();
} else {
LatLng latLng = new LatLng(
currentLocation.getLatitude(),
currentLocation.getLongitude()
);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(
latLng, 10
);
mMap.moveCamera(update);
}
}
@Override
public void onConnected(Bundle bundle) {
Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show();
mListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
LatLng latLng1 = new LatLng(latitude, longitude);
MarkerOptions mp = new MarkerOptions();
mp = new MarkerOptions();
mp.position(new LatLng(location.getLatitude(),
location.getLongitude()));
Toast.makeText(MainActivity.this, "Location : " + location.getLatitude() + ", " + location.getLongitude(), Toast.LENGTH_LONG).show();
if (FirstTimeMapIniciate == 0) {
gotoLocation(location.getLatitude(), location.getLongitude(), 15);
FirstTimeMapIniciate = 1;
}
AppUtill.UniqueId();
new JSONAsyncTask().execute("http://13.7/hajjapi/api/GPSLocator/GetLocations");
if (AppStatus.getInstance(getContext()).isOnline()) {
} else {
Toast.makeText(MainActivity.this, "Turn On your WIFI ", Toast.LENGTH_LONG).show();
}
}
};
LocationRequest request = LocationRequest.create();
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
request.setInterval(1000);
request.setFastestInterval(1000);
LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, request, mListener);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(getApplicationContext(), "fetch data from server", Toast.LENGTH_LONG).show();
}
@Override
protected Boolean doInBackground(String... urls) {
try {
HttpGet httpGet = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httpGet);
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONArray jsonarray = new JSONArray(data);
latLngList.clear();
for (int i = 0; i < jsonarray.length(); i++) {
ModelClass modelClass = new Gson().fromJson(jsonarray.getJSONObject(i).toString(), ModelClass.class);
LatLng latLng = new LatLng(Double.parseDouble(modelClass.getLatitude()), Double.parseDouble(modelClass.getLongitude())); // Use your server's methods
latLngList.add(latLng);
}
return true;
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
Toast.makeText(getApplicationContext(), "Receicve data from server", Toast.LENGTH_LONG).show();
if (result == false) {
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
AddPointer();
}
}
private void AddPointer() {
try {
if (marker != null) {
mMap.clear();
Toast.makeText(getApplicationContext(), "Remove", Toast.LENGTH_LONG).show();
}
for (LatLng object : latLngList)
marker = mMap.addMarker(new MarkerOptions().title("User Name").position(object).icon(BitmapDescriptorFactory.fromResource(R.drawable.female4)));
System.out.println(marker.getPosition() + " Marker position.......");
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Error ", Toast.LENGTH_LONG).show();
}
}
public MainActivity() {
instance = this;
}
public static Context getContext() {
return instance;
}
}
这是我的Json数据
[{“longi”:“74.3230343”,“lati”:“31.5004135”,“uniqueid”:“25c0414a064bce9”},{“longi”:“74.3230899”,“lati”:“31.5003008”,“uniqueid”:“78cd7908e14a38d6”}]
代替LatLng的列表
保存模型类的列表
。然后,您将获得创建标记所需的坐标和id 从服务器获取LatLng
列表,并使用您的模型类解析JSON数据,然后现在您可以获取设置为您最喜欢的循环的所有点,以逐个获取点并创建标记。我希望这个解决方案对你有用 你说在每个标记上都显示它是什么意思?在信息窗口?是的,我理解唯一id部分,我问你“在标记上”是什么意思。据我所知,谷歌地图并没有将文字直接覆盖在标记上的机制。以下是地图标记的指南:。我认为你能做的最好的事情就是设置标记标题。我认为您也可以通过编程方式同时打开多个标记的信息窗口,而不必等待用户点击标记。添加标记的代码看起来很正确,它有什么不正确的地方吗?请注意,除非你做一些不同的事情,否则在你点击标记之前,标题不会显示。我不知道“未能实现”是什么意思。请准确解释什么是不正确的行为。如果你想展示一些JSON,最好把它放在一个更容易阅读的问题中。我已经问了两次关于错误行为的解释,你只是一直说你做不到。如果你能解释出哪里出了问题,我可以试着帮你,否则我无能为力。重复逻辑是错误的是没有帮助的。
public class ModelClass {
@SerializedName("longi")
public String longitudeServer;
@SerializedName("lati")
public String latitudeServer;
@SerializedName("uniqueid")
public String uniqueidSserver;
public ModelClass(){
// blank constructor is required
}
public String getLongitude(){
return longitudeServer;
}
public String getLatitude(){
return latitudeServer;
}
public String getUniqueId(){
return uniqueidSserver;
}
}