初学者在这里。 我在Android Studio中创建了一个带有地图片段的活动,我没有动态添加1个标记或非动态添加多个标记的问题,如下所示:
LatLng sydney = new LatLng(-34, 151);
LatLng spokane = new LatLng(-35, 171);
LatLng idaho = new LatLng(-32.3, 150);
但是,我需要在从mysql中获取坐标后动态设置一个标记数组。
目前,出于测试目的,我有一个按钮' getCoords'在点击时执行此操作(我将coords输出到textview以查看它是否有效。我能够获取要在textview中显示的值):
public void getCoords(View view){
new CoordsBackgroundTask().execute();
}
class CoordsBackgroundTask extends AsyncTask<Void, Void, String>{
String json_url_coords;
@Override
protected void onPreExecute(){
json_url_coords = "MY PHP FILE URL";
}
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL(json_url_coords);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
while ((JSON_STRING = bufferedReader.readLine())!=null)
{
stringBuilder.append(JSON_STRING+"\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
}catch (MalformedURLException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... values){
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
TextView textview = (TextView) findViewById(R.id.textview);
textview.setText(result);
json_string = result;
}
}
PHP:
<?php
$mysqli = new mysqli("localhost", "MYUSER", "MYPASS", "MYDB");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT markers.lat, markers.lng
FROM markers
INNER JOIN userIds ON markers.user_id = userIds.user_id";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
echo $row['lat'];
echo $row['lng'];
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
?>
我能够成功返回lat和lng数组,我只是在这里度过了一段艰难时期,关于如何点击从数据库获取coords的按钮,然后将这些标记添加到该活动的地图片段中。我需要以某种方式解析数组吗?我可以使用循环将标记带入地图吗?总会有不同数量的坐标。
我的地图看起来有点像这样。我在setUpMap中有它,所以我可以默认调用onMapReady,然后在单击按钮时调用它...
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUpMap();
}
private void setUpMap() {
LatLng sydney = new LatLng(-34, 151);
LatLng spokane = new LatLng(-35, 171);
LatLng idaho = new LatLng(-32.3, 150);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.addMarker(new MarkerOptions().position(spokane).title("Marker in spokane"));
mMap.addMarker(new MarkerOptions().position(idaho).title("Marker in idaho"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
谢谢!
答案 0 :(得分:1)
你可能需要一个for循环来插入标记,你可以发布PHP响应,这样我就可以帮你实现一些东西来解析一个LatLng列表,然后为每个标记添加一个标记。
<强> ANSWER 强>
假设您的积分是像lat,lng,#-32.430412,-58.321323,#
这样的行,并且您的服务器为您提供了一个列表,您首先将其拆分为:
String answer = "-32.430412,-58.321323,#-32.430412,-58.321323,#-32.430412,-58.321323,#";
// note that line breaks disappeared after parsing it to string
String[] parts = answer.split("#");
for (String point : parts) {
String[] pointData = point.split(",");
Float lat= Float.parseFloat(pointData[0]);
Float lng= Float.parseFloat(pointData[1]);
/// check if fit your actual map, remember you need everything required here loaded and declared before calling.
mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("Hello world"));
}