Ich entwickle eine App mit google map.In diesem möchte ich google map von bereitgestellten Werten von Breite und Länge zeigen. In app hole ich die Breiten- und Längenwerte aus der Datenbank und bei diesen Werten von Breite und Länge möchte ich zeigen Markierung auf Google-Karte.Wie kann man den Marker mit den Breiten- und Längenwerten von Google Maps in Android anzeigen?
Nachstehend ist der Code, den ich verwendet habe, In unter Code zeige ich abgerufene Werte von Längen- und Breitengrad und auch Marker zeigen auf diesem Abruf Werte von Längen- und Breitengrad auf Google Map, aber das Problem ist, wenn ich die App zum ersten Mal Karte ausführen zeige genau auf abgerufene Werte von Breite und Länge, aber wenn ich die App zum zweiten Mal öffne, zeigt es blau auf der Karte.Ich möchte Karte bei abgerufenen Werten von Breite und Länge zeigen. Wie mache ich das?
//java code
public class Location_Track6 extends FragmentActivity {
JSONArray result = null;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String CON = "CON";
JSONObject jsonobject;
JSONArray jsonarray;
SupportMapFragment fm;
private String url ="";
private static final String TAG_USER = "result";
// private static final String TAG_SNAME = "pseats";
private static final String TAG_LONG = "longitude";
private static final String TAG_LAT = "latitude";
private static final String TAG_ADDRESS = "paddress";
private List<LatLng> points = new ArrayList<>();
Polyline line; //added
TextView tv_mobno, tv_latitude, tv_longitude, tv_time;
String getLatitude;
String getLongitude;
Button slocation;
TextView etLng, etLat;
Button btnShow;
double lati=0;
double lngi=0;
GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_track6);
tv_mobno=(TextView)findViewById(R.id.textView_mob);
tv_latitude=(TextView)findViewById(R.id.textView_latitude);
tv_longitude=(TextView)findViewById(R.id.textView_longitude);
tv_time=(TextView)findViewById(R.id.textView_time);
etLat = (TextView) findViewById(R.id.et_lat);
etLng = (TextView) findViewById(R.id.et_lng);
fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
// slocation=(Button)findViewById(R.id.button_slocation);
url = "http://example.in/gmap_track.php";
// Getting reference to button btn_show
btnShow = (Button) findViewById(R.id.btn_show);
new JSONParse().execute();
// Setting click event listener for the button
btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getUserLocation();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Location_Track6.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// Getting JSON Array
result = json.getJSONArray(TAG_USER);
JSONObject c = result.getJSONObject(0);
// Storing JSON item in a Variable
String lat = c.getString(TAG_LAT);
String lng = c.getString(TAG_LONG);
//Set JSON Data in TextView
etLat.setText(lat);
etLng.setText(lng);
lati = Double.parseDouble(etLat.getText().toString());
lngi = Double.parseDouble(etLng.getText().toString());
// MarkerOptions marker = new MarkerOptions().position(new LatLng(lati, lng)).title("point");
// googleMap.addMarker(marker);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private void getUserLocation(){
LatLng position = new LatLng(lati, lngi);
// Instantiating MarkerOptions class
MarkerOptions options = new MarkerOptions();
// Setting position for the MarkerOptions
options.position(position);
// Setting title for the MarkerOptions
options.title("Position");
// Setting snippet for the MarkerOptions
options.snippet("Latitude:"+lati+",Longitude:"+lngi);
// Getting Reference to SupportMapFragment of activity_map.xml
// Getting reference to google map
googleMap = fm.getMap();
// Adding Marker on the Google Map
googleMap.addMarker(options);
// Creating CameraUpdate object for position
CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position);
// Creating CameraUpdate object for zoom
CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(13);
// Updating the camera position to the user input latitude and longitude
googleMap.moveCamera(updatePosition);
// Applying zoom to the marker position
googleMap.animateCamera(updateZoom);
}
private void addMarker() {
MarkerOptions options = new MarkerOptions();
// following four lines requires 'Google Maps Android API Utility Library'
// https://developers.google.com/maps/documentation/android/utility/
// I have used this to display the time as title for location markers
// you can safely comment the following four lines but for this info
/* IconGenerator iconFactory = new IconGenerator(this);
iconFactory.setStyle(IconGenerator.STYLE_PURPLE);
options.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(String.valueOf(R.drawable.one))));
options.anchor(iconFactory.getAnchorU(), iconFactory.getAnchorV());
*/
LatLng currentLatLng = new LatLng(lati,lngi);
options.position(currentLatLng);
Marker mapMarker = googleMap.addMarker(options);
// long atTime = mCurrentLocation.getTime();
// mLastUpdateTime = DateFormat.getTimeInstance().format(new Date(atTime));
// mapMarker.setTitle(mLastUpdateTime);
mapMarker.setTitle("point");
// Log.d(TAG, "Marker added.............................");
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng,
13));
// Log.d(TAG, "Zoom done.............................");
}
}
Ihre Log auf Fehler prüfen wenn die Karte leer wird, t Ime und post es hier, damit wir besser helfen können. – Nidhi
logcat zeigt nur Karten api Links ihre ist kein Fehler – hostogen