从数据库使用PHP和MySQL的android标记什么是最好的方法 [英] android markers from database using php and mysql what is the best method

查看:95
本文介绍了从数据库使用PHP和MySQL的android标记什么是最好的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在android工作室中构建应用程序基本上是一种应用程序,它会向用户显示许多标记,标记也由用户创建。基本上,我第一次使用android studio,而且我不想搞砸东西,并在这里发布一个问题,这样我就可以在开发代码时获得体验的声音并做好预防准备。



我的应用程序将使用php页面保存数据并从mysql数据库中获取数据,我只想知道用户会增加,指针也是如此,在我的应用程序中调用这些指针的最佳方法是什么



Json?
或是否有其他方法可以做到这一点,考虑到有很多标记是最佳做法。



是否将标记限制为在窗口视图中,代码的增加是否会增加服务器的负载,或者在移动屏幕时通常需要多长时间才能加载更多数据。



如果我加载所有的标记在同一时间,是否有机会滞后和利用手机和服务器资源?

我相信你们会引导我尽可能地利用最好的做法谢谢。

解决方案

您可以使用带有纬度经度值的JSON数据:

  [
{
name:New York,
latlng:[
62.457434,
17.349869
],
population:123
},
{
name:Dhaka,
latlng: [
62.455102,
17.345 599
],
人口:132
},
]

最好在地图加载过程中加载所有标记数据,因为如果你想根据用户的地图位置改变加载数据,它会在每次使用触摸地图时调用网络api,这将会拖延UI 。

您可以加载工作线程中的所有数据,因为它是网络操作。所以UI不会很慢。



以下是一个示例代码:

  public class MainActivity extends FragmentActivity {
private static final String LOG_TAG =ExampleApp;

private static final String SERVICE_URL =YOUR SERVICE URL;

保护GoogleMap地图;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpMapIfNeeded();


@Override
protected void onResume(){
super.onResume();
setUpMapIfNeeded();

$ b private void setUpMapIfNeeded(){
if(map == null){$ b $ map =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id .map))
.getMap();
if(map!= null){
setUpMap();



$ b private void setUpMap(){
//从Web服务中检索城市数据
// In一个工作者线程,因为这是一个网络操作。
new Thread(new Runnable(){
public void run(){
try {
retrieveAndAddCities();
} catch(IOException e){
Log.e(LOG_TAG,Can not retrieve cities,e);
return;
}
}
})。start();
}

protected void retrieveAndAddCities()throws IOException {
HttpURLConnection conn = null;
final StringBuilder json = new StringBuilder();
尝试{
//连接到Web服务
URL url =新URL(SERVICE_URL);
conn =(HttpURLConnection)url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());

//将JSON数据读入到StringBuilder中
int read;
char [] buff = new char [1024]; (读取= in.read(buff))!= -1){
json.append(buff,0,read);
while
}
} catch(IOException e){
Log.e(LOG_TAG,Error connected to service,e);
抛出新的IOException(连接服务错误,e);
} finally {
if(conn!= null){
conn.disconnect();
}
}

//为城市数据创建标记。
//必须在UI线程上运行它,因为它是一个UI操作。
runOnUiThread(new Runnable(){
public void run(){
try {
createMarkersFromJson(json.toString());
} catch(JSONException e) ($ LOG $(LOG_TAG,Error processing JSON,e);
}
}
});
}

void createMarkersFromJson(String json)throws JSONException {
//将JSON字符串反序列化成一个城市对象数组
JSONArray jsonArray = new JSONArray( JSON);
for(int i = 0; i< jsonArray.length(); i ++){
//在JSON数据中为每个城市创建一个标记。
JSONObject jsonObj = jsonArray.getJSONObject(i);
map.addMarker(new MarkerOptions()
.title(jsonObj.getString(name))
.snippet(Integer.toString(jsonObj.getInt(population)))
.position(new LatLng(
)jsonObj.getJSONArray(latlng)。getDouble(0),
jsonObj.getJSONArray(latlng)。getDouble(1)
))
);
}
}
}


I am building an app in android studio basically a kind of a app which will show many markers to the user, the markers are also created by the users.

Basically its my first time with the android studio and I don't want to mess up things, and posting a question here so I can get a voice of experience and be precaution while developing the code.

My application will use php pages to save data and fetch data from mysql database, I just want to know as the user will increase so does the pointers what is the best method to call those pointer in my application,

Json ? or is there any other way I can do it, the best practice considering there will be lots of markers.

Is it a good idea to restrict markers to the window view, does the increase in code will increase the load in server or how much time it will usually take to load further data when screen is moved.

If I load all the markers at same time, is there any chance of lagging and utilizing resource of phone and server ?

I believe you guys will guide me the best possible practice I can utilize thank you.

解决方案

You can use a JSON data with latitude longitude value:

    [
    {
        "name": "New York",
        "latlng": [
            62.457434,
            17.349869
        ],
        "population": "123"
    },
    {
        "name": "Dhaka",
        "latlng": [
            62.455102,
            17.345599
        ],
        "population": "132"
    },
] 

it is better to load all marker data during map load, because if u want to load data based on user's map location change it will call network api in every time use touch on map which will lag the UI.

you can load all data In a worker thread since it's a network operation. so UI will not be slow.

Here is a sample code:

    public class MainActivity extends FragmentActivity {
    private static final String LOG_TAG = "ExampleApp";

    private static final String SERVICE_URL = "YOUR SERVICE URL";

    protected GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        if (map == null) {
            map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            if (map != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        // Retrieve the city data from the web service
        // In a worker thread since it's a network operation.
        new Thread(new Runnable() {
            public void run() {
                try {
                    retrieveAndAddCities();
                } catch (IOException e) {
                    Log.e(LOG_TAG, "Cannot retrieve cities", e);
                    return;
                }
            }
        }).start();
    }

    protected void retrieveAndAddCities() throws IOException {
        HttpURLConnection conn = null;
        final StringBuilder json = new StringBuilder();
        try {
            // Connect to the web service
            URL url = new URL(SERVICE_URL);
            conn = (HttpURLConnection) url.openConnection();
            InputStreamReader in = new InputStreamReader(conn.getInputStream());

            // Read the JSON data into the StringBuilder
            int read;
            char[] buff = new char[1024];
            while ((read = in.read(buff)) != -1) {
                json.append(buff, 0, read);
            }
        } catch (IOException e) {
            Log.e(LOG_TAG, "Error connecting to service", e);
            throw new IOException("Error connecting to service", e);
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }

        // Create markers for the city data.
        // Must run this on the UI thread since it's a UI operation.
        runOnUiThread(new Runnable() {
            public void run() {
                try {
                    createMarkersFromJson(json.toString());
                } catch (JSONException e) {
                    Log.e(LOG_TAG, "Error processing JSON", e);
                }
            }
        });
    }

    void createMarkersFromJson(String json) throws JSONException {
        // De-serialize the JSON string into an array of city objects
        JSONArray jsonArray = new JSONArray(json);
        for (int i = 0; i < jsonArray.length(); i++) {
            // Create a marker for each city in the JSON data.
            JSONObject jsonObj = jsonArray.getJSONObject(i);
            map.addMarker(new MarkerOptions()
                .title(jsonObj.getString("name"))
                .snippet(Integer.toString(jsonObj.getInt("population")))
                .position(new LatLng(
                        jsonObj.getJSONArray("latlng").getDouble(0),
                        jsonObj.getJSONArray("latlng").getDouble(1)
                 ))
            );
        }
    }
}

这篇关于从数据库使用PHP和MySQL的android标记什么是最好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆