从数据库获取点来映射android

时间:2017-03-18 21:51:24

标签: java android

我想从我的数据库中显示标记来映射android,但它是我的一个空白页面,我能做到这一点已经3周了,我坚持了!!!帮助! 它是我项目入门的第一步。 我的主要活动

public class MainActivity extends FragmentActivity {

// Google Map
private GoogleMap googleMap;

// Latitude & Longitude
private Double Latitude = 0.00;
private Double Longitude = 0.00;

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

    //*** Permission StrictMode
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new   StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    ArrayList<HashMap<String, String>> location = null;
    String url = "http://192.168.1.3:80/pfe/getlatLon.php";
    try {

        JSONArray data = new JSONArray(getHttpGet(url));

        location = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;

        for(int i = 0; i < data.length(); i++){
            JSONObject c = data.getJSONObject(i);

            map = new HashMap<String, String>();
            map.put("LocationID", c.getString("LocationID"));
            map.put("Latitude", c.getString("Latitude"));
            map.put("Longitude", c.getString("Longitude"));
            map.put("LocationName", c.getString("LocationName"));
            location.add(map);

        }

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // *** Display Google Map
    googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap)).getMap();
    // *** Focus & Zoom

    Latitude =  Double.parseDouble(location.get(0).get("Latitude").toString());
     Longitude = Double.parseDouble(location.get(0).get("Longitude").toString());
    LatLng coordinate = new LatLng(Latitude, Longitude);
     googleMap.setMapType(com.google.android.gms.maps.GoogleMap.MAP_TYPE_HYBRID);
    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 17));

    // *** Marker (Loop)
    for (int i = 0; i < location.size(); i++) {
        Latitude = Double.parseDouble(location.get(i).get("Latitude").toString());
        Longitude =  Double.parseDouble(location.get(i).get("Longitude").toString());
        String name = location.get(i).get("LocationName").toString();
        MarkerOptions marker = new MarkerOptions().position(new  LatLng(Latitude, Longitude)).title(name);
        googleMap.addMarker(marker);
    }

}

 public static String getHttpGet(String url) {
    StringBuilder str = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) { // Download OK
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                str.append(line);
            }
        } else {
            Log.e("Log", "Failed to download result..");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return str.toString();
}

}

MY MANIFEST:




<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chaima.myapplicationpfe2">


<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>
    </activity>
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyC_VseeYaw_PHdYFT4t8L1xIV0tIBszEvI" />
</application> 

</manifest>

MY XML:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/googleMap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>


MY GRADLE:





<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/googleMap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>

2 个答案:

答案 0 :(得分:1)

如果您没有cloud console>API manager> Credentials>your MapAPI,请转到keytool -list -v -keystore mystore.keystore ,然后创建一个。{/ p>

使用以下方式获取您的包哈希:

Package name

确保您输入正确的localtexmf和SHA-1。

注意 是的,您必须为调试应用程序和发布应用程序添加2个不同的SHA

答案 1 :(得分:0)

我喜欢mOhemmed说..检查pakage和apikey ......这是所有代码..我可以使用它:D