在android中,如何在谷歌地图的给定位置显示标记?
我正在使用谷歌地图开发一个应用程序。在这个应用程序中,我想在从服务器获取经度和纬度值的位置显示标记。如何在获取的经度和纬度值处显示标记? 下面的代码成功地从我的服务器获取了经度和纬度值,我想在谷歌地图上用这个获取经度和纬度来显示标记。我该怎么做在android中,如何在谷歌地图的给定位置显示标记?,android,google-maps,Android,Google Maps,我正在使用谷歌地图开发一个应用程序。在这个应用程序中,我想在从服务器获取经度和纬度值的位置显示标记。如何在获取的经度和纬度值处显示标记? 下面的代码成功地从我的服务器获取了经度和纬度值,我想在谷歌地图上用这个获取经度和纬度来显示标记。我该怎么做 //java code public class Location_Track1 extends Activity { private static final String TAG = "Location_Track1"; priva
//java code
public class Location_Track1 extends Activity {
private static final String TAG = "Location_Track1";
private static final long INTERVAL = 1000 * 60 * 1; //1 minute
private static final long FASTEST_INTERVAL = 1000 * 60 * 1; // 1 minute
private static final float SMALLEST_DISPLACEMENT = 0.25F; //quarter of a meter
Button btnFusedLocation;
TextView tvLocation;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mCurrentLocation;
String mLastUpdateTime;
GoogleMap googleMap;
private ImageView mCurrentPointer;
private double longitude;
private double latitude;
// private ArrayList<LatLng> points; //added
private List<LatLng> points = new ArrayList<>();
Polyline line; //added
TextView tv_mobno, tv_latitude, tv_longitude, tv_time;
String getLatitude;
String getLongitude;
Button slocation;
JSONArray result = null;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String CON = "CON";
JSONObject jsonobject;
JSONArray jsonarray;
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";
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setSmallestDisplacement(SMALLEST_DISPLACEMENT); //added
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_track);
Log.d(TAG, "onCreate ...............................");
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);
slocation=(Button)findViewById(R.id.button_slocation);
url = "http://example.in/gmap_track.php";
// points = new ArrayList<LatLng>();
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
slocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// new DownloadJSON().execute();
new JSONParse().execute();
}
});
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Location_Track1.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 longt = c.getString(TAG_LONG);
//Set JSON Data in TextView
tv_latitude.setText(lat);
tv_longitude.setText(longt);
/* double lat = c.getDouble(TAG_LAT);
double longt = c.getDouble(TAG_LONG);
MarkerOptions marker = new MarkerOptions().position(new LatLng(lat, longt)).title("point");
googleMap.addMarker(marker);*/
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
//java代码
公共类位置\u Track1扩展活动{
私有静态最终字符串TAG=“Location\u Track1”;
私有静态最终长间隔=1000*60*1;//1分钟
私有静态最终最长最快_间隔=1000*60*1;//1分钟
专用静态最终浮子最小位移=0.25F;//四分之一米
按钮BTN使用位置;
文本视图位置;
位置请求mLocationRequest;
GoogleapClient MGoogleapClient;
位置mCurrentLocation;
字符串mLastUpdateTime;
谷歌地图谷歌地图;
私有ImageView mCurrentPointer;
私人双经度;
私人双纬度;
//私有ArrayList点;//已添加
私有列表点=新的ArrayList();
多段线;//已添加
TextView tv_mobno、tv_纬度、tv_经度、tv_时间;
弦纬度;
字符串经度;
按钮定位;
JSONArray结果=null;
进程对话框;
ArrayList ArrayList;
静态字符串CON=“CON”;
JSONObject JSONObject;
JSONArray JSONArray;
私有字符串url=“”;
私有静态最终字符串标记_USER=“result”;
//私有静态最终字符串标记_SNAME=“pseats”;
私有静态最终字符串标记_LONG=“经度”;
私有静态最终字符串标记_LAT=“lation”;
私有静态最终字符串标记_ADDRESS=“paddress”;
受保护的void createLocationRequest(){
mlLocationRequest=新位置请求();
mlLocationRequest.setInterval(间隔);
mLocationRequest.SetFastTestInterval(最快间隔);
mlLocationRequest.setSmallestDisplacement(最小位移);//已添加
mLocationRequest.setPriority(位置请求.优先级高精度);
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、位置和轨道);
日志d(标签“onCreate”);
tv_mobno=(TextView)findViewById(R.id.TextView_mob);
tv_纬度=(TextView)findViewById(R.id.TextView_纬度);
tv_经度=(TextView)findViewById(R.id.TextView_经度);
tv_time=(TextView)findViewById(R.id.TextView_time);
slocation=(按钮)findViewById(R.id.Button\u slocation);
url=”http://example.in/gmap_track.php";
//points=新的ArrayList();
googleMap=((MapFragment)getFragmentManager().findFragmentById(R.id.map))
.getMap();
slocation.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//新建下载JSON().execute();
新建JSONParse().execute();
}
});
}
私有类JSONParse扩展了异步任务{
私人对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(位置\轨道1.this);
setMessage(“获取数据…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的JSONObject doInBackground(字符串…args){
JSONParser jParser=新的JSONParser();
//从URL获取JSON
JSONObject json=jParser.getJSONFromUrl(url);
返回json;
}
@凌驾
受保护的void onPostExecute(JSONObject json){
pDialog.disclose();
试一试{
//获取JSON数组
result=json.getJSONArray(TAG_USER);
JSONObject c=result.getJSONObject(0);
//在变量中存储JSON项
String lat=c.getString(TAG_lat);
String LONG=c.getString(TAG_LONG);
//在TextView中设置JSON数据
tv_纬度.setText(纬度);
tv_经度.setText(长);
/*双lat=c.getDouble(TAG_lat);
双长=c.getDouble(标记长);
MarkerOptions marker=新MarkerOptions().位置(新板条(板条,长板条)).标题(“点”);
谷歌地图。添加标记(标记)*/
}捕获(JSONException e){
e、 printStackTrace();
}
}
}
}
您必须使用:
GoogleMap.addMarker(MarkerOptions);
请观看此网站并回答此问题:
这是演示类,使用它可以向给定的lat和long添加标记
public class MarkerDemoActivity extends FragmentActivity implements
OnMarkerClickListener,
OnMapReadyCallback {
private static final LatLng PERTH = new LatLng(-31.952854, 115.857342);
private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689);
private static final LatLng BRISBANE = new LatLng(-27.47093, 153.0235);
private Marker mPerth;
private Marker mSydney;
private Marker mBrisbane;
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.marker_demo);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/** Called when the map is ready. */
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
// Add some markers to the map, and add a data object to each marker.
mPerth = mMap.addMarker(new MarkerOptions()
.position(PERTH)
.title("Perth");
mPerth.setTag(0);
mSydney = mMap.addMarker(new MarkerOptions()
.position(SYDNEY)
.title("Sydney");
mSydney.setTag(0);
mBrisbane = mMap.addMarker(new MarkerOptions()
.position(BRISBANE)
.title("Brisbane");
mBrisbane.setTag(0);
// Set a listener for marker click.
mMap.setOnMarkerClickListener(this);
}
/** Called when the user clicks a marker. */
@Override
public boolean onMarkerClick(final Marker marker) {
// Retrieve the data from the marker.
Integer clickCount = (Integer) marker.getTag();
// Check if a click count was set, then display the click count.
if (clickCount != null) {
clickCount = clickCount + 1;
marker.setTag(clickCount);
Toast.makeText(this,
marker.getTitle() +
" has been clicked " + clickCount + " times.",
Toast.LENGTH_SHORT).show();
}
// Return false to indicate that we have not consumed the event and that we wish
// for the default behavior to occur (which is for the camera to move such that the
// marker is centered and for the marker's info window to open, if it has one).
return false;
}
}为什么要注释掉添加标记的代码? 试试这个:
// Storing JSON item in a Variable
String lat = c.getString(TAG_LAT);
String longt = c.getString(TAG_LONG);
//Set JSON Data in TextView
tv_latitude.setText(lat);
tv_longitude.setText(longt);
//Parse data to double
double latD = Double.parseDouble(lat);
double longtD = Double.parseDouble(longt);
MarkerOptions marker = new MarkerOptions().position(new LatLng(latD, longtD)).title("point");
googleMap.addMarker(marker);
我想在给定的任何(经度,纬度)值处显示标记,该值是我从服务器获取的。只需将sydney(经度,纬度)值替换为从服务器获取的lat/long值即可。anyne可以建议我如何显示(经度,纬度)的json值吗正在将表单服务器提取到google map?使用上面的命令,它不会显示任何位置。map只会显示空白。如何将提取的Langitute和latitude值显示给标记?使用此命令,double latD=double.parseDouble(lat);double longtD=double.parseDouble(longt);无法获取Langitute和latitude的值。您使用错误的方法初始化地图。当我将munaly值添加到marker选项方法时,请尝试阅读我给出的链接,然后它会显示映射;double longtD=double.parseDouble(longt);不可能。
// Storing JSON item in a Variable
String lat = c.getString(TAG_LAT);
String longt = c.getString(TAG_LONG);
//Set JSON Data in TextView
tv_latitude.setText(lat);
tv_longitude.setText(longt);
//Parse data to double
double latD = Double.parseDouble(lat);
double longtD = Double.parseDouble(longt);
MarkerOptions marker = new MarkerOptions().position(new LatLng(latD, longtD)).title("point");
googleMap.addMarker(marker);